Skip to content

Commit

Permalink
Merge pull request #11 from fisker/fix-extra-keys
Browse files Browse the repository at this point in the history
fix: do not add extra keys to object
  • Loading branch information
keithamus committed Dec 10, 2019
2 parents e2d65d5 + 8ddc383 commit 084f2be
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 6 additions & 2 deletions index.js
Expand Up @@ -7,8 +7,12 @@ module.exports = function sortObjectByKeyNameList(object, sortWith) {
} else {
keys = sortWith;
}
return (keys || []).concat(Object.keys(object).sort(sortFn)).reduce(function(total, key) {
total[key] = object[key];

var objectKeys = Object.keys(object);
return (keys || []).concat(objectKeys.sort(sortFn)).reduce(function(total, key) {
if (objectKeys.indexOf(key) !== -1) {
total[key] = object[key];
}
return total;
}, Object.create(null));
}
8 changes: 8 additions & 0 deletions test.js
Expand Up @@ -42,3 +42,11 @@ assert.equal(JSON.stringify(sortObject({
"key-3": 1,
"key-10": 1,
}));

assert.deepEqual(Object.keys(sortObject({
b: 1,
a: 1,
}, ['a', 'b', 'c', 'd'])), [
'a',
'b'
])

0 comments on commit 084f2be

Please sign in to comment.