UNPKG

bootstrap-vue-next

Version:

Seamless integration of Vue 3, Bootstrap 5, and TypeScript for modern, type-safe UI development

105 lines (104 loc) 3.91 kB
//#region src/utils/object.ts /** * You need this because fundamentally isArray can't make the assumption that a readonly array is of type array * Only that `readonly T[]` has array like properties. So it breaks a bit when making things "readonly" */ var isReadOnlyArray = (arr) => Array.isArray(arr); /** * Removes properties from an object, based on the values in an array, and returns the new object. * Equivalent to an object version of TS Omit<> */ var omit = (objToPluck, keysToPluck) => Object.keys(objToPluck).filter((key) => !keysToPluck.map((el) => el.toString()).includes(key)).reduce((result, key) => ({ ...result, [key]: objToPluck[key] }), {}); /** * Picks properties from an object, base on the values in an array, and returns the new object. * Equivalent to an object version of TS Pick<> */ var pick = (objToPluck, keysToPluck) => [...keysToPluck].reduce((memo, prop) => { memo[prop] = objToPluck[prop]; return memo; }, {}); /** * Dynamically get a nested value from an array or * object with a string. * * @example get(person, 'friends[0].name') * * Thanks to * @link https://github.com/rayepps/radash/blob/master/src/object.ts#L214 * * @deprecated The purpose of this function is to allow one to use "dot notation" to pick nested properties from an object. * Usually for props, it would be better to use a function that picks the property you want instead of relying on weak string paths * Using a function allows for better type safety. */ var get = (value, path, defaultValue) => { const segments = path.split(/[.[\]]/g); let current = value; for (const key of segments) { if (current === null) return defaultValue; if (current === void 0) return defaultValue; if (key.trim() === "") continue; current = current[key]; } if (current === void 0) return defaultValue; return current; }; /** * Opposite of get, dynamically set a nested value into * an object using a key path. Does not modify the given * initial object. * * @example * set({}, 'name', 'ra') // => { name: 'ra' } * set({}, 'cards[0].value', 2) // => { cards: [{ value: 2 }] } * * Thanks to * @link https://github.com/rayepps/radash/blob/master/src/object.ts#L214 */ var set = (initial, path, value) => { const clone = (obj) => { const isPrimitive = (value) => value === void 0 || value === null || typeof value !== "object" && typeof value !== "function"; if (isPrimitive(obj)) return obj; if (typeof obj === "function") return obj.bind({}); const newObj = new obj.constructor(); Object.getOwnPropertyNames(obj).forEach((prop) => { newObj[prop] = obj[prop]; }); return newObj; }; const toInt = (value, defaultValue) => { const def = defaultValue === void 0 ? 0 : defaultValue; if (value === null || value === void 0) return def; const result = Number.parseInt(value); return Number.isNaN(result) ? def : result; }; if (!initial) return {}; if (!path || value === void 0) return initial; const segments = path.split(/[.[\]]/g).filter((x) => !!x.trim()); const _set = (node) => { if (segments.length > 1) { const key = segments.shift(); if (segments[0] !== void 0) { const nextIsNum = toInt(segments[0], null) !== null; node[key] = node[key] === void 0 ? nextIsNum ? [] : {} : node[key]; _set(node[key]); } } else if (segments[0] !== void 0) node[segments[0]] = value; }; const cloned = clone(initial); _set(cloned); return cloned; }; var deepEqual = (a, b) => { if (a === b) return true; if (typeof a !== "object" || a === null || typeof b !== "object" || b === null) return false; const keysA = Object.keys(a), keysB = Object.keys(b); if (keysA.length !== keysB.length) return false; for (const key of keysA) if (!keysB.includes(key) || !deepEqual(a[key], b[key])) return false; return true; }; //#endregion export { pick as a, omit as i, get as n, set as o, isReadOnlyArray as r, deepEqual as t }; //# sourceMappingURL=object-CHQkkner.mjs.map