Skip to content

Commit

Permalink
feat: support peerDependenciesMeta field (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored and keithamus committed Jan 11, 2020
1 parent 4a554fd commit d25c728
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 9 deletions.
16 changes: 15 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,19 @@ const uniqAndSortArray = pipe([uniq, sortArray])
const isPlainObject = x =>
x && Object.prototype.toString.call(x) === '[object Object]'
const onObject = fn => x => (isPlainObject(x) ? fn(x) : x)
const sortObjectBy = comparator => onObject(x => sortObjectKeys(x, comparator))
const sortObjectBy = (comparator, deep) => {
const over = onObject(object => {
object = sortObjectKeys(object, comparator)
if (deep) {
for (const [key, value] of Object.entries(object)) {
object[key] = over(value)
}
}
return object
})

return over
}
const sortObject = sortObjectBy()
const sortURLObject = sortObjectBy(['type', 'url'])
const sortPeopleObject = sortObjectBy(['name', 'email', 'url'])
Expand Down Expand Up @@ -227,6 +239,8 @@ const fields = [
{ key: 'dependencies', over: sortObject },
{ key: 'devDependencies', over: sortObject },
{ key: 'peerDependencies', over: sortObject },
// TODO: only sort depth = 2
{ key: 'peerDependenciesMeta', over: sortObjectBy(undefined, true) },
{ key: 'optionalDependencies', over: sortObject },
{ key: 'bundledDependencies', over: uniqAndSortArray },
{ key: 'bundleDependencies', over: uniqAndSortArray },
Expand Down
31 changes: 23 additions & 8 deletions tests/_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,34 @@ const sortPackageJsonAsObject = ({ path, value, options }) =>
path,
)

const keysToObject = keys => {
const keysToObject = (keys, depth = 1) => {
if (keys.some((value, index) => keys.indexOf(value) !== index)) {
throw new Error(`${keys} should be unique.`)
}
return keys.reduce((object, key) => Object.assign(object, { [key]: key }), {})

if (depth < 1) {
throw new Error(`depth should be a positive integer, got ${depth}.`)
}

return keys.reduce(
(object, key) =>
Object.assign(object, {
[key]: depth > 1 ? keysToObject(keys, depth - 1) : key,
}),
{},
)
}

function sortObjectAlphabetically(t, options) {
sortObject(t, {
...options,
value: keysToObject(['z', 'a']),
expect: keysToObject(['a', 'z']),
})
function sortObjectAlphabetically(t, options = {}) {
const { maxDepth = 1, expect } = options

for (let depth = 1; depth < maxDepth + 1; depth++) {
sortObject(t, {
...options,
value: keysToObject(['z', 'a'], depth),
expect: expect || keysToObject(['a', 'z'], depth),
})
}
}

function sortObject(
Expand Down
8 changes: 8 additions & 0 deletions tests/deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ for (const field of [
]) {
test(field, macro.uniqueAndSort, { path: field })
}

// peerDependenciesMeta
test('peerDependenciesMeta', macro.sortObjectAlphabetically, {
path: 'peerDependenciesMeta',
maxDepth: 2,
// TODO: don't use snapshot, find a esaier way for review
expect: 'snapshot',
})
57 changes: 57 additions & 0 deletions tests/snapshots/deps.js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Snapshot report for `tests/deps.js`

The actual snapshot is saved in `deps.js.snap`.

Generated by [AVA](https://ava.li).

## peerDependenciesMeta

> Should sort `peerDependenciesMeta` as object.
{
input: `{␊
"peerDependenciesMeta": {␊
"z": "z",␊
"a": "a"␊
}␊
}`,
options: undefined,
output: `{␊
"peerDependenciesMeta": {␊
"a": "a",␊
"z": "z"␊
}␊
}`,
pretty: true,
}

> Should sort `peerDependenciesMeta` as object.
{
input: `{␊
"peerDependenciesMeta": {␊
"z": {␊
"z": "z",␊
"a": "a"␊
},␊
"a": {␊
"z": "z",␊
"a": "a"␊
}␊
}␊
}`,
options: undefined,
output: `{␊
"peerDependenciesMeta": {␊
"a": {␊
"a": "a",␊
"z": "z"␊
},␊
"z": {␊
"a": "a",␊
"z": "z"␊
}␊
}␊
}`,
pretty: true,
}
Binary file added tests/snapshots/deps.js.snap
Binary file not shown.
2 changes: 2 additions & 0 deletions tests/snapshots/main.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Generated by [AVA](https://ava.li).
"optionalDependencies": "optionalDependencies",␊
"os": "os",␊
"peerDependencies": "peerDependencies",␊
"peerDependenciesMeta": "peerDependenciesMeta",␊
"pre-commit": "pre-commit",␊
"preferGlobal": "preferGlobal",␊
"prettier": "prettier",␊
Expand Down Expand Up @@ -160,6 +161,7 @@ Generated by [AVA](https://ava.li).
"dependencies": "dependencies",␊
"devDependencies": "devDependencies",␊
"peerDependencies": "peerDependencies",␊
"peerDependenciesMeta": "peerDependenciesMeta",␊
"optionalDependencies": "optionalDependencies",␊
"bundledDependencies": "bundledDependencies",␊
"bundleDependencies": "bundleDependencies",␊
Expand Down
Binary file modified tests/snapshots/main.js.snap
Binary file not shown.

0 comments on commit d25c728

Please sign in to comment.