sort-keys-recursive
Advanced tools
Comparing version 1.2.1 to 2.0.0
@@ -0,1 +1,12 @@ | ||
<a name="2.0.0"></a> | ||
# 2.0.0 (2017-02-26) | ||
* test/docs: add test and docs for the custom sort function parameter ([6c570dd](https://github.com/kikobeats/sort-keys-recursive/commit/6c570dd)) | ||
* Update docs ([5b64158](https://github.com/kikobeats/sort-keys-recursive/commit/5b64158)) | ||
* refactor: move 'compareFunction' parameter to 'options' ([a4145da](https://github.com/kikobeats/sort-keys-recursive/commit/a4145da)) | ||
* feat: add 'options' support with a new third, optional parameter ([c9a1995](https://github.com/kikobeats/sort-keys-recursive/commit/c9a1995)) | ||
* test: add non-array/non-object test ([a98894c](https://github.com/kikobeats/sort-keys-recursive/commit/a98894c)) | ||
<a name="1.2.1"></a> | ||
@@ -2,0 +13,0 @@ ## 1.2.1 (2017-01-06) |
30
index.js
@@ -6,2 +6,8 @@ 'use strict' | ||
function inArray (value, arr) { | ||
if (!arr || kindOf(arr) !== 'array') return false | ||
return arr.indexOf(value) > -1 | ||
} | ||
function sortObjectKeys (obj, compare) { | ||
@@ -11,8 +17,11 @@ return sortKeys(obj, compare) | ||
function sortArray (arr, compare) { | ||
return arr.slice().sort(compare) | ||
function sortArray (arr, options) { | ||
const compareFunction = options && options.compareFunction | ||
return arr.slice().sort(compareFunction) | ||
} | ||
function sortObject (obj, compare) { | ||
var result = sortObjectKeys(obj, compare) | ||
function sortObject (obj, options) { | ||
const compareFunction = options && options.compareFunction | ||
var result = sortObjectKeys(obj, compareFunction) | ||
@@ -24,3 +33,5 @@ Object.keys(obj).forEach(function (key) { | ||
if (type === 'object') { | ||
result[key] = sortObject(current, compare) | ||
if (!options || !inArray(key, options.ignoreObjectAtKeys)) { | ||
result[key] = sortObject(current, options) | ||
} | ||
return | ||
@@ -30,3 +41,5 @@ } | ||
if (type === 'array') { | ||
result[key] = sortArray(current) | ||
if (!options || !inArray(key, options.ignoreArrayAtKeys)) { | ||
result[key] = sortArray(current, options) | ||
} | ||
return | ||
@@ -39,5 +52,6 @@ } | ||
function sort (something, compareFn) { | ||
function sort (something, opts) { | ||
var type = kindOf(something) | ||
if (sort[type]) return sort[type](something, compareFn) | ||
if (sort[type]) return sort[type](something, opts) | ||
return something | ||
@@ -44,0 +58,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"homepage": "https://github.com/Kikobeats/sort-keys-recursive", | ||
"version": "1.2.1", | ||
"version": "2.0.0", | ||
"main": "./index.js", | ||
@@ -8,0 +8,0 @@ "author": { |
121
README.md
@@ -11,3 +11,3 @@ # sort-keys-recursive | ||
> Sort the keys of an object recursively | ||
> Sort the keys of an object recursively. | ||
@@ -23,5 +23,5 @@ ## Install | ||
```js | ||
var sort = require('sort-keys-recursive') | ||
const sortKeysRecursive = require('sort-keys-recursive') | ||
var object = { | ||
const object = { | ||
c: 0, | ||
@@ -36,3 +36,3 @@ a: { | ||
var output = sort(object) | ||
const output = sortKeysRecursive(object) | ||
@@ -53,4 +53,117 @@ console.log(output) | ||
## API | ||
### sortKeysRecursive(input, [options]) | ||
#### input | ||
*Required*<br> | ||
Type: `array`|`object` | ||
The collection to be sorted. | ||
#### options | ||
##### compareFunction | ||
Type: `function` | ||
[Compare function](https://github.com/sindresorhus/sort-keys#compare). | ||
##### 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 | ||
### <code>ignoreArrayAtKeys</code> and <code>ignoreObjectAtKeys</code> | ||
```js | ||
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'] | ||
// } | ||
``` | ||
### <code>compareFunction</code> | ||
You can pass a custom sort function as <code>compareFunction</code>. This function is passed to Javascript <code>[sort()](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)</code>, that sorts in alphabetical order by default. The custom function should return zero, a negative or positive value: | ||
```js | ||
var reverseAlphabeticalSort = function (a, b) { | ||
return a < b | ||
} | ||
var options = { | ||
compareFunction: reverseAlphabeticalSort | ||
} | ||
var object = { | ||
a: { | ||
a: 0, | ||
c: ['c', 'a', 'b'], | ||
b: 0 | ||
}, | ||
c: 0, | ||
b: 0 | ||
} | ||
var output = sort(object, options) | ||
console.log(output) | ||
// { | ||
// c: 0, | ||
// b: 0, | ||
// a: { | ||
// c: ['c', 'b', 'a'], | ||
// b: 0, | ||
// a: 0 | ||
// } | ||
// } | ||
``` | ||
## License | ||
MIT © [Kiko Beats](http://www.kikobeats.com) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10382
43
166