sort-object-keys
Advanced tools
+15
| // From DefinitelyTyped by Emily Marigold Klassen <https://github.com/forivall> | ||
| /** | ||
| * Sort an object's keys, including an optional key list. | ||
| * | ||
| * @param object - An object. | ||
| * @param sortWith - An ordered keys array or a sort function. | ||
| * @returns Sorted object. | ||
| */ | ||
| declare function sortObjectByKeyNameList<T>( | ||
| object: T, | ||
| sortWith?: ReadonlyArray<keyof T> | ((a: keyof T, b: keyof T) => number), | ||
| ): T; | ||
| export default sortObjectByKeyNameList; |
+7
-6
@@ -1,5 +0,6 @@ | ||
| module.exports = function sortObjectByKeyNameList(object, sortWith) { | ||
| var keys; | ||
| var sortFn; | ||
| const has = (object, key) => Object.prototype.hasOwnProperty.call(object, key); | ||
| export default function sortObjectByKeyNameList(object, sortWith) { | ||
| let keys, sortFn; | ||
| if (typeof sortWith === 'function') { | ||
@@ -11,5 +12,5 @@ sortFn = sortWith; | ||
| var objectKeys = Object.keys(object); | ||
| return (keys || []).concat(objectKeys.sort(sortFn)).reduce(function(total, key) { | ||
| if (objectKeys.indexOf(key) !== -1) { | ||
| const objectKeys = Object.keys(object); | ||
| return [...(keys ?? []), ...objectKeys.sort(sortFn)].reduce((total, key) => { | ||
| if (has(object, key)) { | ||
| total[key] = object[key]; | ||
@@ -16,0 +17,0 @@ } |
+20
-18
| { | ||
| "name": "sort-object-keys", | ||
| "version": "1.1.3", | ||
| "version": "2.0.0", | ||
| "description": "Sort an object's keys, including an optional key list", | ||
@@ -14,28 +14,30 @@ "keywords": [ | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+ssh://git@github.com/keithamus/sort-object-keys.git" | ||
| }, | ||
| "license": "MIT", | ||
| "author": "Keith Cirkel <npm@keithcirkel.co.uk> (http://keithcirkel.co.uk/)", | ||
| "type": "module", | ||
| "exports": { | ||
| ".": { | ||
| "import": { | ||
| "types": "./index.d.ts", | ||
| "default": "./index.js" | ||
| } | ||
| } | ||
| }, | ||
| "main": "index.js", | ||
| "types": "index.d.ts", | ||
| "files": [ | ||
| "index.js" | ||
| "index.js", | ||
| "index.d.ts" | ||
| ], | ||
| "main": "index.js", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+ssh://git@github.com/keithamus/sort-object-keys.git" | ||
| }, | ||
| "scripts": { | ||
| "semantic-release": "travis-after-all && semantic-release pre && npm publish && semantic-release post", | ||
| "sort": "sort-package-json", | ||
| "test": "node test.js" | ||
| }, | ||
| "config": { | ||
| "ghooks": { | ||
| "pre-commit": "npm t", | ||
| "commit-msg": "validate-commit-msg" | ||
| } | ||
| }, | ||
| "devDependencies": { | ||
| "ghooks": "^1.0.1", | ||
| "semantic-release": "^4.3.5", | ||
| "travis-after-all": "^1.4.4", | ||
| "validate-commit-msg": "^2.4.1" | ||
| "sort-package-json": "^3.4.0" | ||
| } | ||
| } |
3112
15.52%1
-75%4
33.33%29
81.25%Yes
NaN