Skip to content

Commit

Permalink
perf: Refactor getSorterFn (#38)
Browse files Browse the repository at this point in the history
* Enable stylistic-type-checked

* Add back old sorter options

* Remove stylistic-type-checked
  • Loading branch information
lachlancollins authored Feb 4, 2024
1 parent fe061fe commit 5a46682
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 36 deletions.
34 changes: 17 additions & 17 deletions src/publish/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,29 +240,29 @@ export const publish = async (options) => {

const changelogCommitsMd = await Promise.all(
Object.entries(
commitsSinceLatestTag.reduce((acc, next) => {
const type = next.parsed.type?.toLowerCase() ?? 'other'
commitsSinceLatestTag.reduce((prev, curr) => {
const type = curr.parsed.type?.toLowerCase() ?? 'other'

return {
...acc,
[type]: [...(acc[type] || []), next],
...prev,
[type]: [...(prev[type] ?? []), curr],
}
}, /** @type {Record<string, import('./types.js').Commit[]>} */ ({})),
)
.sort(
getSorterFn([
([d]) =>
[
'other',
'examples',
'docs',
'chore',
'refactor',
'perf',
'fix',
'feat',
].indexOf(d),
]),
getSorterFn(([type]) =>
[
'other',
'examples',
'docs',
'ci',
'chore',
'refactor',
'perf',
'fix',
'feat',
].indexOf(type),
),
)
.reverse()
.map(async ([type, commits]) => {
Expand Down
30 changes: 11 additions & 19 deletions src/publish/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,19 @@ export const updatePackageJson = async (pathName, transform) => {

/**
* @template TItem
* @param {((d: TItem) => any)[]} sorters
* @param {((d: TItem) => number)} sorter
* @returns {(a: TItem, b: TItem) => number}
*/
export const getSorterFn = (sorters) => {
export const getSorterFn = (sorter) => {
return (a, b) => {
let i = 0

sorters.some((sorter) => {
const sortedA = sorter(a)
const sortedB = sorter(b)
if (sortedA > sortedB) {
i = 1
return true
}
if (sortedA < sortedB) {
i = -1
return true
}
return false
})

return i
const sortedA = sorter(a)
const sortedB = sorter(b)
if (sortedA > sortedB) {
return 1
}
if (sortedA < sortedB) {
return -1
}
return 0
}
}

0 comments on commit 5a46682

Please sign in to comment.