包详细信息

sort-keys-recursive

kikobeats87.3kMIT2.1.10

Sort the keys of an object recursively

array, deep, deterministic, key

自述文件

sort-keys-recursive

Last version Coverage Status NPM Status

Sort the keys of an object recursively.

Install

npm install sort-keys-recursive --save

Usage

const sortKeysRecursive = require('sort-keys-recursive')

const object = {
  c: 0,
  a: {
    c: ['c', 'a', 'b'],
    a: 0,
    b: 0
  },
  b: 0
}

const output = sortKeysRecursive(object)

console.log(output)

// {
//   a: {
//     a: 0,
//     b: 0,
//     c: ['a', 'b', 'c']
//   },
//   b: 0,
//   c: 0
// };

API

sortKeysRecursive(input, [options])

input

Required
Type: array|object

The collection to be sorted.

options

compareFunction

Type: function

Compare function.

ignoreArrayAtKeys

Type: array

Don't sort the Array at the specified keys, if any.

ignoreObjectAtKeys

Type: array

Don't sort the Object at the specified keys, if any.

Examples

ignoreArrayAtKeys and ignoreObjectAtKeys


const options = {
  ignoreArrayAtKeys: [ // Don't sort the Array at the specified keys, if any.
    'b'
  ],
  ignoreObjectAtKeys: [ // Don't sort the Object at the specified keys, if any.
    'a'
  ]
}

const input = {
  a: { // This Object will not be sorted.
    a: 'a',
    b: 'b',
    c: 'c',
    d: ['a', 'c', 'b']
  },
  b: ['a', 'c', 'b'], // This Array will not be sorted.
  d: ['a', 'c', 'b']
}

const output = sort(object, options)

console.log(output)

// {
//   a: {
//     a: 'a',
//     b: 'b',
//     c: 'c',
//     d: ['a', 'c', 'b']
//   },
//   b: ['a', 'c', 'b'],
//   d: ['a', 'b', 'c']
// }

compareFunction

You can pass a custom sort function as compareFunction. This function is passed to Javascript sort(), that sorts in alphabetical order by default. The custom function should return zero, a negative or positive value:

const reverseAlphabeticalSort = function (a, b) {
  return a < b
}

const options = {
  compareFunction: reverseAlphabeticalSort
}

const object = {
  a: {
    a: 0,
    c: ['c', 'a', 'b'],
    b: 0
  },
  c: 0,
  b: 0
}

const output = sort(object, options)

console.log(output)

// {
//   c: 0,
//   b: 0,
//   a: {
//     c: ['c', 'b', 'a'],
//     b: 0,
//     a: 0
//   }
// }

License

MIT © Kiko Beats

更新日志

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

2.1.10 (2023-10-23)

2.1.9 (2023-09-05)

2.1.8 (2022-11-16)

2.1.7 (2022-05-17)

2.1.6 (2022-04-11)

2.1.5 (2022-04-01)

2.1.4 (2022-03-02)

2.1.3 (2022-02-24)

2.1.2 (2021-10-12)

Bug Fixes

2.1.1 (2021-01-01)

2.1.0 (2020-10-14)

Features

  • add 'options' support with a new third, optional parameter (c9a1995)

Bug Fixes

  • package: update kind-of to version 5.1.0 (a431902)
  • package: update kind-of to version 6.0.0 (3688a3a)
  • package: update sort-keys to version 3.0.0 (497af99)

2.0.1 (2017-07-18)

  • Fix interface (883ae34)
  • Fix linter (81b0cd3)
  • Fix linter (1792395)
  • Little Refactor (0a40c2c)
  • Update README.md (f986868)
  • Update travis builds (0a949fe)
  • docs(readme): add Greenkeeper badge (14b62b8)
  • chore(package): update dependencies (2dcdb49)
  • chore(package): update kind-of to version 3.2.2 (39b9d7f)
  • chore(package): update kind-of to version 4.0.0 (4cedab2)

2.0.0 (2017-02-26)

  • test/docs: add test and docs for the custom sort function parameter (6c570dd)
  • Update docs (5b64158)
  • refactor: move 'compareFunction' parameter to 'options' (a4145da)
  • feat: add 'options' support with a new third, optional parameter (c9a1995)
  • test: add non-array/non-object test (a98894c)

1.2.1 (2017-01-06)

1.2.0 (2017-01-06)

  • Add babel config (bfa9719)
  • Add dist version (d9049d8)
  • Add initial version (6cf5a69)
  • Add new JS class for the file (57a007a)
  • Avoid mutate state (7fc4b28)
  • Fix issues with the build scripts (02c77d1)
  • New Year Refactor (e408cbf)
  • Remove class into hiw own file (6ed57c4)
  • Remove no longer used file (6abe470)
  • Remove not required files anymore (2e7d6ab)
  • Remove not required packages any more (c8a81a8)
  • Remove not used files anymore (71911f7)
  • Set correct permissions to files (1c3836d)
  • Test on the dist version (2c3750c)

1.1.1 (2015-06-15)

1.1.0 (2015-06-14)

1.0.3 (2015-03-16)

1.0.2 (2015-03-04)