lakutata
Version:
An IoC-based universal application framework.
222 lines (195 loc) • 4.73 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, {
value: "Module"
});
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 O = {
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 h(e, n = {}) {
n = Object.assign({
computed: {},
customOrders: {},
nullRank: 1,
undefinedRank: 1
}, n);
e.sort(j(n));
return e;
}
function j(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 = O.isDefined(u[n[f]]) ? u[n[f]] : s[n[f]] && s[n[f]](u);
d = O.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 (O.isNull(a) && O.isUndefined(d)) {
l = c === "asc" ? 1 : c === "desc" ? -1 : 0;
} else if (O.isUndefined(a) && O.isNull(d)) {
l = c === "asc" ? -1 : c === "desc" ? 1 : 0;
} else if (O.isNull(a) && O.isDefinedValue(d)) {
l = e.nullRank;
} else if (O.isUndefined(a) && O.isDefinedValue(d)) {
l = e.undefinedRank;
} else if (O.isNull(d) && O.isDefinedValue(a)) {
l = -e.nullRank;
} else if (O.isUndefined(d) && O.isDefinedValue(a)) {
l = -e.undefinedRank;
} else {
l = a < d ? -1 : a > d ? 1 : 0;
if (c === "desc") {
l = l * -1;
}
}
if (l === 0 && O.isDefined(n[f + 1])) {
l = t(u, o, f + 1);
}
return l;
};
}
function D(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 h(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] = D(e, n).map((e => e[i]));
}
}));
return h(e, t);
}
}
exports.SortArray = D;