sort-object-keys
Advanced tools
Comparing version 1.0.0 to 1.1.0
12
index.js
@@ -1,3 +0,11 @@ | ||
module.exports = function sortObjectByKeyNameList(object, keys) { | ||
return (keys || []).concat(Object.keys(object).sort()).reduce((total, key) => { | ||
module.exports = function sortObjectByKeyNameList(object, sortWith) { | ||
var keys; | ||
var sortFn; | ||
if (typeof sortWith === 'function') { | ||
sortFn = sortWith; | ||
} else { | ||
keys = sortWith; | ||
} | ||
return (keys || []).concat(Object.keys(object).sort(sortFn)).reduce((total, key) => { | ||
total[key] = object[key]; | ||
@@ -4,0 +12,0 @@ return total; |
{ | ||
"name": "sort-object-keys", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Sort an object's keys, including an optional key list", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "test.js" | ||
"test": "node test.js" | ||
}, | ||
@@ -9,0 +9,0 @@ "repository": { |
Sort Object | ||
[![Build Status](https://travis-ci.org/keithamus/sort-object-keys.svg)](https://travis-ci.org/keithamus/sort-object-keys) | ||
Returns a copy of an object with all keys sorted. | ||
You can pass an array of keys, which it will use for ordering - to provide custom sorts. | ||
The second argument is optional and is used for ordering - to provide custom sorts. You can either pass an array containing ordered keys or a function to sort the keys (same signature as in [`Array.prototype.sort()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)). | ||
@@ -34,2 +36,20 @@ ```js | ||
})); | ||
function removeKeyAncCompareIndex(keyA, keyB){ | ||
var a = parseInt(keyA.slice(4)); | ||
var b = parseInt(keyB.slice(4)); | ||
return a - b; | ||
} | ||
assert.equal(JSON.stringify(sortObject({ | ||
"key-1": 1, | ||
"key-3": 1, | ||
"key-10": 1, | ||
"key-2": 1, | ||
}, removeKeyAncCompareIndex)), JSON.stringify({ | ||
"key-1": 1, | ||
"key-2": 1, | ||
"key-3": 1, | ||
"key-10": 1, | ||
})); | ||
``` |
18
test.js
@@ -27,1 +27,19 @@ const assert = require('assert'); | ||
})); | ||
function removeKeyAncCompareIndex(keyA, keyB){ | ||
var a = parseInt(keyA.slice(4)); | ||
var b = parseInt(keyB.slice(4)); | ||
return a - b; | ||
} | ||
assert.equal(JSON.stringify(sortObject({ | ||
"key-1": 1, | ||
"key-3": 1, | ||
"key-10": 1, | ||
"key-2": 1, | ||
}, removeKeyAncCompareIndex)), JSON.stringify({ | ||
"key-1": 1, | ||
"key-2": 1, | ||
"key-3": 1, | ||
"key-10": 1, | ||
})); |
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
2967
5
53
55