bootstrap-vue-next
Version:
Seamless integration of Vue 3, Bootstrap 5, and TypeScript for modern, type-safe UI development
1 lines • 7.57 kB
Source Map (JSON)
{"version":3,"file":"object-CHQkkner.mjs","names":[],"sources":["../src/utils/object.ts"],"sourcesContent":["/**\n * You need this because fundamentally isArray can't make the assumption that a readonly array is of type array\n * Only that `readonly T[]` has array like properties. So it breaks a bit when making things \"readonly\"\n */\nexport const isReadOnlyArray = <T>(arr: unknown): arr is readonly T[] => Array.isArray(arr)\n\n/**\n * Removes properties from an object, based on the values in an array, and returns the new object.\n * Equivalent to an object version of TS Omit<>\n */\nexport const omit = <\n A extends Record<PropertyKey, unknown>,\n const B extends ReadonlyArray<PropertyKey>,\n>(\n objToPluck: Readonly<A>,\n keysToPluck: Readonly<B> | readonly (keyof A)[]\n): Omit<A, B[number]> =>\n Object.keys(objToPluck)\n .filter((key) => !keysToPluck.map((el) => el.toString()).includes(key))\n .reduce((result, key) => ({...result, [key]: objToPluck[key]}), {} as Omit<A, B[number]>)\n\n/**\n * Picks properties from an object, base on the values in an array, and returns the new object.\n * Equivalent to an object version of TS Pick<>\n */\nexport const pick = <\n A extends Record<PropertyKey, unknown>,\n const B extends ReadonlyArray<PropertyKey>,\n>(\n objToPluck: Readonly<A>,\n keysToPluck: Readonly<B> | readonly (keyof A)[]\n): Pick<A, B[number]> =>\n [...keysToPluck].reduce(\n (memo, prop) => {\n memo[prop] = objToPluck[prop]\n return memo\n },\n {} as Record<PropertyKey, unknown>\n ) as Pick<A, B[number]>\n\n/**\n * Dynamically get a nested value from an array or\n * object with a string.\n *\n * @example get(person, 'friends[0].name')\n *\n * Thanks to\n * @link https://github.com/rayepps/radash/blob/master/src/object.ts#L214\n *\n * @deprecated The purpose of this function is to allow one to use \"dot notation\" to pick nested properties from an object.\n * Usually for props, it would be better to use a function that picks the property you want instead of relying on weak string paths\n * Using a function allows for better type safety.\n */\nexport const get = <TDefault = unknown>(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n value: any,\n path: string,\n defaultValue?: Readonly<TDefault>\n): TDefault => {\n const segments = path.split(/[.[\\]]/g)\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let current: any = value\n for (const key of segments) {\n if (current === null) return defaultValue as TDefault\n if (current === undefined) return defaultValue as TDefault\n if (key.trim() === '') continue\n current = current[key]\n }\n if (current === undefined) return defaultValue as TDefault\n return current\n}\n\n/**\n * Opposite of get, dynamically set a nested value into\n * an object using a key path. Does not modify the given\n * initial object.\n *\n * @example\n * set({}, 'name', 'ra') // => { name: 'ra' }\n * set({}, 'cards[0].value', 2) // => { cards: [{ value: 2 }] }\n *\n * Thanks to\n * @link https://github.com/rayepps/radash/blob/master/src/object.ts#L214\n */\nexport const set = <T extends object, K>(initial: T, path: string, value: K): T => {\n const clone = <T>(obj: T): T => {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const isPrimitive = (value: any): boolean =>\n value === undefined ||\n value === null ||\n (typeof value !== 'object' && typeof value !== 'function')\n // Primitive values do not need cloning.\n if (isPrimitive(obj)) {\n return obj\n }\n\n if (typeof obj === 'function') {\n return obj.bind({})\n }\n\n const newObj = new ((obj as object).constructor as {new (): T})()\n\n // Assign the props.\n Object.getOwnPropertyNames(obj).forEach((prop) => {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n ;(newObj as any)[prop] = (obj as any)[prop]\n })\n\n return newObj\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const toInt = <T extends number | null = number>(value: any, defaultValue?: T): number | T => {\n const def = defaultValue === undefined ? 0 : defaultValue\n if (value === null || value === undefined) {\n return def\n }\n const result = Number.parseInt(value)\n return Number.isNaN(result) ? def : result\n }\n\n if (!initial) return {} as T\n if (!path || value === undefined) return initial\n const segments = path.split(/[.[\\]]/g).filter((x) => !!x.trim())\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const _set = (node: any) => {\n if (segments.length > 1) {\n const key = segments.shift() as string\n if (segments[0] !== undefined) {\n const nextIsNum = toInt(segments[0], null) !== null\n node[key] = node[key] === undefined ? (nextIsNum ? [] : {}) : node[key]\n _set(node[key])\n }\n } else {\n if (segments[0] !== undefined) {\n node[segments[0]] = value\n }\n }\n }\n const cloned = clone(initial)\n _set(cloned)\n return cloned\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport const deepEqual = (a: any, b: any): boolean => {\n if (a === b) {\n return true\n }\n\n if (typeof a !== 'object' || a === null || typeof b !== 'object' || b === null) {\n return false\n }\n\n const keysA = Object.keys(a),\n keysB = Object.keys(b)\n\n if (keysA.length !== keysB.length) {\n return false\n }\n\n for (const key of keysA) {\n if (!keysB.includes(key) || !deepEqual(a[key], b[key])) {\n return false\n }\n }\n\n return true\n}\n"],"mappings":";;;;;AAIA,IAAa,mBAAsB,QAAsC,MAAM,QAAQ,IAAI;;;;;AAM3F,IAAa,QAIX,YACA,gBAEA,OAAO,KAAK,WAAW,CACpB,QAAQ,QAAQ,CAAC,YAAY,KAAK,OAAO,GAAG,UAAU,CAAC,CAAC,SAAS,IAAI,CAAC,CACtE,QAAQ,QAAQ,SAAS;CAAC,GAAG;EAAS,MAAM,WAAW;CAAK,GAAG,EAAE,CAAuB;;;;;AAM7F,IAAa,QAIX,YACA,gBAEA,CAAC,GAAG,YAAY,CAAC,QACd,MAAM,SAAS;AACd,MAAK,QAAQ,WAAW;AACxB,QAAO;GAET,EAAE,CACH;;;;;;;;;;;;;;AAeH,IAAa,OAEX,OACA,MACA,iBACa;CACb,MAAM,WAAW,KAAK,MAAM,UAAU;CAEtC,IAAI,UAAe;AACnB,MAAK,MAAM,OAAO,UAAU;AAC1B,MAAI,YAAY,KAAM,QAAO;AAC7B,MAAI,YAAY,KAAA,EAAW,QAAO;AAClC,MAAI,IAAI,MAAM,KAAK,GAAI;AACvB,YAAU,QAAQ;;AAEpB,KAAI,YAAY,KAAA,EAAW,QAAO;AAClC,QAAO;;;;;;;;;;;;;;AAeT,IAAa,OAA4B,SAAY,MAAc,UAAgB;CACjF,MAAM,SA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