{"version":3,"file":"recompose.cjs","names":["DEFAULT_DECOMPOSE_OPTIONS"],"sources":["../src/recompose.ts"],"sourcesContent":["import { merge } from 'ts-deepmerge';\nimport { DEFAULT_DECOMPOSE_OPTIONS } from './constants';\nimport { SEPARATOR } from './constants/strings';\nimport type { DecomposeOptions, Recompose, Ru } from './types.types';\n\nexport function recomposeObjectUrl<T>(\n  shape: string,\n  value: T,\n  sep: string = SEPARATOR,\n) {\n  const obj: Ru = {};\n  if (shape.length <= 0) return obj;\n\n  const keys = shape.split(sep);\n  if (keys.length === 1) {\n    const key = keys.shift();\n    obj[key!] = value;\n  } else {\n    const key = keys.shift();\n    obj[key!] = recomposeObjectUrl(keys.join(sep), value, sep);\n  }\n\n  return obj;\n}\n\n/**\n *  Recompose a flatten object \n *  @example\n * \n *  { 'data.age': 10, 'human.login': 'login' }\n *  will become\n *  {\n      data: {\n        age: 10,\n      },\n      human: {\n        login: 'login',\n      },\n    }\n *  @remark   \n *  The generated typescript type takes too much ressources\n\n *  @todo\n    Add type to the return\n */\ntype Recompose_F = <\n  const T extends Ru,\n  const O extends DecomposeOptions = typeof DEFAULT_DECOMPOSE_OPTIONS,\n>(\n  shape: T,\n  options?: O,\n) => Recompose<T, O>;\ntype _Recompose_F = (shape: any, options?: DecomposeOptions) => any;\ntype _Recompose2_F = <\n  T extends Ru,\n  const O extends DecomposeOptions = typeof DEFAULT_DECOMPOSE_OPTIONS,\n>(\n  shape: T,\n  options?: O,\n) => Recompose<T, O>;\n\nexport type Recomposer = _Recompose2_F & {\n  strict: Recompose_F;\n  low: _Recompose_F;\n};\n\nconst _recompose: _Recompose_F = (shape, options) => {\n  const { sep, start } = {\n    ...DEFAULT_DECOMPOSE_OPTIONS,\n    ...options,\n  };\n  const entries = Object.entries(shape);\n  if (entries.length === 0) return {};\n  const arr: any[] = [];\n  entries.forEach(([key, value]) => {\n    const cleanKey =\n      start && key.startsWith(sep) ? key.slice(sep.length) : key;\n    arr.push(recomposeObjectUrl(cleanKey, value, sep));\n  });\n  return _recompose2(merge(...arr));\n};\n\nconst _recompose2: (shape: any) => any = shape => {\n  const mustReturn =\n    Array.isArray(shape) || typeof shape !== 'object' || shape === null;\n  if (mustReturn) return shape;\n\n  const entries = Object.entries(shape).sort(([a], [b]) =>\n    a.localeCompare(b),\n  );\n\n  const isEmpty = entries.length === 0;\n  if (isEmpty) return {};\n\n  const isArray = entries.every(\n    ([key]) => key.startsWith('[') && key.endsWith(']'),\n  );\n  if (isArray) {\n    const arr: any[] = [];\n    entries.forEach(([key, value]) => {\n      const index = parseInt(key.slice(1, -1), 10);\n      arr[index] = _recompose2(value);\n    });\n    return arr;\n  }\n\n  return entries.reduce((acc, [key, value]) => {\n    acc[key] = _recompose2(value);\n    return acc;\n  }, {} as any);\n};\n\nexport const recompose: Recomposer = (shape, options) =>\n  _recompose(shape, options);\nrecompose.low = _recompose;\nrecompose.strict = _recompose;\n"],"mappings":";;;;;AAKA,SAAgB,mBACd,OACA,OACA,MAAA,KACA;CACA,MAAM,MAAU,CAAC;CACjB,IAAI,MAAM,UAAU,GAAG,OAAO;CAE9B,MAAM,OAAO,MAAM,MAAM,GAAG;CAC5B,IAAI,KAAK,WAAW,GAAG;EACrB,MAAM,MAAM,KAAK,MAAM;EACvB,IAAI,OAAQ;CACd,OAAO;EACL,MAAM,MAAM,KAAK,MAAM;EACvB,IAAI,OAAQ,mBAAmB,KAAK,KAAK,GAAG,GAAG,OAAO,GAAG;CAC3D;CAEA,OAAO;AACT;AA2CA,MAAM,cAA4B,OAAO,YAAY;CACnD,MAAM,EAAE,KAAK,UAAU;EACrB,GAAGA,0BAAAA;EACH,GAAG;CACL;CACA,MAAM,UAAU,OAAO,QAAQ,KAAK;CACpC,IAAI,QAAQ,WAAW,GAAG,OAAO,CAAC;CAClC,MAAM,MAAa,CAAC;CACpB,QAAQ,SAAS,CAAC,KAAK,WAAW;EAChC,MAAM,WACJ,SAAS,IAAI,WAAW,GAAG,IAAI,IAAI,MAAM,IAAI,MAAM,I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