UNPKG

@litewit/helpers

Version:
57 lines (56 loc) 1.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sortByDate = exports.sortById = exports.reduceUniqueIds = exports.equalNum = exports.parseNum = exports.isPromise = exports.toSubstring = void 0; var toSubstring = function (str, length, endsWith) { if (length === void 0) { length = 0; } if (endsWith === void 0) { endsWith = ''; } return str && (str === null || str === void 0 ? void 0 : str.length) >= length ? str.slice(0, length - 1) + endsWith : str; }; exports.toSubstring = toSubstring; var isPromise = function (foo) { return !!(foo === null || foo === void 0 ? void 0 : foo.then) && typeof foo.then === 'function'; }; exports.isPromise = isPromise; var parseNum = function (num) { return parseInt(String(num), 10) || 0; }; exports.parseNum = parseNum; var equalNum = function (num1, num2) { return !!num1 && !!num2 && (0, exports.parseNum)(num1) === (0, exports.parseNum)(num2); }; exports.equalNum = equalNum; function reduceUniqueIds(ids, id) { if (id && !ids.includes(id)) { ids.push(id); } return ids; } exports.reduceUniqueIds = reduceUniqueIds; var sortById = function (id1, id2, desc) { if (desc === void 0) { desc = false; } if (!id1) { return 1; } if (!id2) { return -1; } if (desc) { return id2 !== id1 ? (id2 - id1 > 0 ? 1 : -1) : 0; } else { return id2 !== id1 ? (id1 - id2 > 0 ? 1 : -1) : 0; } }; exports.sortById = sortById; var sortByDate = function (d1, d2, desc) { if (desc === void 0) { desc = false; } if (!d1) { return 1; } if (!d2) { return -1; } if (desc) { return d2 !== d1 ? (new Date(d2).getTime() - new Date(d1).getTime() > 0 ? 1 : -1) : 0; } else { return d2 !== d1 ? (new Date(d1).getTime() - new Date(d2).getTime() > 0 ? 1 : -1) : 0; } }; exports.sortByDate = sortByDate;