UNPKG

sdj-esm

Version:
154 lines (153 loc) 5.13 kB
/* Copyright (c) 2023-2024 Will Rudolph <@willrudolph.com> This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */ import { each, has, isArray, isFunction, isObject, isString } from "lodash-es"; import { getRegEx } from "./regex.js"; // Warning - this will throw errors on its own if not encapsulated export function cloneJI(inJson) { return JSON.parse(JSON.stringify(inJson)); } export const hasSdChildren = (item) => (has(item, "sdChildren") && Array.isArray(item["sdChildren"]) && item["sdChildren"].length > 0); export const copyNonObjects = (inputObj) => { const rtnObj = {}; Object.entries(inputObj).forEach(([key, value]) => { if (!Array.isArray(value) && !isObject(value)) { rtnObj[key] = value; } }); return rtnObj; }; export const isInString = (searchItem, inputString) => { let rtnVal = false; if (Array.isArray(searchItem)) { searchItem.forEach((item) => { if (inputString.indexOf(item) > -1) { rtnVal = true; } }); } else if (inputString.indexOf(searchItem) > -1) { rtnVal = true; } return rtnVal; }; export function findCoreIds(sdCores, sdId) { const idxArray = isArray(sdId); let rtnArray = []; each(sdCores, (sdCoreObj) => { if (idxArray && (sdId.indexOf(sdCoreObj.sdId) !== -1)) { rtnArray.push(sdCoreObj); } else if (!idxArray && sdCoreObj.sdId === sdId) { rtnArray.push(sdCoreObj); } }); return rtnArray; } export const uniqWith = (arr, fn) => arr.filter((element, index) => arr.findIndex((step) => fn(element, step)) === index); export const reject = (arr, predicate) => { const complement = function (f) { return function (x) { return !f(x); }; }; return arr.filter(complement(predicate)); }; const cryptoRandom = () => { const randomBuffer = new Uint32Array(1); crypto.getRandomValues(randomBuffer); return randomBuffer[0] / (0xffffffff + 1); }, makeUUID = (length) => { let result = ""; const characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", charactersLength = characters.length; let counter = 0; while (counter < length) { result += characters.charAt(Math.floor(cryptoRandom() * charactersLength)); counter += 1; } return result; }; export class UUID { static GetNew() { const uuidRtn = makeUUID(25), splits = String(uuidRtn).split(""); splits[6] = "-"; splits[12] = "-"; splits[17] = "-"; return splits.join(""); } static GetCompress() { const compactRtn = makeUUID(10), splits = String(compactRtn).split(""); splits[5] = "-"; return splits.join(""); } static GetEmpty(compact = false) { return (compact) ? "00000-0000" : "000000-00000-0000-0000000"; } static Valid(checkVal) { const regEx = getRegEx("uuid"); return isString(checkVal) ? regEx.test(checkVal) : false; } static ValidData(checkVal) { const regEx = getRegEx("uuidData"); return isString(checkVal) ? regEx.test(checkVal) : false; } } export const getIdFromKey = (sdKey, coreArray) => { let rtnVal; for (let i = 0; i < coreArray.length; i += 1) { const testItem = coreArray[i]; if (testItem && testItem.sdKey === sdKey) { rtnVal = testItem.sdId; break; } } return rtnVal; }; export const getFromCoreArray = (idOrKey, coreArray) => { let entId = isString(idOrKey) ? getIdFromKey(idOrKey, coreArray) : idOrKey; return (entId) ? coreArray[entId] : undefined; }; export const getKeyFromId = (sdId, coreArray) => { let rtnVal; for (let i = 0; i < coreArray.length; i += 1) { const testItem = coreArray[i]; if (testItem && testItem.sdId === sdId) { rtnVal = testItem.sdKey; break; } } return rtnVal; }; export const mutableObjUpdate = (mutableRef, newObject) => { for (const item in mutableRef) { delete mutableRef[item]; } for (const newItem in newObject) { if (Object.prototype.hasOwnProperty.call(newObject, newItem)) { mutableRef[newItem] = newObject[newItem]; } } }; export const mutableArrayUpdate = (mutableArray, newArray) => { mutableArray.length = 0; newArray.forEach((item) => mutableArray.push(item)); }; export const validTypeLexName = (checkVal) => { const regEx = getRegEx("typeLexName"); return isString(checkVal) ? regEx.test(checkVal) : false; }; export function deepFreeze(obj) { if (obj) { if ((isObject(obj) || isArray(obj)) && !isFunction(obj)) { if (!Object.isFrozen(obj)) { Object.freeze(obj); Object.values(obj).forEach((value) => deepFreeze(value)); } } } return obj; } //# sourceMappingURL=func.std.js.map