{"version":3,"file":"objects.mjs","sources":["../../../admin/src/utils/objects.ts"],"sourcesContent":["import clone from 'lodash/clone';\nimport toPath from 'lodash/toPath';\n\n/**\n * Deeply get a value from an object via its path.\n */\nexport function getIn(obj: any, key: string | string[], def?: any, pathStartIndex: number = 0) {\n const path = toPath(key);\n while (obj && pathStartIndex < path.length) {\n obj = obj[path[pathStartIndex++]];\n }\n\n // check if path is not in the end\n if (pathStartIndex !== path.length && !obj) {\n return def;\n }\n\n return obj === undefined ? def : obj;\n}\n\n/** @internal is the given object an Object? */\nexport const isObject = (obj: any): obj is object =>\n obj !== null && typeof obj === 'object' && !Array.isArray(obj);\n\n/** @internal is the given object an integer? */\nexport const isInteger = (obj: any): boolean => String(Math.floor(Number(obj))) === obj;\n\n/**\n * Deeply set a value from in object via its path. If the value at `path`\n * has changed, return a shallow copy of obj with `value` set at `path`.\n * If `value` has not changed, return the original `obj`.\n *\n * Existing objects / arrays along `path` are also shallow copied. Sibling\n * objects along path retain the same internal js reference. Since new\n * objects / arrays are only created along `path`, we can test if anything\n * changed in a nested structure by comparing the object's reference in\n * the old and new object, similar to how russian doll cache invalidation\n * works.\n *\n * In earlier versions of this function, which used cloneDeep, there were\n * issues whereby settings a nested value would mutate the parent\n * instead of creating a new object. `clone` avoids that bug making a\n * shallow copy of the objects along the update path\n * so no object is mutated in place.\n *\n * Before changing this function, please read through the following\n * discussions.\n *\n * @see https://github.com/developit/linkstate\n * @see https://github.com/jaredpalmer/formik/pull/123\n */\nexport function setIn(obj: any, path: string, value: any): any {\n const res: any = clone(obj); // this keeps inheritance when obj is a class\n let resVal: any = res;\n let i = 0;\n const pathArray = toPath(path);\n\n for (; i < pathArray.length - 1; i++) {\n const currentPath: string = pathArray[i];\n const currentObj: any = getIn(obj, pathArray.slice(0, i + 1));\n\n if (currentObj && (isObject(currentObj) || Array.isArray(currentObj))) {\n resVal = resVal[currentPath] = clone(currentObj);\n } else {\n const nextPath: string = pathArray[i + 1];\n resVal = resVal[currentPath] = isInteger(nextPath) && Number(nextPath) >= 0 ? [] : {};\n }\n }\n\n // Return original object if new value is the same as current\n if ((i === 0 ? obj : resVal)[pathArray[i]] === value) {\n return obj;\n }\n\n if (value === undefined) {\n delete resVal[pathArray[i]];\n } else {\n resVal[pathArray[i]] = value;\n }\n\n // If the path array has a single element, the loop did not run.\n // Deleting on `resVal` had no effect in this scenario, so we delete on the result instead.\n if (i === 0 && value === undefined) {\n delete res[pathArray[i]];\n }\n\n return res;\n}\n"],"names":["getIn","obj","key","def","pathStartIndex","path","toPath","length","undefined","isObject","Array","isArray","isInteger","String","Math","floor","Number","setIn","value","res","clone","resVal","i","pathArray","currentPath","currentObj","slice","nextPath"],"mappings":";;;AAGA;;IAGO,SAASA,KAAAA,CAAMC,GAAQ,EAAEC,GAAsB,EAAEC,GAAS,EAAEC,cAAAA,GAAyB,CAAC,EAAA;AAC3F,IAAA,MAAMC,OAAOC,MAAOJ,CAAAA,GAAAA,CAAAA;AACpB,IAAA,MAAOD,GAAOG,IAAAA,cAAAA,GAAiBC,IAAKE,CAAAA,MAAM,CAAE;AAC1CN,QAAAA,GAAAA,GAAMA,GAAG,CAACI,IAAI,CAACD,iBAAiB,CAAC;AACnC;;AAGA,IAAA,IAAIA,cAAmBC,KAAAA,IAAAA,CAAKE,MAAM,IAAI,CAACN,GAAK,EAAA;QAC1C,OAAOE,GAAAA;AACT;IAEA,OAAOF,GAAAA,KAAQO,YAAYL,GAAMF,GAAAA,GAAAA;AACnC;AAEA,gDACaQ,MAAAA,QAAAA,GAAW,CAACR,GACvBA,GAAAA,GAAAA,KAAQ,IAAQ,IAAA,OAAOA,QAAQ,QAAY,IAAA,CAACS,KAAMC,CAAAA,OAAO,CAACV,GAAK;AAEjE,iDACaW,MAAAA,SAAAA,GAAY,CAACX,GAAAA,GAAsBY,MAAOC,CAAAA,IAAAA,CAAKC,KAAK,CAACC,MAAOf,CAAAA,GAAAA,CAAAA,CAAAA,CAAAA,KAAWA;AAEpF;;;;;;;;;;;;;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