pax_global_header00006660000000000000000000000064135252270260014516gustar00rootroot0000000000000052 comment=a3d55abdfd8bdf82117d657118cb6774f4170e72 sort-keys-4.0.0/000077500000000000000000000000001352522702600134575ustar00rootroot00000000000000sort-keys-4.0.0/.editorconfig000066400000000000000000000002571352522702600161400ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 sort-keys-4.0.0/.gitattributes000066400000000000000000000000231352522702600163450ustar00rootroot00000000000000* text=auto eol=lf sort-keys-4.0.0/.github/000077500000000000000000000000001352522702600150175ustar00rootroot00000000000000sort-keys-4.0.0/.github/funding.yml000066400000000000000000000001631352522702600171740ustar00rootroot00000000000000github: sindresorhus open_collective: sindresorhus tidelift: npm/sort-keys custom: https://sindresorhus.com/donate sort-keys-4.0.0/.github/security.md000066400000000000000000000002631352522702600172110ustar00rootroot00000000000000# Security Policy To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. sort-keys-4.0.0/.gitignore000066400000000000000000000000271352522702600154460ustar00rootroot00000000000000node_modules yarn.lock sort-keys-4.0.0/.npmrc000066400000000000000000000000231352522702600145720ustar00rootroot00000000000000package-lock=false sort-keys-4.0.0/.travis.yml000066400000000000000000000000651352522702600155710ustar00rootroot00000000000000language: node_js node_js: - '12' - '10' - '8' sort-keys-4.0.0/index.d.ts000066400000000000000000000016571352522702600153710ustar00rootroot00000000000000declare namespace sortKeys { interface Options { /** Recursively sort keys, including keys of objects inside arrays. @default false */ readonly deep?: boolean; /** [Compare function.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) */ readonly compare?: (left: string, right: string) => number; } } /** Sort the keys of an object. @returns A new object with sorted keys. @example ``` import sortKeys = require('sort-keys'); sortKeys({c: 0, a: 0, b: 0}); //=> {a: 0, b: 0, c: 0} sortKeys({b: {b: 0, a: 0}, a: 0}, {deep: true}); //=> {a: 0, b: {a: 0, b: 0}} sortKeys({b: [{b: 0, a: 0}], a: 0}, {deep: true}); //=> {a: 0, b: [{a: 0, b: 0}]} sortKeys({c: 0, a: 0, b: 0}, { compare: (a, b) => -a.localeCompare(b) }); //=> {c: 0, b: 0, a: 0} ``` */ declare function sortKeys( object: T, options?: sortKeys.Options ): T; export = sortKeys; sort-keys-4.0.0/index.js000066400000000000000000000023551352522702600151310ustar00rootroot00000000000000'use strict'; const isPlainObject = require('is-plain-obj'); module.exports = (object, options = {}) => { if (!isPlainObject(object)) { throw new TypeError('Expected a plain object'); } const {deep} = options; const seenInput = []; const seenOutput = []; const deepSortArray = array => { const seenIndex = seenInput.indexOf(array); if (seenIndex !== -1) { return seenOutput[seenIndex]; } const result = []; seenInput.push(array); seenOutput.push(result); result.push(...array.map(item => { if (Array.isArray(item)) { return deepSortArray(item); } if (isPlainObject(item)) { return sortKeys(item); } return item; })); return result; }; const sortKeys = object => { const seenIndex = seenInput.indexOf(object); if (seenIndex !== -1) { return seenOutput[seenIndex]; } const result = {}; const keys = Object.keys(object).sort(options.compare); seenInput.push(object); seenOutput.push(result); for (const key of keys) { const value = object[key]; if (deep && Array.isArray(value)) { result[key] = deepSortArray(value); continue; } result[key] = deep && isPlainObject(value) ? sortKeys(value) : value; } return result; }; return sortKeys(object); }; sort-keys-4.0.0/index.test-d.ts000066400000000000000000000007271352522702600163430ustar00rootroot00000000000000import {expectType} from 'tsd'; import sortKeys = require('.'); const options: sortKeys.Options = {}; expectType<{a: 0; b: 0; c: 0}>(sortKeys({c: 0, a: 0, b: 0})); expectType<{a: 0; b: {a: 0; b: 0}}>( sortKeys({b: {b: 0, a: 0}, a: 0}, {deep: true}) ); expectType<{c: 0; b: 0; a: 0}>( sortKeys( {c: 0, a: 0, b: 0}, { compare: (left, right) => { expectType(left); expectType(right); return -left.localeCompare(right); } } ) ); sort-keys-4.0.0/license000066400000000000000000000021251352522702600150240ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. sort-keys-4.0.0/package.json000066400000000000000000000012131352522702600157420ustar00rootroot00000000000000{ "name": "sort-keys", "version": "4.0.0", "description": "Sort the keys of an object", "license": "MIT", "repository": "sindresorhus/sort-keys", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=8" }, "scripts": { "test": "xo && ava && tsd" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "sort", "object", "keys", "key", "stable", "deterministic", "deep", "recursive", "recursively" ], "dependencies": { "is-plain-obj": "^2.0.0" }, "devDependencies": { "ava": "^2.2.0", "tsd": "^0.7.4", "xo": "^0.24.0" } } sort-keys-4.0.0/readme.md000066400000000000000000000027731352522702600152470ustar00rootroot00000000000000# sort-keys [![Build Status](https://travis-ci.org/sindresorhus/sort-keys.svg?branch=master)](https://travis-ci.org/sindresorhus/sort-keys) > Sort the keys of an object Useful to get a deterministically ordered object, as the order of keys can vary between engines. ## Install ``` $ npm install sort-keys ``` ## Usage ```js const sortKeys = require('sort-keys'); sortKeys({c: 0, a: 0, b: 0}); //=> {a: 0, b: 0, c: 0} sortKeys({b: {b: 0, a: 0}, a: 0}, {deep: true}); //=> {a: 0, b: {a: 0, b: 0}} sortKeys({b: [{b: 0, a: 0}], a: 0}, {deep: true}); //=> {a: 0, b: [{a: 0, b: 0}]} sortKeys({c: 0, a: 0, b: 0}, { compare: (a, b) => -a.localeCompare(b) }); //=> {c: 0, b: 0, a: 0} ``` ## API ### sortKeys(object, options?) Returns a new object with sorted keys. #### object Type: `object` #### options Type: `object` ##### deep Type: `boolean`
Default: `false` Recursively sort keys, including keys of objects inside arrays. ##### compare Type: `Function` [Compare function.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) ---
Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.
sort-keys-4.0.0/test.js000066400000000000000000000042361352522702600150010ustar00rootroot00000000000000import test from 'ava'; import sortKeys from '.'; test('sort the keys of an object', t => { t.deepEqual(sortKeys({c: 0, a: 0, b: 0}), {a: 0, b: 0, c: 0}); }); test('custom compare function', t => { const compare = (a, b) => a.localeCompare(b); t.deepEqual(sortKeys({c: 0, a: 0, b: 0}, {compare}), {c: 0, b: 0, a: 0}); }); test('deep option', t => { t.deepEqual(sortKeys({c: {c: 0, a: 0, b: 0}, a: 0, b: 0}, {deep: true}), {a: 0, b: 0, c: {a: 0, b: 0, c: 0}}); t.notThrows(() => { const object = {a: 0}; object.circular = object; sortKeys(object, {deep: true}); }); const object = {z: 0}; object.circular = object; const sortedObject = sortKeys(object, {deep: true}); t.is(sortedObject, sortedObject.circular); t.deepEqual(Object.keys(sortedObject), ['circular', 'z']); const object1 = {b: 0}; const object2 = {d: 0}; const object3 = {a: [{b: 0}]}; const object4 = {a: [{d: 0}]}; object1.a = object2; object2.c = object1; object3.a[0].a = object4.a[0]; object4.a[0].c = object3.a[0]; t.notThrows(() => { sortKeys(object1, {deep: true}); sortKeys(object2, {deep: true}); sortKeys(object3, {deep: true}); sortKeys(object4, {deep: true}); }); const sorted = sortKeys(object1, {deep: true}); const deepSorted = sortKeys(object3, {deep: true}); t.is(sorted, sorted.a.c); t.deepEqual(deepSorted.a[0], deepSorted.a[0].a.c); t.deepEqual(Object.keys(sorted), ['a', 'b']); t.deepEqual(Object.keys(deepSorted.a[0]), ['a', 'b']); t.deepEqual(sortKeys({c: {c: 0, a: 0, b: 0}, a: 0, b: 0, z: [9, 8, 7, 6, 5]}, {deep: true}), {a: 0, b: 0, c: {a: 0, b: 0, c: 0}, z: [9, 8, 7, 6, 5]}); t.deepEqual(Object.keys(sortKeys({a: [{b: 0, a: 0}]}, {deep: true}).a[0]), ['a', 'b']); }); test('deep arrays', t => { const object = { b: 0, a: [ {b: 0, a: 0}, [{b: 0, a: 0}] ] }; object.a.push(object); object.a[1].push(object.a[1]); t.notThrows(() => { sortKeys(object, {deep: true}); }); const sorted = sortKeys(object, {deep: true}); t.is(sorted.a[2], sorted); t.is(sorted.a[1][1], sorted.a[1]); t.deepEqual(Object.keys(sorted), ['a', 'b']); t.deepEqual(Object.keys(sorted.a[0]), ['a', 'b']); t.deepEqual(Object.keys(sorted.a[1][0]), ['a', 'b']); });