Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sort-keys

Package Overview
Dependencies
Maintainers
1
Versions
12
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 1.0.0 to 1.1.0

license

33

index.js
'use strict';
module.exports = function (obj, compareFn) {
if (typeof obj !== 'object') {
var isObj = require('is-obj');
module.exports = function (obj, opts) {
if (!isObj(obj)) {
throw new TypeError('Expected an object');
}
var ret = {};
opts = opts || {};
Object.keys(obj).sort(compareFn).forEach(function (el) {
ret[el] = obj[el];
});
// DEPRECATED
if (typeof opts === 'function') {
opts = {compare: opts};
}
return ret;
var deep = opts.deep;
var sortKeys = function (x) {
var ret = {};
var keys = Object.keys(x).sort(opts.compare);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var val = x[key];
ret[key] = deep && val !== x && isObj(val) ? sortKeys(val) : val;
}
return ret;
};
return sortKeys(obj);
};
{
"name": "sort-keys",
"version": "1.0.0",
"version": "1.1.0",
"description": "Sort the keys of an object",

@@ -10,3 +10,3 @@ "license": "MIT",

"email": "sindresorhus@gmail.com",
"url": "http://sindresorhus.com"
"url": "sindresorhus.com"
},

@@ -27,4 +27,12 @@ "engines": {

"key",
"sort"
"sort",
"stable",
"deterministic",
"deep",
"recursive",
"recursively"
],
"dependencies": {
"is-obj": "^1.0.0"
},
"devDependencies": {

@@ -31,0 +39,0 @@ "mocha": "*"

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

Useful to get a deterministically ordered object as the order of keys can vary between engines.
Useful to get a deterministically ordered object, as the order of keys can vary between engines.

@@ -11,3 +11,3 @@

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

@@ -25,4 +25,9 @@ ```

sortKeys({c: 0, a: 0, b: 0}, function (a, b) {
return -a.localeCompare(b);
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: function (a, b) {
return -a.localeCompare(b);
}
});

@@ -35,3 +40,3 @@ //=> {c: 0, b: 0, a: 0}

### sortKeys(input, [compare])
### sortKeys(input, [options])

@@ -45,4 +50,12 @@ Returns a new object with sorted keys.

#### compare
#### options
##### deep
Type: `boolean`
Recursively sort keys.
##### compare
Type: `function`

@@ -49,0 +62,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc