Comparing version 1.1.2 to 2.0.0
37
index.js
'use strict'; | ||
var isPlainObj = require('is-plain-obj'); | ||
const isPlainObj = require('is-plain-obj'); | ||
module.exports = function (obj, opts) { | ||
module.exports = (obj, opts) => { | ||
if (!isPlainObj(obj)) { | ||
@@ -13,11 +13,11 @@ throw new TypeError('Expected a plain object'); | ||
if (typeof opts === 'function') { | ||
opts = {compare: opts}; | ||
throw new TypeError('Specify the compare function as an option instead'); | ||
} | ||
var deep = opts.deep; | ||
var seenInput = []; | ||
var seenOutput = []; | ||
const deep = opts.deep; | ||
const seenInput = []; | ||
const seenOutput = []; | ||
var sortKeys = function (x) { | ||
var seenIndex = seenInput.indexOf(x); | ||
const sortKeys = x => { | ||
const seenIndex = seenInput.indexOf(x); | ||
@@ -28,4 +28,4 @@ if (seenIndex !== -1) { | ||
var ret = {}; | ||
var keys = Object.keys(x).sort(opts.compare); | ||
const ret = {}; | ||
const keys = Object.keys(x).sort(opts.compare); | ||
@@ -35,6 +35,17 @@ seenInput.push(x); | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i]; | ||
var val = x[key]; | ||
for (let i = 0; i < keys.length; i++) { | ||
const key = keys[i]; | ||
const val = x[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; | ||
continue; | ||
} | ||
ret[key] = deep && isPlainObj(val) ? sortKeys(val) : val; | ||
@@ -41,0 +52,0 @@ } |
{ | ||
"name": "sort-keys", | ||
"version": "1.1.2", | ||
"version": "2.0.0", | ||
"description": "Sort the keys of an object", | ||
@@ -13,6 +13,6 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && mocha" | ||
"test": "xo && ava" | ||
}, | ||
@@ -38,5 +38,5 @@ "files": [ | ||
"devDependencies": { | ||
"mocha": "*", | ||
"ava": "*", | ||
"xo": "*" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3838
40