Comparing version 1.0.0 to 1.1.0
33
index.js
'use strict'; | ||
module.exports = function (obj, compareFn) { | ||
if (typeof obj !== 'object') { | ||
var isObj = require('is-obj'); | ||
module.exports = function (obj, opts) { | ||
if (!isObj(obj)) { | ||
throw new TypeError('Expected an object'); | ||
} | ||
var ret = {}; | ||
opts = opts || {}; | ||
Object.keys(obj).sort(compareFn).forEach(function (el) { | ||
ret[el] = obj[el]; | ||
}); | ||
// DEPRECATED | ||
if (typeof opts === 'function') { | ||
opts = {compare: opts}; | ||
} | ||
return ret; | ||
var deep = opts.deep; | ||
var sortKeys = function (x) { | ||
var ret = {}; | ||
var keys = Object.keys(x).sort(opts.compare); | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i]; | ||
var val = x[key]; | ||
ret[key] = deep && val !== x && isObj(val) ? sortKeys(val) : val; | ||
} | ||
return ret; | ||
}; | ||
return sortKeys(obj); | ||
}; |
{ | ||
"name": "sort-keys", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Sort the keys of an object", | ||
@@ -10,3 +10,3 @@ "license": "MIT", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "http://sindresorhus.com" | ||
"url": "sindresorhus.com" | ||
}, | ||
@@ -27,4 +27,12 @@ "engines": { | ||
"key", | ||
"sort" | ||
"sort", | ||
"stable", | ||
"deterministic", | ||
"deep", | ||
"recursive", | ||
"recursively" | ||
], | ||
"dependencies": { | ||
"is-obj": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
@@ -31,0 +39,0 @@ "mocha": "*" |
@@ -5,3 +5,3 @@ # sort-keys [![Build Status](https://travis-ci.org/sindresorhus/sort-keys.svg?branch=master)](https://travis-ci.org/sindresorhus/sort-keys) | ||
Useful to get a deterministically ordered object as the order of keys can vary between engines. | ||
Useful to get a deterministically ordered object, as the order of keys can vary between engines. | ||
@@ -11,3 +11,3 @@ | ||
```sh | ||
``` | ||
$ npm install --save sort-keys | ||
@@ -25,4 +25,9 @@ ``` | ||
sortKeys({c: 0, a: 0, b: 0}, function (a, b) { | ||
return -a.localeCompare(b); | ||
sortKeys({b: {b: 0, a: 0}, a: 0}, {deep: true}); | ||
//=> {a: 0, b: {a: 0, b: 0}} | ||
sortKeys({c: 0, a: 0, b: 0}, { | ||
compare: function (a, b) { | ||
return -a.localeCompare(b); | ||
} | ||
}); | ||
@@ -35,3 +40,3 @@ //=> {c: 0, b: 0, a: 0} | ||
### sortKeys(input, [compare]) | ||
### sortKeys(input, [options]) | ||
@@ -45,4 +50,12 @@ Returns a new object with sorted keys. | ||
#### compare | ||
#### options | ||
##### deep | ||
Type: `boolean` | ||
Recursively sort keys. | ||
##### compare | ||
Type: `function` | ||
@@ -49,0 +62,0 @@ |
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
3379
4
24
64
1
+ Addedis-obj@^1.0.0
+ Addedis-obj@1.0.1(transitive)