mirror of
https://git.friendi.ca/friendica/friendica-addons.git
synced 2024-09-04 18:20:10 +00:00
- slim/slim: ^3.1 - symfony/expression-language: ^3.4 - fxp/composer-asset-plugin: ~1.3 - bower-asset/vue: ^2.5 - bower-asset/vue-resource: ^1.5
55 lines
1 KiB
JavaScript
55 lines
1 KiB
JavaScript
'use strict';
|
|
const isPlainObj = require('is-plain-obj');
|
|
|
|
module.exports = (obj, opts) => {
|
|
if (!isPlainObj(obj)) {
|
|
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 seenInput = [];
|
|
const seenOutput = [];
|
|
|
|
const sortKeys = x => {
|
|
const seenIndex = seenInput.indexOf(x);
|
|
|
|
if (seenIndex !== -1) {
|
|
return seenOutput[seenIndex];
|
|
}
|
|
|
|
const ret = {};
|
|
const keys = Object.keys(x).sort(opts.compare);
|
|
|
|
seenInput.push(x);
|
|
seenOutput.push(ret);
|
|
|
|
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;
|
|
}
|
|
|
|
return ret;
|
|
};
|
|
|
|
return sortKeys(obj);
|
|
};
|