sort-object-keys
Advanced tools
+8
-5
| const has = (object, key) => Object.prototype.hasOwnProperty.call(object, key); | ||
| export default function sortObjectByKeyNameList(object, sortWith) { | ||
| let keys, sortFn; | ||
| let keys, sortFn, key; | ||
@@ -12,9 +12,12 @@ if (typeof sortWith === 'function') { | ||
| const objectKeys = Object.keys(object); | ||
| return [...(keys ?? []), ...objectKeys.sort(sortFn)].reduce((total, key) => { | ||
| const total = {}; | ||
| const objectKeys = [...(keys ?? []), ...Object.keys(object).sort(sortFn)]; | ||
| for (key of objectKeys) { | ||
| if (has(object, key)) { | ||
| total[key] = object[key]; | ||
| } | ||
| return total; | ||
| }, Object.create(null)); | ||
| } | ||
| return total; | ||
| } |
+3
-1
| { | ||
| "name": "sort-object-keys", | ||
| "version": "2.0.0", | ||
| "version": "2.0.1", | ||
| "description": "Sort an object's keys, including an optional key list", | ||
@@ -36,2 +36,3 @@ "keywords": [ | ||
| "scripts": { | ||
| "prepare": "husky", | ||
| "sort": "sort-package-json", | ||
@@ -41,4 +42,5 @@ "test": "node test.js" | ||
| "devDependencies": { | ||
| "husky": "^9.1.7", | ||
| "sort-package-json": "^3.4.0" | ||
| } | ||
| } |
+2
-4
| # Sort Object | ||
| [](https://travis-ci.org/keithamus/sort-object-keys) | ||
@@ -12,4 +10,4 @@ | ||
| ```js | ||
| const assert = require('assert'); | ||
| const sortObject = require('sort-object-keys'); | ||
| import assert from 'assert'; | ||
| import sortObject from 'sort-object-keys'; | ||
@@ -16,0 +14,0 @@ assert.equal(JSON.stringify({ |
3132
0.64%30
3.45%2
100%55
-3.51%