summaryrefslogtreecommitdiff
path: root/node_modules/@sapphire/utilities/dist/lib/getDeepObjectKeys.js.map
blob: 3e70a04fd576276750ab4a6d4722a28f07d6d138 (plain)
1
{"version":3,"sources":["../../src/lib/getDeepObjectKeys.ts"],"names":[],"mappings":";;;;AAAA,SAAS,gCAAgC;AAclC,SAAS,kBAAqB,KAAmB,SAA8C;AACrG,SAAO,CAAC,GAAG,2BAA2B,KAAK,OAAO,CAAC;AACpD;AAFgB;AAIhB,UAAU,2BACT,KACA,EAAE,sBAAsB,SAAS,IAA8B,EAAE,qBAAqB,SAAS,GAC3E;AACpB,MAAI,MAAM,QAAQ,GAAG,GAAG;AACvB,eAAW,CAAC,OAAO,KAAK,KAAK,IAAI,QAAQ,GAAG;AAC3C,aAAO,0BAA0B,OAAO,OAAO,EAAE,oBAAoB,CAAC;AAAA,IACvE;AAAA,EACD,OAAO;AACN,eAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,GAAG,GAAG;AAC/C,aAAO,2BAA2B,OAAO,GAAG,OAAO,EAAE,oBAAoB,CAAC;AAAA,IAC3E;AAAA,EACD;AACD;AAbU;AAeV,UAAU,0BAA0B,OAAgB,OAAe,EAAE,oBAAoB,GAAgD;AACxI,QAAM,gBAAgB,wBAAwB,WAAW,GAAG,UAAU,wBAAwB,WAAW,IAAI,WAAW,IAAI;AAC5H,SAAO,2BAA2B,OAAO,eAAe,EAAE,oBAAoB,CAAC;AAChF;AAHU;AAKV,UAAU,2BAA2B,KAAc,QAAgB,EAAE,oBAAoB,GAAgD;AACxI,MAAI,OAAO,QAAQ,YAAY,QAAQ,MAAM;AAC5C,UAAM;AACN;AAAA,EACD;AAEA,MAAI,MAAM,QAAQ,GAAG,GAAG;AACvB,eAAW,CAAC,OAAO,KAAK,KAAK,IAAI,QAAQ,GAAG;AAC3C,YAAM,wBAAwB,wBAAwB,WAAW,GAAG,UAAU,UAAU,GAAG,UAAU;AAErG,aAAO,2BAA2B,OAAO,uBAAuB,EAAE,oBAAoB,CAAC;AAAA,IACxF;AAAA,EACD,OAAO;AACN,UAAM,gBAAgB,OAAO,QAAQ,GAAG;AACxC,QAAI,yBAAyB,aAAa,KAAK,QAAQ;AACtD,YAAM;AAAA,IACP,OAAO;AACN,iBAAW,CAAC,KAAK,KAAK,KAAK,eAAe;AACzC,eAAO,2BAA2B,OAAO,wBAAwB,WAAW,GAAG,SAAS,QAAQ,GAAG,UAAU,OAAO;AAAA,UACnH;AAAA,QACD,CAAC;AAAA,MACF;AAAA,IACD;AAAA,EACD;AACD;AAxBU","sourcesContent":["import { isNullOrUndefinedOrEmpty } from './isNullOrUndefinedOrEmpty';\nimport type { AnyObject } from './types';\n\n/**\n * Flattens an object to a list of its keys, traversing deeply into nested objects and arrays of objects.\n *\n * @note By default Nested array values are flattened to `arrayKey.${index}.subKey`.\n * This can be changed to `arrayKey[${index}].subKey` by setting `options.arrayKeysIndexStyle` to `'braces-with-dot'`.\n * Or it can also be changed to `arrayKey[${index}]subKey` by setting `options.arrayKeysIndexStyle` to `'braces'`.\n *\n * @param obj The object of which to deeply retrieve its keys\n * @param options The options with which to customize the output of this function\n * @returns An array of strings holding the keys of the object\n */\nexport function getDeepObjectKeys<T>(obj: AnyObject<T>, options?: GetDeepObjectKeysOptions): string[] {\n\treturn [...getDeepObjectKeysGenerator(obj, options)];\n}\n\nfunction* getDeepObjectKeysGenerator<T>(\n\tobj: AnyObject<T>,\n\t{ arrayKeysIndexStyle = 'dotted' }: GetDeepObjectKeysOptions = { arrayKeysIndexStyle: 'dotted' }\n): Generator<string> {\n\tif (Array.isArray(obj)) {\n\t\tfor (const [index, value] of obj.entries()) {\n\t\t\tyield* getDeepArrayKeysRecursive(value, index, { arrayKeysIndexStyle });\n\t\t}\n\t} else {\n\t\tfor (const [key, value] of Object.entries(obj)) {\n\t\t\tyield* getDeepObjectKeysRecursive(value, `${key}`, { arrayKeysIndexStyle });\n\t\t}\n\t}\n}\n\nfunction* getDeepArrayKeysRecursive(value: unknown, index: number, { arrayKeysIndexStyle }: GetDeepObjectKeysOptions): Generator<string> {\n\tconst resolvedIndex = arrayKeysIndexStyle === 'dotted' ? `${index}` : arrayKeysIndexStyle === 'braces' ? `[${index}]` : `[${index}].`;\n\tyield* getDeepObjectKeysRecursive(value, resolvedIndex, { arrayKeysIndexStyle });\n}\n\nfunction* getDeepObjectKeysRecursive(obj: unknown, prefix: string, { arrayKeysIndexStyle }: GetDeepObjectKeysOptions): Generator<string> {\n\tif (typeof obj !== 'object' || obj === null) {\n\t\tyield prefix;\n\t\treturn;\n\t}\n\n\tif (Array.isArray(obj)) {\n\t\tfor (const [index, value] of obj.entries()) {\n\t\t\tconst resolvedPrefixedIndex = arrayKeysIndexStyle === 'dotted' ? `${prefix}.${index}` : `${prefix}[${index}]`;\n\n\t\t\tyield* getDeepObjectKeysRecursive(value, resolvedPrefixedIndex, { arrayKeysIndexStyle });\n\t\t}\n\t} else {\n\t\tconst objectEntries = Object.entries(obj);\n\t\tif (isNullOrUndefinedOrEmpty(objectEntries) && prefix) {\n\t\t\tyield prefix;\n\t\t} else {\n\t\t\tfor (const [key, value] of objectEntries) {\n\t\t\t\tyield* getDeepObjectKeysRecursive(value, arrayKeysIndexStyle === 'braces' ? `${prefix}${key}` : `${prefix}.${key}`, {\n\t\t\t\t\tarrayKeysIndexStyle\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n}\n\n/**\n * The options for {@link getDeepObjectKeys}\n */\nexport interface GetDeepObjectKeysOptions {\n\t/**\n\t * Whether to use `.${index}.` (`'dotted'`), `[${index}].`, (`'braces-with-dot'`) or `[${index}]` (`'braces'`) to separate array keys\n\t * @default 'dotted'\n\t */\n\tarrayKeysIndexStyle?: 'dotted' | 'braces-with-dot' | 'braces';\n}\n"]}