sort-object-keys2
Advanced tools
Comparing version
55
index.js
"use strict"; | ||
function sortObject(object, sortWith) { | ||
let options = {}; | ||
if (typeof sortWith === 'function') { | ||
options.sort = sortWith; | ||
} | ||
else if (Array.isArray(sortWith)) { | ||
options.keys = sortWith; | ||
} | ||
else { | ||
options = Object.assign(options, sortWith); | ||
} | ||
let keys = (options.keys || []); | ||
if (options.onlyKeys) { | ||
options.useSource = false; | ||
if ((options.keys || []).length == 0) { | ||
throw new ReferenceError(`options.key is empty or not exists.`); | ||
} | ||
} | ||
else { | ||
keys = keys.concat() | ||
.concat(Object.keys(object).sort(options.sort)); | ||
} | ||
keys = array_unique(keys); | ||
if (options.desc) { | ||
keys = keys.reverse(); | ||
} | ||
let ret = keys.reduce(function (total, key) { | ||
if (options.allowNotExists || key in object) { | ||
total[key] = object[key]; | ||
} | ||
return total; | ||
}, {}); | ||
if (options.useSource) { | ||
Object.keys(ret) | ||
.forEach(function (key, index, array) { | ||
delete object[key]; | ||
object[key] = ret[key]; | ||
}); | ||
return object; | ||
} | ||
return ret; | ||
} | ||
function array_unique(array) { | ||
return array.filter(function (el, index, arr) { | ||
return index == arr.indexOf(el); | ||
}); | ||
} | ||
module.exports = sortObject; | ||
/** | ||
* Created by user on 2018/4/3/003. | ||
*/ | ||
const sortObject = require("./core"); | ||
const sortObjectKeys = sortObject; | ||
sortObjectKeys.default = sortObjectKeys.sortObjectKeys = sortObjectKeys; | ||
module.exports = sortObjectKeys; |
{ | ||
"name": "sort-object-keys2", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Sort an object's keys, including an optional key list", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
2948
-27.01%8
-83.67%1
Infinity%