lakutata
Version:
An IoC-based universal application framework.
66 lines (63 loc) • 1.62 kB
JavaScript
function r(r) {
if (typeof r !== "object" || r === null) {
return false;
}
const t = Object.getPrototypeOf(r);
return (t === null || t === Object.prototype || Object.getPrototypeOf(t) === null) && !(Symbol.toStringTag in r) && !(Symbol.iterator in r);
}
function t(t, e = {}) {
if (!r(t) && !Array.isArray(t)) {
throw new TypeError("Expected a plain object or array");
}
const {deep: n, compare: o} = e;
const s = [];
const c = [];
const i = t => {
const e = s.indexOf(t);
if (e !== -1) {
return c[e];
}
const n = [];
s.push(t);
c.push(n);
n.push(...t.map((t => {
if (Array.isArray(t)) {
return i(t);
}
if (r(t)) {
return u(t);
}
return t;
})));
return n;
};
const u = t => {
const e = s.indexOf(t);
if (e !== -1) {
return c[e];
}
const p = {};
const f = Object.keys(t).sort(o);
s.push(t);
c.push(p);
for (const e of f) {
const o = t[e];
let s;
if (n && Array.isArray(o)) {
s = i(o);
} else {
s = n && r(o) ? u(o) : o;
}
Object.defineProperty(p, e, {
...Object.getOwnPropertyDescriptor(t, e),
value: s
});
}
return p;
};
if (Array.isArray(t)) {
return n ? i(t) : t.slice();
}
return u(t);
}
export { t as SortKeys };