Skip to content

Commit 5a46682

Browse files
perf: Refactor getSorterFn (#38)
* Enable stylistic-type-checked * Add back old sorter options * Remove stylistic-type-checked
1 parent fe061fe commit 5a46682

2 files changed

Lines changed: 28 additions & 36 deletions

File tree

src/publish/index.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -240,29 +240,29 @@ export const publish = async (options) => {
240240

241241
const changelogCommitsMd = await Promise.all(
242242
Object.entries(
243-
commitsSinceLatestTag.reduce((acc, next) => {
244-
const type = next.parsed.type?.toLowerCase() ?? 'other'
243+
commitsSinceLatestTag.reduce((prev, curr) => {
244+
const type = curr.parsed.type?.toLowerCase() ?? 'other'
245245

246246
return {
247-
...acc,
248-
[type]: [...(acc[type] || []), next],
247+
...prev,
248+
[type]: [...(prev[type] ?? []), curr],
249249
}
250250
}, /** @type {Record<string, import('./types.js').Commit[]>} */ ({})),
251251
)
252252
.sort(
253-
getSorterFn([
254-
([d]) =>
255-
[
256-
'other',
257-
'examples',
258-
'docs',
259-
'chore',
260-
'refactor',
261-
'perf',
262-
'fix',
263-
'feat',
264-
].indexOf(d),
265-
]),
253+
getSorterFn(([type]) =>
254+
[
255+
'other',
256+
'examples',
257+
'docs',
258+
'ci',
259+
'chore',
260+
'refactor',
261+
'perf',
262+
'fix',
263+
'feat',
264+
].indexOf(type),
265+
),
266266
)
267267
.reverse()
268268
.map(async ([type, commits]) => {

src/publish/utils.js

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,19 @@ export const updatePackageJson = async (pathName, transform) => {
3232

3333
/**
3434
* @template TItem
35-
* @param {((d: TItem) => any)[]} sorters
35+
* @param {((d: TItem) => number)} sorter
3636
* @returns {(a: TItem, b: TItem) => number}
3737
*/
38-
export const getSorterFn = (sorters) => {
38+
export const getSorterFn = (sorter) => {
3939
return (a, b) => {
40-
let i = 0
41-
42-
sorters.some((sorter) => {
43-
const sortedA = sorter(a)
44-
const sortedB = sorter(b)
45-
if (sortedA > sortedB) {
46-
i = 1
47-
return true
48-
}
49-
if (sortedA < sortedB) {
50-
i = -1
51-
return true
52-
}
53-
return false
54-
})
55-
56-
return i
40+
const sortedA = sorter(a)
41+
const sortedB = sorter(b)
42+
if (sortedA > sortedB) {
43+
return 1
44+
}
45+
if (sortedA < sortedB) {
46+
return -1
47+
}
48+
return 0
5749
}
5850
}

0 commit comments

Comments
 (0)