Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: keithamus/sort-package-json
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.34.0
Choose a base ref
...
head repository: keithamus/sort-package-json
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.35.0
Choose a head ref
  • 1 commit
  • 2 files changed
  • 2 contributors

Commits on Dec 30, 2019

  1. feat: prepend custom order to default sort order (#108)

    * feat: prepend custom order to default sort order
    
    * fix: support function type `options.sortOrder`
    
    * refactor: remove redundant code
    
    * fix: add defaultSortOrder keys
    
    * style: prettier
    
    * style: prettier
    
    Co-authored-by: Keith Cirkel <keithamus@users.noreply.github.com>
    fisker and keithamus committed Dec 30, 2019
    Copy the full SHA
    90891fa View commit details
Showing with 26 additions and 1 deletion.
  1. +6 −1 index.js
  2. +20 −0 test.js
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -192,7 +192,12 @@ function sortPackageJson(jsonIsh, options = {}) {
if (Array.isArray(sortOrder)) {
const keys = Object.keys(json)
const [privateKeys, publicKeys] = partition(keys, isPrivateKey)
sortOrder = [...sortOrder, ...publicKeys.sort(), ...privateKeys.sort()]
sortOrder = [
...sortOrder,
...defaultSortOrder,
...publicKeys.sort(),
...privateKeys.sort(),
]
}

const newJson = sortObjectKeys(json, sortOrder)
20 changes: 20 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -92,6 +92,26 @@ fs.readFile('./package.json', 'utf8', (error, contents) => {
['z', 'a', 'name'],
)

// defaultSortOrder still applied, when using custom sortOrder
assert.deepStrictEqual(
Object.keys(
sortPackageJson(
{
b: 'b',
a: 'a',
z: 'z',
version: '1.0.0',
name: 'foo',
private: true,
},
{
sortOrder: ['z', 'private'],
},
),
),
['z', 'private', 'name', 'version', 'a', 'b'],
)

// Custom sort order should not effect field sorting
assert.deepStrictEqual(
Object.keys(