@enonic/js-utils
Version:
Enonic XP JavaScript Utils
149 lines (133 loc) • 3.94 kB
JavaScript
// value/isStringLiteral.ts
var isStringLiteral = (value) => typeof value === "string";
// array/isStringArray.ts
function isStringArray(value) {
return Array.isArray(value) && value.every(isStringLiteral);
}
// array/findIndex.ts
function findIndex(array, callbackFn) {
const length = array.length >>> 0;
for (let i = 0; i < length; i++) {
if (callbackFn(array[i], i, array)) {
return i;
}
}
return -1;
}
// array/flatten.ts
function flatten(arr, d = 1) {
return d > 0 ? arr.reduce((acc, val) => 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, fromIndex = 0) {
if (array == null) {
throw new TypeError('"array" is null or not defined');
}
const o = Object(array);
const len = o.length >>> 0;
if (len === 0) {
return false;
}
const n = fromIndex | 0;
let 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 = (value) => typeof value === "object";
// value/isNumber.ts
function isNumber(value) {
return typeof value === "number" && isFinite(value);
}
// value/isStringObject.ts
var isStringObject = (value) => value instanceof String;
// value/isString.ts
var isString = (value) => isStringLiteral(value) || isStringObject(value);
// value/isSymbol.ts
var isSymbol = (value) => typeof value === "symbol";
// value/isPropertyKey.ts
var isPropertyKey = (value) => isString(value) || isNumber(value) || isSymbol(value);
// value/toStr.ts
function toStr(value, replacer, space = 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! ${toStr(obj)}`);
}
if (!isPropertyKey(propKey)) {
throw new Error(`Second parameter to hasOwnProperty must be a PropertyKey (string|number|symbol)! ${toStr(propKey)}`);
}
return obj.hasOwnProperty(propKey);
}
// value/isObject.ts
var isObject = (value) => Object.prototype.toString.call(value).slice(8, -1) === "Object";
// array/sortBy.ts
function compareNumbers(a, b) {
return a - b;
}
function compareStrings(a, b, caseSensitive = 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) {
const localArray = JSON.parse(JSON.stringify(array));
return localArray.sort((a, b) => {
if (!isObject(a) || !isObject(b)) {
throw new TypeError(`sortByProperty: a or b not an object! a:${toStr(a)} b:${toStr(b)}`);
}
if (!hasOwnProperty(a, propertyName)) {
throw new TypeError(`sortByProperty: a doesn't have a property named:'${propertyName}'! a:${toStr(a)}`);
}
if (!hasOwnProperty(b, propertyName)) {
throw new TypeError(`sortByProperty: b doesn't have a property named:'${propertyName}'! b:${toStr(b)}`);
}
const valueA = a[propertyName];
const 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:${propertyName} is neither number nor string! a:${toStr(a)} b:${toStr(b)}`);
});
}
export {
findIndex,
flatten,
forceArray,
includes,
isStringArray,
sortByProperty
};