Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

sort-keys

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sort-keys - npm Package Compare versions

Comparing version
2.0.0
to
3.0.0
+43
index.d.ts
declare namespace sortKeys {
interface Options {
/**
Recursively sort keys.
@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({c: 0, a: 0, b: 0}, {
compare: (a, b) => -a.localeCompare(b)
});
//=> {c: 0, b: 0, a: 0}
```
*/
declare function sortKeys<T extends {[key: string]: unknown}>(
object: T,
options?: sortKeys.Options
): T;
export = sortKeys;
+17
-31
'use strict';
const isPlainObj = require('is-plain-obj');
const isPlainObject = require('is-plain-obj');
module.exports = (obj, opts) => {
if (!isPlainObj(obj)) {
module.exports = (object, options = {}) => {
if (!isPlainObject(object)) {
throw new TypeError('Expected a plain object');
}
opts = opts || {};
// DEPRECATED
if (typeof opts === 'function') {
throw new TypeError('Specify the compare function as an option instead');
}
const deep = opts.deep;
const {deep} = options;
const seenInput = [];
const seenOutput = [];
const sortKeys = x => {
const seenIndex = seenInput.indexOf(x);
const sortKeys = object => {
const seenIndex = seenInput.indexOf(object);

@@ -27,30 +20,23 @@ if (seenIndex !== -1) {

const ret = {};
const keys = Object.keys(x).sort(opts.compare);
const result = {};
const keys = Object.keys(object).sort(options.compare);
seenInput.push(x);
seenOutput.push(ret);
seenInput.push(object);
seenOutput.push(result);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const val = x[key];
for (const key of keys) {
const value = object[key];
if (deep && Array.isArray(val)) {
const retArr = [];
for (let j = 0; j < val.length; j++) {
retArr[j] = isPlainObj(val[j]) ? sortKeys(val[j]) : val[j];
}
ret[key] = retArr;
if (deep && Array.isArray(value)) {
result[key] = value.map(arrayValue => isPlainObject(arrayValue) ? sortKeys(arrayValue) : arrayValue);
continue;
}
ret[key] = deep && isPlainObj(val) ? sortKeys(val) : val;
result[key] = deep && isPlainObject(value) ? sortKeys(value) : value;
}
return ret;
return result;
};
return sortKeys(obj);
return sortKeys(object);
};
{
"name": "sort-keys",
"version": "2.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": ">=4"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"sort",
"object",
"keys",
"obj",
"key",
"stable",
"deterministic",
"deep",
"recursive",
"recursively"
],
"dependencies": {
"is-plain-obj": "^1.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
}
"name": "sort-keys",
"version": "3.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": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}

@@ -11,3 +11,3 @@ # sort-keys [![Build Status](https://travis-ci.org/sindresorhus/sort-keys.svg?branch=master)](https://travis-ci.org/sindresorhus/sort-keys)

```
$ npm install --save sort-keys
$ npm install sort-keys
```

@@ -36,15 +36,18 @@

### sortKeys(input, [options])
### sortKeys(object, [options])
Returns a new object with sorted keys.
#### input
#### object
Type: `Object`
Type: `object`
#### options
Type: `object`
##### deep
Type: `boolean`
Type: `boolean`<br>
Default: `false`

@@ -51,0 +54,0 @@ Recursively sort keys.