Skip to content

Commit f94ab01

Browse files
authored
refactor: simplify & dry (#385)
1 parent fe45870 commit f94ab01

1 file changed

Lines changed: 11 additions & 40 deletions

File tree

index.js

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -127,36 +127,13 @@ const sortDependenciesLikeNpm = sortObjectBy((a, b) => a.localeCompare(b, 'en'))
127127
*
128128
* @see https://docs.npmjs.com/cli/v7/using-npm/workspaces?v=true#running-commands-in-the-context-of-workspaces
129129
*/
130-
const sortWorkspaces = (workspaces) => {
131-
if (!isPlainObject(workspaces)) {
132-
return workspaces
133-
}
134-
135-
// Sort known properties in a specific order
136-
const sortedWorkspaces = {}
137-
138-
// First add packages if it exists
139-
if (workspaces.packages) {
140-
sortedWorkspaces.packages = uniqAndSortArray(workspaces.packages)
141-
}
142-
143-
// Then add catalog if it exists and sort it like dependencies
144-
if (workspaces.catalog) {
145-
sortedWorkspaces.catalog = sortDependenciesLikeNpm(workspaces.catalog)
146-
}
147-
148-
// Add any other properties in alphabetical order
149-
const knownKeys = ['packages', 'catalog']
150-
const otherKeys = Object.keys(workspaces)
151-
.filter((key) => !knownKeys.includes(key))
152-
.sort()
153-
154-
for (const key of otherKeys) {
155-
sortedWorkspaces[key] = workspaces[key]
156-
}
157-
158-
return sortedWorkspaces
159-
}
130+
const sortWorkspaces = onObject(
131+
pipe([
132+
sortObjectBy(['packages', 'catalog']),
133+
overProperty('packages', uniqAndSortArray),
134+
overProperty('catalog', sortDependenciesLikeNpm),
135+
]),
136+
)
160137

161138
// https://github.com/eslint/eslint/blob/acc0e47572a9390292b4e313b4a4bf360d236358/conf/config-schema.js
162139
const eslintBaseConfigProperties = [
@@ -525,15 +502,6 @@ function editStringJSON(json, over) {
525502
return over(json)
526503
}
527504

528-
const isPrivateKey = (key) => key[0] === '_'
529-
const partition = (array, predicate) =>
530-
array.reduce(
531-
(result, value) => {
532-
result[predicate(value) ? 0 : 1].push(value)
533-
return result
534-
},
535-
[[], []],
536-
)
537505
function sortPackageJson(jsonIsh, options = {}) {
538506
return editStringJSON(
539507
jsonIsh,
@@ -542,7 +510,10 @@ function sortPackageJson(jsonIsh, options = {}) {
542510

543511
if (Array.isArray(sortOrder)) {
544512
const keys = Object.keys(json)
545-
const [privateKeys, publicKeys] = partition(keys, isPrivateKey)
513+
const { privateKeys = [], publicKeys = [] } = objectGroupBy(
514+
keys,
515+
(key) => (key[0] === '_' ? 'privateKeys' : 'publicKeys'),
516+
)
546517
sortOrder = [
547518
...sortOrder,
548519
...defaultSortOrder,

0 commit comments

Comments
 (0)