UNPKG

@churchapps/helpers

Version:

Library of helper functions not specific to any one ChurchApps project or framework.

165 lines 6.1 kB
import { UniqueIdHelper } from "./UniqueIdHelper.js"; export class ArrayHelper { static getIds(array, propertyName) { const result = []; for (const item of array) { const id = item[propertyName]?.toString(); if (!UniqueIdHelper.isMissing(id) && result.indexOf(id) === -1) result.push(id); } return result; } static sortBy(array, propertyName, descending = false) { array.sort((a, b) => { const valA = a[propertyName]; const valB = b[propertyName]; if (valA < valB) return descending ? 1 : -1; else return descending ? -1 : 1; }); } static getIndex(array, propertyName, value) { for (let i = 0; i < array.length; i++) { const item = array[i]; if (ArrayHelper.compare(item, propertyName, value)) return i; } return -1; } static getOne(array, propertyName, value) { for (const item of array || []) if (ArrayHelper.compare(item, propertyName, value)) return item; return null; } static getAll(array, propertyName, value) { const result = []; for (const item of array || []) { if (ArrayHelper.compare(item, propertyName, value)) result.push(item); } return result; } static getAllArray(array, propertyName, values) { const result = []; for (const item of array || []) if (values.indexOf(item[propertyName]) > -1) result.push(item); return result; } static compare(item, propertyName, value) { const propChain = propertyName.split("."); if (propChain.length === 1) return item[propertyName] === value; else { let obj = item; for (let i = 0; i < propChain.length - 1; i++) { if (obj && obj[propChain[i]] !== undefined) obj = obj[propChain[i]]; else return false; } return obj[propChain[propChain.length - 1]] === value; } } static getUniqueValues(array, propertyName) { const result = []; for (const item of array) { const val = (propertyName.indexOf(".") === -1) ? item[propertyName] : this.getDeepValue(item, propertyName); if (result.indexOf(val) === -1) result.push(val); } return result; } static getUnique(array) { const result = []; const jsonList = []; for (const item of array) { const json = JSON.stringify(item); if (jsonList.indexOf(json) === -1) { result.push(item); jsonList.push(json); } } return result; } static getAllOperatorArray(array, propertyName, values, operator, dataType = "string") { const result = []; values.forEach(v => { const filtered = this.getAllOperator(array, propertyName, v, operator.replace("notIn", "notEqual").replace("in", "equals").replace("donatedToAny", "equals").replace("donatedTo", "equals").replace("attendedCampus", "equals").replace("attendedAny", "equals").replace("attendedServiceTime", "equals").replace("attendedService", "equals").replace("attendedGroup", "equals"), dataType); filtered.forEach(f => result.push(f)); }); return result; } static getAllOperator(array, propertyName, value, operator, dataType = "string") { const result = []; for (const item of array) { let propVal = item[propertyName] ?? ""; let compVal = value ?? ""; if (dataType === "number") { propVal = parseFloat(propVal); compVal = parseFloat(compVal); } else if (dataType === "string") { propVal = String(propVal).toLowerCase(); compVal = String(compVal).toLowerCase(); } switch (operator) { case "equals": if (propVal === compVal) result.push(item); break; case "startsWith": if (propVal.indexOf(compVal) === 0) result.push(item); break; case "endsWith": if (propVal.indexOf(compVal) === propVal.length - compVal.length) result.push(item); break; case "contains": if (propVal.indexOf(compVal) > -1) result.push(item); break; case "greaterThan": if (propVal > compVal) result.push(item); break; case "greaterThanEqual": if (propVal >= compVal) result.push(item); break; case "lessThan": if (propVal < compVal) result.push(item); break; case "lessThanEqual": if (propVal <= compVal) result.push(item); break; case "notEqual": if (propVal !== compVal) result.push(item); break; } } return result; } static getDeepValue(item, propertyName) { const propertyNames = propertyName.split("."); let result = item; propertyNames.forEach(name => { if (result != null) result = result[name]; }); return result; } static fillArray(contents, length) { const result = []; for (let i = 0; i < length; i++) result.push(contents); return result; } } //# sourceMappingURL=ArrayHelper.js.map