pax_global_header00006660000000000000000000000064151312140320014502gustar00rootroot0000000000000052 comment=2319c64ffbe7970c8987090ef63cb913668c587f sort-object-keys-2.1.0/000077500000000000000000000000001513121403200147065ustar00rootroot00000000000000sort-object-keys-2.1.0/.github/000077500000000000000000000000001513121403200162465ustar00rootroot00000000000000sort-object-keys-2.1.0/.github/workflows/000077500000000000000000000000001513121403200203035ustar00rootroot00000000000000sort-object-keys-2.1.0/.github/workflows/publish.yml000066400000000000000000000010241513121403200224710ustar00rootroot00000000000000name: Publish on: release: types: [created] jobs: publish-npm: runs-on: ubuntu-latest permissions: id-token: write steps: - uses: actions/checkout@v6 - uses: actions/setup-node@v6 with: node-version: 25 registry-url: https://registry.npmjs.org/ cache: npm - run: npm ci - run: npm test - run: npm version ${TAG_NAME} --git-tag-version=false env: TAG_NAME: ${{ github.event.release.tag_name }} - run: npm publish sort-object-keys-2.1.0/.gitignore000066400000000000000000000000151513121403200166720ustar00rootroot00000000000000node_modules sort-object-keys-2.1.0/.husky/000077500000000000000000000000001513121403200161275ustar00rootroot00000000000000sort-object-keys-2.1.0/.husky/pre-commit000066400000000000000000000000571513121403200201300ustar00rootroot00000000000000npm run sort && npm run format && npm run test sort-object-keys-2.1.0/README.md000066400000000000000000000025371513121403200161740ustar00rootroot00000000000000# 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. 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)). ```js import assert from 'assert' import sortObject from 'sort-object-keys' assert.equal( JSON.stringify({ c: 1, b: 1, d: 1, a: 1, }), JSON.stringify({ a: 1, b: 1, c: 1, d: 1, }), ) assert.equal( JSON.stringify( sortObject( { c: 1, b: 1, d: 1, a: 1, }, ['b', 'a', 'd', 'c'], ), ), JSON.stringify({ b: 1, a: 1, d: 1, c: 1, }), ) 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, }), ) ``` sort-object-keys-2.1.0/index.d.ts000066400000000000000000000007011513121403200166050ustar00rootroot00000000000000// From DefinitelyTyped by Emily Marigold Klassen /** * 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( object: T, sortWith?: ReadonlyArray | ((a: keyof T, b: keyof T) => number), ): T export default sortObjectByKeyNameList sort-object-keys-2.1.0/index.js000066400000000000000000000007421513121403200163560ustar00rootroot00000000000000const has = (object, key) => Object.prototype.hasOwnProperty.call(object, key) export default function sortObjectByKeyNameList(object, sortWith) { let keys, sortFn, key if (typeof sortWith === 'function') { sortFn = sortWith } else { keys = sortWith } const total = {} const objectKeys = [...(keys ?? []), ...Object.keys(object).sort(sortFn)] for (key of objectKeys) { if (has(object, key)) { total[key] = object[key] } } return total } sort-object-keys-2.1.0/package-lock.json000066400000000000000000000137071513121403200201320ustar00rootroot00000000000000{ "name": "sort-object-keys", "version": "2.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "sort-object-keys", "version": "2.0.1", "license": "MIT", "devDependencies": { "husky": "^9.1.7", "prettier": "^3.6.2", "sort-package-json": "^3.4.0" } }, "node_modules/detect-indent": { "version": "7.0.2", "resolved": "https://registry.npmjs.org/detect-indent/-/detect-indent-7.0.2.tgz", "integrity": "sha512-y+8xyqdGLL+6sh0tVeHcfP/QDd8gUgbasolJJpY7NgeQGSZ739bDtSiaiDgtoicy+mtYB81dKLxO9xRhCyIB3A==", "dev": true, "license": "MIT", "engines": { "node": ">=12.20" }, "funding": { "url": "https://github.com/sponsors/sindresorhus" } }, "node_modules/detect-newline": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/detect-newline/-/detect-newline-4.0.1.tgz", "integrity": "sha512-qE3Veg1YXzGHQhlA6jzebZN2qVf6NX+A7m7qlhCGG30dJixrAQhYOsJjsnBjJkCSmuOPpCk30145fr8FV0bzog==", "dev": true, "license": "MIT", "engines": { "node": "^12.20.0 || ^14.13.1 || >=16.0.0" }, "funding": { "url": "https://github.com/sponsors/sindresorhus" } }, "node_modules/fdir": { "version": "6.5.0", "resolved": "https://registry.npmjs.org/fdir/-/fdir-6.5.0.tgz", "integrity": "sha512-tIbYtZbucOs0BRGqPJkshJUYdL+SDH7dVM8gjy+ERp3WAUjLEFJE+02kanyHtwjWOnwrKYBiwAmM0p4kLJAnXg==", "dev": true, "license": "MIT", "engines": { "node": ">=12.0.0" }, "peerDependencies": { "picomatch": "^3 || ^4" }, "peerDependenciesMeta": { "picomatch": { "optional": true } } }, "node_modules/git-hooks-list": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/git-hooks-list/-/git-hooks-list-4.1.1.tgz", "integrity": "sha512-cmP497iLq54AZnv4YRAEMnEyQ1eIn4tGKbmswqwmFV4GBnAqE8NLtWxxdXa++AalfgL5EBH4IxTPyquEuGY/jA==", "dev": true, "license": "MIT", "funding": { "url": "https://github.com/fisker/git-hooks-list?sponsor=1" } }, "node_modules/husky": { "version": "9.1.7", "resolved": "https://registry.npmjs.org/husky/-/husky-9.1.7.tgz", "integrity": "sha512-5gs5ytaNjBrh5Ow3zrvdUUY+0VxIuWVL4i9irt6friV+BqdCfmV11CQTWMiBYWHbXhco+J1kHfTOUkePhCDvMA==", "dev": true, "license": "MIT", "bin": { "husky": "bin.js" }, "engines": { "node": ">=18" }, "funding": { "url": "https://github.com/sponsors/typicode" } }, "node_modules/is-plain-obj": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-4.1.0.tgz", "integrity": "sha512-+Pgi+vMuUNkJyExiMBt5IlFoMyKnr5zhJ4Uspz58WOhBF5QoIZkFyNHIbBAtHwzVAgk5RtndVNsDRN61/mmDqg==", "dev": true, "license": "MIT", "engines": { "node": ">=12" }, "funding": { "url": "https://github.com/sponsors/sindresorhus" } }, "node_modules/picomatch": { "version": "4.0.3", "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.3.tgz", "integrity": "sha512-5gTmgEY/sqK6gFXLIsQNH19lWb4ebPDLA4SdLP7dsWkIXHWlG66oPuVvXSGFPppYZz8ZDZq0dYYrbHfBCVUb1Q==", "dev": true, "license": "MIT", "engines": { "node": ">=12" }, "funding": { "url": "https://github.com/sponsors/jonschlinkert" } }, "node_modules/prettier": { "version": "3.6.2", "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.6.2.tgz", "integrity": "sha512-I7AIg5boAr5R0FFtJ6rCfD+LFsWHp81dolrFD8S79U9tb8Az2nGrJncnMSnys+bpQJfRUzqs9hnA81OAA3hCuQ==", "dev": true, "license": "MIT", "bin": { "prettier": "bin/prettier.cjs" }, "engines": { "node": ">=14" }, "funding": { "url": "https://github.com/prettier/prettier?sponsor=1" } }, "node_modules/semver": { "version": "7.7.2", "resolved": "https://registry.npmjs.org/semver/-/semver-7.7.2.tgz", "integrity": "sha512-RF0Fw+rO5AMf9MAyaRXI4AV0Ulj5lMHqVxxdSgiVbixSCXoEmmX/jk0CuJw4+3SqroYO9VoUh+HcuJivvtJemA==", "dev": true, "license": "ISC", "bin": { "semver": "bin/semver.js" }, "engines": { "node": ">=10" } }, "node_modules/sort-object-keys": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/sort-object-keys/-/sort-object-keys-1.1.3.tgz", "integrity": "sha512-855pvK+VkU7PaKYPc+Jjnmt4EzejQHyhhF33q31qG8x7maDzkeFhAAThdCYay11CISO+qAMwjOBP+fPZe0IPyg==", "dev": true, "license": "MIT" }, "node_modules/sort-package-json": { "version": "3.4.0", "resolved": "https://registry.npmjs.org/sort-package-json/-/sort-package-json-3.4.0.tgz", "integrity": "sha512-97oFRRMM2/Js4oEA9LJhjyMlde+2ewpZQf53pgue27UkbEXfHJnDzHlUxQ/DWUkzqmp7DFwJp8D+wi/TYeQhpA==", "dev": true, "license": "MIT", "dependencies": { "detect-indent": "^7.0.1", "detect-newline": "^4.0.1", "git-hooks-list": "^4.0.0", "is-plain-obj": "^4.1.0", "semver": "^7.7.1", "sort-object-keys": "^1.1.3", "tinyglobby": "^0.2.12" }, "bin": { "sort-package-json": "cli.js" }, "engines": { "node": ">=20" } }, "node_modules/tinyglobby": { "version": "0.2.15", "resolved": "https://registry.npmjs.org/tinyglobby/-/tinyglobby-0.2.15.tgz", "integrity": "sha512-j2Zq4NyQYG5XMST4cbs02Ak8iJUdxRM0XI5QyxXuZOzKOINmWurp3smXu3y5wDcJrptwpSjgXHzIQxR0omXljQ==", "dev": true, "license": "MIT", "dependencies": { "fdir": "^6.5.0", "picomatch": "^4.0.3" }, "engines": { "node": ">=12.0.0" }, "funding": { "url": "https://github.com/sponsors/SuperchupuDev" } } } } sort-object-keys-2.1.0/package.json000066400000000000000000000020651513121403200171770ustar00rootroot00000000000000{ "name": "sort-object-keys", "version": "2.0.1", "description": "Sort an object's keys, including an optional key list", "keywords": [ "keys", "object", "sort" ], "homepage": "https://github.com/keithamus/sort-object-keys#readme", "bugs": { "url": "https://github.com/keithamus/sort-object-keys/issues" }, "repository": { "type": "git", "url": "git+ssh://git@github.com/keithamus/sort-object-keys.git" }, "license": "MIT", "author": "Keith Cirkel (http://keithcirkel.co.uk/)", "type": "module", "exports": { ".": { "types": "./index.d.ts", "default": "./index.js" } }, "main": "index.js", "types": "index.d.ts", "files": [ "index.js", "index.d.ts" ], "scripts": { "format": "prettier -w .", "prepare": "husky", "sort": "sort-package-json", "test": "node test.js" }, "prettier": { "semi": false, "singleQuote": true }, "devDependencies": { "husky": "^9.1.7", "prettier": "^3.6.2", "sort-package-json": "^3.4.0" } } sort-object-keys-2.1.0/test.js000066400000000000000000000020241513121403200162210ustar00rootroot00000000000000import assert from 'node:assert' import sortObject from './index.js' assert.equal( JSON.stringify( sortObject({ c: 1, b: 1, d: 1, a: 1, }), ), JSON.stringify({ a: 1, b: 1, c: 1, d: 1, }), ) assert.equal( JSON.stringify( sortObject( { c: 1, b: 1, d: 1, a: 1, }, ['b', 'a', 'd', 'c'], ), ), JSON.stringify({ b: 1, a: 1, d: 1, c: 1, }), ) 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, }), ) assert.deepEqual( Object.keys( sortObject( { b: 1, a: 1, }, ['a', 'b', 'c', 'd'], ), ), ['a', 'b'], )