UNPKG

@enonic/js-utils

Version:
221 lines (220 loc) 7.34 kB
"use strict"; function _instanceof(left, right) { if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) { return !!right[Symbol.hasInstance](left); } else { return left instanceof right; } } function _type_of(obj) { "@swc/helpers - typeof"; return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; } var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = function(target, all) { for(var name in all)__defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = function(to, from, except, desc) { if (from && (typeof from === "undefined" ? "undefined" : _type_of(from)) === "object" || typeof from === "function") { var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined; try { var _loop = function() { var key = _step.value; if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: function() { return from[key]; }, enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); }; for(var _iterator = __getOwnPropNames(from)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop(); } catch (err) { _didIteratorError = true; _iteratorError = err; } finally{ try { if (!_iteratorNormalCompletion && _iterator.return != null) { _iterator.return(); } } finally{ if (_didIteratorError) { throw _iteratorError; } } } } return to; }; var __toCommonJS = function(mod) { return __copyProps(__defProp({}, "__esModule", { value: true }), mod); }; // array/index.ts var array_exports = {}; __export(array_exports, { findIndex: function() { return findIndex; }, flatten: function() { return flatten; }, forceArray: function() { return forceArray; }, includes: function() { return includes; }, isStringArray: function() { return isStringArray; }, sortByProperty: function() { return sortByProperty; } }); module.exports = __toCommonJS(array_exports); // value/isStringLiteral.ts var isStringLiteral = function(value) { return typeof value === "string"; }; // array/isStringArray.ts function isStringArray(value) { return Array.isArray(value) && value.every(isStringLiteral); } // array/findIndex.ts function findIndex(array, callbackFn) { var length = array.length >>> 0; for(var i = 0; i < length; i++){ if (callbackFn(array[i], i, array)) { return i; } } return -1; } // array/flatten.ts function flatten(arr) { var d = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 1; return d > 0 ? arr.reduce(function(acc, val) { return acc.concat(Array.isArray(val) ? flatten(val, d - 1) : val); }, []) : arr.slice(); } // array/forceArray.ts function forceArray(data) { return Array.isArray(data) ? data : [ data ]; } // array/includes.ts function sameValueZero(x, y) { return x === y || typeof x === "number" && typeof y === "number" && isNaN(x) && isNaN(y); } function includes(array, searchElement) { var fromIndex = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : 0; if (array == null) { throw new TypeError('"array" is null or not defined'); } var o = Object(array); var len = o.length >>> 0; if (len === 0) { return false; } var n = fromIndex | 0; var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0); while(k < len){ if (sameValueZero(o[k], searchElement)) { return true; } k++; } return false; } // value/isBasicObject.ts var isBasicObject = function(value) { return (typeof value === "undefined" ? "undefined" : _type_of(value)) === "object"; }; // value/isNumber.ts function isNumber(value) { return typeof value === "number" && isFinite(value); } // value/isStringObject.ts var isStringObject = function(value) { return _instanceof(value, String); }; // value/isString.ts var isString = function(value) { return isStringLiteral(value) || isStringObject(value); }; // value/isSymbol.ts var isSymbol = function(value) { return (typeof value === "undefined" ? "undefined" : _type_of(value)) === "symbol"; }; // value/isPropertyKey.ts var isPropertyKey = function(value) { return isString(value) || isNumber(value) || isSymbol(value); }; // value/toStr.ts function toStr(value, replacer) { var space = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : 4; return JSON.stringify(value, replacer, space); } // object/hasOwnProperty.ts function hasOwnProperty(obj, propKey) { if (!isBasicObject(obj)) { throw new Error("First parameter to hasOwnProperty must be a basic Object! ".concat(toStr(obj))); } if (!isPropertyKey(propKey)) { throw new Error("Second parameter to hasOwnProperty must be a PropertyKey (string|number|symbol)! ".concat(toStr(propKey))); } return obj.hasOwnProperty(propKey); } // value/isObject.ts var isObject = function(value) { return Object.prototype.toString.call(value).slice(8, -1) === "Object"; }; // array/sortBy.ts function compareNumbers(a, b) { return a - b; } function compareStrings(a, b) { var caseSensitive = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : true; if (!caseSensitive) { a = a.toLowerCase(); b = b.toLowerCase(); } if (a < b) { return -1; } if (a > b) { return 1; } return 0; } function sortByProperty(array, propertyName) { var localArray = JSON.parse(JSON.stringify(array)); return localArray.sort(function(a, b) { if (!isObject(a) || !isObject(b)) { throw new TypeError("sortByProperty: a or b not an object! a:".concat(toStr(a), " b:").concat(toStr(b))); } if (!hasOwnProperty(a, propertyName)) { throw new TypeError("sortByProperty: a doesn't have a property named:'".concat(propertyName, "'! a:").concat(toStr(a))); } if (!hasOwnProperty(b, propertyName)) { throw new TypeError("sortByProperty: b doesn't have a property named:'".concat(propertyName, "'! b:").concat(toStr(b))); } var valueA = a[propertyName]; var valueB = b[propertyName]; if (isNumber(valueA) && isNumber(valueB)) { return compareNumbers(valueA, valueB); } if (isString(valueA) && isString(valueB)) { return compareStrings(valueA, valueB); } throw new TypeError("sortByProperty: Value of propertyName:".concat(propertyName, " is neither number nor string! a:").concat(toStr(a), " b:").concat(toStr(b))); }); }