@shopware-ag/meteor-component-library
Version:
The meteor component library is a Vue component library developed by Shopware. It is based on the [Meteor Design System](https://shopware.design/).
1 lines • 4.34 kB
Source Map (JSON)
{"version":3,"file":"object-e11d5dd3.mjs","sources":["../src/utils/object.ts"],"sourcesContent":["/**\n * Deep copy an object\n */\nexport function deepCopyObject<O extends object>(copyObject: O): O {\n return JSON.parse(JSON.stringify(copyObject)) as O;\n}\n\nexport function hasOwnProperty(scope: any, prop: string): boolean {\n return Object.prototype.hasOwnProperty.call(scope, prop);\n}\n\ntype GetPath = string | number | (string | number)[];\n\n/**\n * Safely retrieves a value from a nested object using a path.\n */\nexport const get = <T extends object, D = undefined>(\n obj: T | null | undefined,\n path: GetPath,\n defValue?: D,\n): D | undefined | any => {\n // If path is not defined or it has false value\n if (!path) return undefined;\n // Check if path is string or array. Regex : ensure that we do not have '.' and brackets.\n // Regex explained: https://regexr.com/58j0k\n const pathArray = Array.isArray(path) ? path : String(path).match(/([^[.\\]])+/g);\n\n if (!pathArray) return undefined;\n\n // Find value\n const result = pathArray.reduce<any>((prevObj, key) => prevObj && prevObj[key], obj);\n // If found value is undefined return default value; otherwise return the value\n return result === undefined ? defValue : result;\n};\n\n/**\n * Retrieves a value from an object using a property path, with support for fallback paths.\n * When given an array of paths, returns the first non-empty value found.\n *\n * @param object - The source object to extract the value from\n * @param propertyPath - Either a single string path or an array of string paths to try in order\n * @param defaultValue - Value to return if no non-empty value is found\n * @returns The first non-empty value found, or the default value if none found\n */\nexport function getPropertyValue(object: any, propertyPath: string | string[], defaultValue?: any) {\n if (!propertyPath) {\n return object;\n }\n\n if (Array.isArray(propertyPath)) {\n for (const path of propertyPath) {\n const value = get(object, path, \"\");\n if (value !== undefined && value !== null && value !== \"\") {\n return value;\n }\n }\n return defaultValue;\n }\n\n return get(object, propertyPath, defaultValue);\n}\n\n/**\n * Takes two objects and merges them deeply.\n * If a property exists in both objects and is an object itself, it will merge them recursively.\n * If a property exists in the source object but not in the target, it will be added to the target.\n * If a property exists in the target but not in the source, it will remain unchanged.\n * If a property exists in both objects but is not an object, the source value will overwrite the target value.\n *\n * @param target - The target object to merge into\n * @param source - The source object to merge from\n * @return The merged object\n */\nexport function deepMergeObjects<T extends object>(target: T, source: Partial<T>): T {\n for (const key in source) {\n const targetValue = target[key];\n const sourceValue = source[key];\n\n if (\n sourceValue instanceof Object &&\n !Array.isArray(sourceValue) &&\n targetValue instanceof Object &&\n !Array.isArray(targetValue)\n ) {\n target[key] = deepMergeObjects(targetValue as any, sourceValue as any);\n } else {\n target[key] = sourceValue as any;\n }\n }\n return target;\n}\n\nexport default {\n deepCopyObject,\n hasOwnProperty,\n get,\n getPropertyValue,\n};\n"],"names":[],"mappings":"AAgBO,MAAM,MAAM,CACjB,KACA,MACA,aACwB;AAExB,MAAI,CAAC;AAAa,WAAA;AAGZ,QAAA,YAAY,MAAM,QAAQ,IAAI,IAAI,OAAO,OAAO,IAAI,EAAE,MAAM,aAAa;AAE/E,MAAI,CAAC;AAAkB,WAAA;AAGjB,QAAA,SAAS,UAAU,OAAY,CAAC,SAAS,QAAQ,WAAW,QAAQ,GAAG,GAAG,GAAG;AAE5E,SAAA,WAAW,SAAY,WAAW;AAC3C;AAWgB,SAAA,iBAAiB,QAAa,cAAiC,cAAoB;AACjG,MAAI,CAAC,cAAc;AACV,WAAA;AAAA,EACT;AAEI,MAAA,MAAM,QAAQ,YAAY,GAAG;AAC/B,eAAW,QAAQ,cAAc;AAC/B,YAAM,QAAQ,IAAI,QAAQ,MAAM,EAAE;AAClC,UAAI,UAAU,UAAa,UAAU,QAAQ,UAAU,IAAI;AAClD,eAAA;AAAA,MACT;AAAA,IACF;AACO,WAAA;AAAA,EACT;AAEO,SAAA,IAAI,QAAQ,cAAc,YAAY;AAC/C;AAagB,SAAA,iBAAmC,QAAW,QAAuB;AACnF,aAAW,OAAO,QAAQ;AAClB,UAAA,cAAc,OAAO,GAAG;AACxB,UAAA,cAAc,OAAO,GAAG;AAE9B,QACE,uBAAuB,UACvB,CAAC,MAAM,QAAQ,WAAW,KAC1B,uBAAuB,UACvB,CAAC,MAAM,QAAQ,WAAW,GAC1B;AACA,aAAO,GAAG,IAAI,iBAAiB,aAAoB,WAAkB;AAAA,IAAA,OAChE;AACL,aAAO,GAAG,IAAI;AAAA,IAChB;AAAA,EACF;AACO,SAAA;AACT;"}