{"version":3,"file":"flatten-object.mjs","names":[],"sources":["../src/flatten-object.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n                       🗲 Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website:                  https://stormsoftware.com\n Repository:               https://github.com/storm-software/stryke\n Documentation:            https://docs.stormsoftware.com/projects/stryke\n Contact:                  https://stormsoftware.com/contact\n\n SPDX-License-Identifier:  Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isPlainObject } from \"@stryke/type-checks\";\nimport type { DeepKey, DeepValue } from \"@stryke/types\";\n\n/**\n * Flattens a nested object into a single level object with dot-separated keys.\n *\n * @example\n * ```typescript\n * const nestedObject = {\n *   a: {\n *     b: {\n *       c: 1\n *     }\n *   },\n *   d: [2, 3]\n * };\n *\n * const flattened = flattenObject(nestedObject);\n * console.log(flattened);\n * // Output:\n * // {\n * //   'a.b.c': 1,\n * //   'd.0': 2,\n * //   'd.1': 3\n * // }\n * ```\n *\n * @param object - The object to flatten.\n * @returns - The flattened object.\n */\nexport function flattenObject<\n  TObject extends Record<string, any> = Record<string, any>,\n  TDeepKeyObject extends {\n    [TKey in DeepKey<TObject>]: DeepValue<TObject, TKey>;\n  } = { [TKey in DeepKey<TObject>]: DeepValue<TObject, TKey> }\n>(object: TObject): TDeepKeyObject {\n  return flattenObjectImpl<TObject, TDeepKeyObject>(object);\n}\n\nfunction flattenObjectImpl<\n  TObject extends Record<string, any> = Record<string, any>,\n  TDeepKeyObject extends {\n    [TKey in DeepKey<TObject>]: DeepValue<TObject, TKey>;\n  } = { [TKey in DeepKey<TObject>]: DeepValue<TObject, TKey> }\n>(object: TObject, prefix = \"\"): TDeepKeyObject {\n  const result = {} as TDeepKeyObject;\n  const keys = Object.keys(object);\n\n  for (const key of keys) {\n    const value = (object as any)[key];\n\n    const prefixedKey = prefix ? `${prefix}.${key}` : key;\n\n    if (isPlainObject(value) && Object.keys(value).length > 0) {\n      Object.assign(\n        result,\n        flattenObjectImpl<typeof value>(value, prefixedKey)\n      );\n    } else if (Array.isArray(value)) {\n      for (const [index, element] of value.entries()) {\n        (result as any)[`${prefixedKey}.${index}` as DeepKey<TObject>] =\n          element;\n      }\n    } else {\n      (result as any)[prefixedKey as DeepKey<TObject>] = value;\n    }\n  }\n\n  return result;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgDA,SAAgB,cAKd,QAAiC;AACjC,QAAO,kBAA2C,OAAO;;AAG3D,SAAS,kBAKP,QAAiB,SAAS,IAAoB;CAC9C,MAAM,SAAS,EAAE;CACjB,MAAM,OAAO,OAAO,KAAK,OAAO;AAEhC,MAAK,MAAM,OAAO,MAAM;EACtB,MAAM,QAAS,OAAe;EAE9B,MAAM,cAAc,SAAS,GAAG,OAAO,GAAG,QAAQ;AAElD,MAAI,cAAc,MAAM,IAAI,OAAO,KAAK,MAAM,CAAC,SAAS,EACtD,QAAO,OACL,QACA,kBAAgC,OAAO,YAAY,CACpD;WACQ,MAAM,QAAQ,MAAM,CAC7B,MAAK,MAAM,CAAC,OAAO,YAAY,MAAM,SAAS,CAC5C,CAAC,OAAe,GAAG,YAAY,GAAG,WAChC;MAGJ,CAAC,OAAe,eAAmC;;AAIvD,QAAO"}