{"name":"sort-keys","version":"6.0.1","description":"Sort the keys of an object","license":"MIT","repository":{"type":"git","url":"git+https://github.com/sindresorhus/sort-keys.git"},"funding":"https://github.com/sponsors/sindresorhus","author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"https://sindresorhus.com"},"type":"module","exports":{"types":"./index.d.ts","default":"./index.js"},"sideEffects":false,"engines":{"node":">=20"},"scripts":{"test":"xo && ava && tsd"},"keywords":["sort","object","keys","key","stable","deterministic","deep","recursive","recursively","array","sorted","sorting"],"dependencies":{"is-plain-obj":"^4.1.0"},"devDependencies":{"ava":"^6.4.1","tsd":"^0.33.0","xo":"^1.2.2"},"gitHead":"75e588682e9a59853053d3d8537ac9d9d7bcc364","types":"./index.d.ts","_id":"sort-keys@6.0.1","bugs":{"url":"https://github.com/sindresorhus/sort-keys/issues"},"homepage":"https://github.com/sindresorhus/sort-keys#readme","_nodeVersion":"22.22.3","_npmVersion":"11.17.0","dist":{"integrity":"sha512-w7xWRu8U9MneKNna8ptG194jp9PLtbd/Rl6gwrmbK4yUeKbE66a64rHgl0iKTBBDr/hpanx7zMGP1Qo8MRkc/w==","shasum":"496d3f5ff9d42c45ba8ad5b500e9022f7e854e17","tarball":"http://registry-npm.ywwl.com/sort-keys/download/sort-keys-6.0.1.tgz","fileCount":5,"unpackedSize":16592,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCID20DClp7QJdXTCxiKZiKgy4NUnal4UEmpl1Tcew7VOwAiBK8SbRSDzVhHrhYAJF9fkj/4YoEACMEYcwDPgW6l9UDw=="}],"size":5206,"noattachment":false,"key":"/sort-keys/-/sort-keys-6.0.1.tgz"},"_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"directories":{},"maintainers":[{"name":"sindresorhus","email":"sindresorhus@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/sort-keys_6.0.1_1782775666925_0.8290909663578774"},"_hasShrinkwrap":false,"readme":"# sort-keys\n\n> Sort the keys of an object\n\nUseful to get a deterministically ordered object, as the order of keys can vary between engines.\n\n## Install\n\n```sh\nnpm install sort-keys\n```\n\n## Usage\n\n```js\nimport sortKeys from 'sort-keys';\n\nsortKeys({c: 0, a: 0, b: 0});\n//=> {a: 0, b: 0, c: 0}\n\nsortKeys({b: {b: 0, a: 0}, a: 0}, {deep: true});\n//=> {a: 0, b: {a: 0, b: 0}}\n\nsortKeys({b: [{b: 0, a: 0}], a: 0}, {deep: true});\n//=> {a: 0, b: [{a: 0, b: 0}]}\n\nsortKeys({c: 0, a: 0, b: 0}, {\n\tcompare: (a, b) => -a.localeCompare(b)\n});\n//=> {c: 0, b: 0, a: 0}\n\nsortKeys([{b: 0, a: 2}], {deep: true});\n//=> [{a: 2, b: 0}]\n```\n\n## Advanced Usage with context\n\nThe `deep` and `ignoreKeys` options can receive a context object with detailed information about the current key being processed:\n\n```js\n// Ignore private keys only at root level\nsortKeys(data, {\n\tignoreKeys: ({key, depth}) => key.startsWith('_') && depth === 0\n});\n\n// Deep process only up to 3 levels, skip config paths\nsortKeys(data, {\n\tdeep: ({path, depth}) => depth < 3 && !path.includes('config')\n});\n\n// Complex path-based logic\nsortKeys(data, {\n\tdeep: ({path}) => !(path[0] === 'user' && path[1] === 'cache'),\n\tignoreKeys: ({path, key}) => (path.length === 1 && path[0] === 'metadata') || key.startsWith('_')\n});\n```\n\n## API\n\n### sortKeys(object, options?)\n\nReturns a new object with sorted keys.\n\nProperty descriptors are preserved, including accessors (get/set), and getters are not invoked or deep-processed. Circular references are supported and preserved.\n\n#### object\n\nType: `object | Array`\n\nWhen it's an object:\n- Only plain objects are deeply processed.\n- Only enumerable own string keys are considered; symbol and non-enumerable properties are ignored.\n\nWhen it's an array:\n- Array order is unchanged; holes in sparse arrays are preserved.\n- Elements may be deep-processed if `deep` enables it.\n- Extra enumerable properties on arrays are ignored.\n\n#### options\n\nType: `object`\n\n##### compare\n\nType: `Function`\n\n[Compare function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) for sorting keys.\n\nIf omitted, remaining keys are sorted using the platform's default string sort.\n\n```js\nsortKeys(object, {\n\tcompare: (a, b) => b.localeCompare(a) // Reverse alphabetical\n});\n```\n\n##### deep\n\nType: `boolean | Function`\\\nDefault: `false`\n\nRecursively sort keys, including keys of objects inside arrays.\n\nOnly plain objects are sorted; other object types are left as-is. For arrays, deep processing applies to their elements.\n\nWhen a boolean:\n- `true`: Deep process all nested objects and arrays.\n- `false`: Only sort keys at the current level.\n\nWhen a function, it receives a context object and should return `true` to enable deep processing for that specific key-value pair:\n\n```js\nsortKeys(data, {\n\tdeep: ({key, value, path, depth}) => {\n\t\t// Only deep process up to 2 levels\n\t\tif (depth >= 2) {\n\t\t\treturn false;\n\t\t}\n\n\t\t// Skip deep processing of large arrays for performance\n\t\tif (Array.isArray(value) && value.length > 100) {\n\t\t\treturn false;\n\t\t}\n\n\t\t// Skip config objects entirely\n\t\tif (path.includes('config')) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn true;\n\t}\n});\n```\n\n##### ignoreKeys\n\nType: `string[] | Function`\\\nDefault: `[]`\n\nKeys to ignore when sorting. Ignored keys will appear first in their original order, followed by the sorted keys. Remaining keys are sorted by `compare`, or by default string sort if `compare` is not provided.\n\nCan be an array of key names:\n\n```js\nsortKeys({c: 0, _private: 1, a: 0, b: 0}, {ignoreKeys: ['_private']});\n//=> {_private: 1, a: 0, b: 0, c: 0}\n```\n\nOr a function that receives a context object:\n\n```js\nsortKeys(data, {\n\tignoreKeys: ({key, value, path, depth}) => {\n\t\t// Ignore private keys at root level\n\t\tif (key.startsWith('_') && depth === 0) {\n\t\t\treturn true;\n\t\t}\n\n\t\t// Ignore metadata keys in user objects\n\t\tif (path[0] === 'user' && key === 'metadata') {\n\t\t\treturn true;\n\t\t}\n\n\t\t// Ignore empty objects\n\t\tif (typeof value === 'object' && Object.keys(value).length === 0) {\n\t\t\treturn true;\n\t\t}\n\n\t\treturn false;\n\t}\n});\n```\n\n**Note**: `ignoreKeys` only affects the ordering of object keys; it does not control deep processing, and array indices are not sorted or filtered. To prevent deep processing of specific values, use the `deep` function option.\n\n#### Context object\n\nWhen using functions for `deep` or `ignoreKeys`, they receive a context object with:\n\n- **`key`** (`string`): The current key being processed.\n- **`value`** (`any`): The value associated with the current key.\n- **`path`** (`string[]`): The full path to this key as an array of elements (for example, `['user', 'profile', 'name']`). Array indices are stringified (for example, `['items', '0', 'title']`).\n- **`depth`** (`number`): The current nesting depth (0 for root level).\n\n#### Path examples\n\n```js\nconst data = {\n\tuser: {\n\t\tprofile: {\n\t\t\tname: 'John'\n\t\t}\n\t},\n\titems: [\n\t\t{title: 'Item 1'},\n\t\t{title: 'Item 2'}\n\t]\n};\n\n// Paths generated during processing (as arrays):\n// ['user'] (depth: 0)\n// ['user', 'profile'] (depth: 1)\n// ['user', 'profile', 'name'] (depth: 2)\n// ['items'] (depth: 0)\n// ['items', '0'] (depth: 1)\n// ['items', '0', 'title'] (depth: 2)\n// ['items', '1'] (depth: 1)\n// ['items', '1', 'title'] (depth: 2)\n```\n\nNote: `path` is an array of elements with stringified array indices (for example, `['items', '0', 'title']`). If your key names include dots or special characters, this representation remains unambiguous.\n","publish_time":1782775667056,"_cnpm_publish_time":1782775667056,"dist-tags":{"latest":"6.0.1"}}