koval-ui
Version:
React components collection with minimalistic design. Supports theming, layout, and input validation.
1 lines • 2.06 kB
Source Map (JSON)
{"version":3,"file":"set.cjs","sources":["../../../../src/internal/utils/set.ts"],"sourcesContent":["/**\n * Naive implementation of a lodash set.\n * Mutates provided object.\n */\nexport const set = <TObject>(\n obj: TObject,\n path: string | string[],\n value: string | number\n): TObject => {\n if (Object(obj) !== obj) return obj; // When obj is not an object,\n // If not yet an array, get the keys from the string-path\n if (!Array.isArray(path)) path = path.toString().match(/[^.[\\]]+/g) || [];\n // @ts-expect-error TODO: improve types\n (path as string[]).slice(0, -1).reduce(\n (\n a,\n c,\n i // Iterate all of them except the last one\n ) =>\n // @ts-expect-error TODO: improve types\n Object(a[c]) === a[c] // Does the key exist and is its value an object?\n ? // Yes: then follow that path\n // @ts-expect-error TODO: improve types\n a[c]\n : // No: create the key. Is the next key a potential array-index?\n // @ts-expect-error TODO: improve types\n (a[c] =\n // @ts-expect-error TODO: improve types\n Math.abs(path[i + 1]) >> 0 === +path[i + 1]\n ? [] // Yes: assign a new array object\n : {}), // No: assign a new plain object\n obj\n )[path[path.length - 1]] = value; // Finally, assign the value to the last key\n return obj; // Return the top-level object to allow chaining\n};\n"],"names":["set","obj","path","value","a","c"],"mappings":"6FAIO,MAAMA,EAAM,CACfC,EACAC,EACAC,KAEI,OAAOF,CAAG,IAAMA,IAEf,MAAM,QAAQC,CAAI,IAAGA,EAAOA,EAAK,SAAW,EAAA,MAAM,WAAW,GAAK,CAAC,GAEvEA,EAAkB,MAAM,EAAG,EAAE,EAAE,OAC5B,CACIE,EACAC,EACA,IAGA,OAAOD,EAAEC,CAAC,CAAC,IAAMD,EAAEC,CAAC,EAGdD,EAAEC,CAAC,EAGFD,EAAEC,CAAC,EAEA,KAAK,IAAIH,EAAK,EAAI,CAAC,CAAC,GAAK,IAAM,CAACA,EAAK,EAAI,CAAC,EACpC,CAAA,EACA,CAAA,EACpBD,GACFC,EAAKA,EAAK,OAAS,CAAC,CAAC,EAAIC,GACpBF"}