UNPKG

lakutata

Version:

An IoC-based universal application framework.

216 lines (191 loc) 4.64 kB
function e(e) { return typeof e === "object" && e !== null; } function n(n) { return e(n) && typeof n.length === "number"; } function t(e) { if (Array.isArray(e)) { return e; } else if (e === undefined) { return []; } else if (n(e) || e instanceof Set) { return Array.from(e); } else { return [ e ]; } } function i(e) { return !isNaN(parseFloat(e)); } function r(e) { return !isNaN(parseFloat(e)) && isFinite(e); } function s(e) { return e !== null && typeof e === "object" && e.constructor === Object; } function u(e) { return o(e) && typeof e.length === "number"; } function o(e) { return typeof e === "object" && e !== null; } function f(e) { return typeof e !== "undefined"; } function c(e) { return !f(e); } function l(e) { return e === null; } function a(e) { return f(e) && !l(e) && !Number.isNaN(e); } function d(e) { if (typeof e === "function") { return /^class /.test(Function.prototype.toString.call(e)); } else { return false; } } function y(e) { if (e === null) return true; switch (typeof e) { case "string": case "number": case "symbol": case "undefined": case "boolean": return true; default: return false; } } function b(e) { if (e) { const n = f(Promise) && e instanceof Promise; const t = e.then && typeof e.then === "function"; return !!(n || t); } else { return false; } } function p(e) { if (e === null || !f(e)) { return false; } else { return typeof e[Symbol.iterator] === "function" || typeof e[Symbol.asyncIterator] === "function"; } } function m(e) { return typeof e === "string"; } function N(e) { return typeof e === "function"; } function g(e) { return typeof e === "function" && e.constructor.name === "AsyncFunction"; } const h = { isNumber: i, isFiniteNumber: r, isPlainObject: s, isArrayLike: u, isObject: o, isDefined: f, isUndefined: c, isNull: l, isDefinedValue: a, isClass: d, isPrimitive: y, isPromise: b, isIterable: p, isString: m, isFunction: N, isAsyncFunction: g }; function O(e, n = {}) { n = Object.assign({ computed: {}, customOrders: {}, nullRank: 1, undefinedRank: 1 }, n); e.sort(D(n)); return e; } function D(e = {}) { const n = t(e.by); const i = t(e.order); const {customOrders: r, computed: s} = e; return function t(u, o, f = 0) { const c = i[f] || "asc"; if (!(c === "asc" || c === "desc" || r[c])) { return 0; } let l, a, d; if (n.length) { a = h.isDefined(u[n[f]]) ? u[n[f]] : s[n[f]] && s[n[f]](u); d = h.isDefined(o[n[f]]) ? o[n[f]] : s[n[f]] && s[n[f]](o); } else { a = u; d = o; } if (r && r[c]) { l = r[c].indexOf(a) - r[c].indexOf(d); } else if (a === d) { l = 0; } else if (h.isNull(a) && h.isUndefined(d)) { l = c === "asc" ? 1 : c === "desc" ? -1 : 0; } else if (h.isUndefined(a) && h.isNull(d)) { l = c === "asc" ? -1 : c === "desc" ? 1 : 0; } else if (h.isNull(a) && h.isDefinedValue(d)) { l = e.nullRank; } else if (h.isUndefined(a) && h.isDefinedValue(d)) { l = e.undefinedRank; } else if (h.isNull(d) && h.isDefinedValue(a)) { l = -e.nullRank; } else if (h.isUndefined(d) && h.isDefinedValue(a)) { l = -e.undefinedRank; } else { l = a < d ? -1 : a > d ? 1 : 0; if (c === "desc") { l = l * -1; } } if (l === 0 && h.isDefined(n[f + 1])) { l = t(u, o, f + 1); } return l; }; } function j(e, ...n) { if (n.length < 2) { let t = { order: "asc" }; if (n.length) t = Object.assign(t, { by: n[0].by, order: n[0].order, computed: n[0].computed }); return O(e, t); } else { const t = { by: [], order: [], customOrders: {} }; n.forEach((n => { if (n.by) { const i = n.by; t.by.push(i); t.order.push(i); t.customOrders[i] = j(e, n).map((e => e[i])); } })); return O(e, t); } } export { j as SortArray };