@ibnlanre/builder
Version:
Creates a builder object for defining keys and values.
1 lines • 5.57 kB
Source Map (JSON)
{"version":3,"sources":["../../src/utilities/is-dictionary/index.ts","../../src/utilities/is-function/index.ts","../../src/core/create-branches/index.ts","../../src/core/create-builder/index.ts"],"names":[],"mappings":";;;AASO,SAAS,aAAa,KAAA,EAAiC;AAC5D,EAAA,OAAO,OAAO,UAAU,QAAA,IAAY,KAAA,KAAU,QAAQ,CAAC,KAAA,CAAM,QAAQ,KAAK,CAAA;AAC5E;;;ACLO,SAAS,WAAW,KAAA,EAA4C;AACrE,EAAA,OAAO,OAAO,KAAA,KAAU,UAAA;AAC1B;;;ACOO,SAAS,cAAA,CAGd,QAAA,EAAoB,MAAA,GAAiB,EAAC,EAAwB;AAC9D,EAAA,MAAM,OAAA,GAAU,MAAA,CAAO,OAAA,CAAQ,QAAQ,CAAA;AAEvC,EAAA,MAAM,WAAyC,OAAA,CAAQ,MAAA;AAAA,IACrD,CAAC,GAAA,EAAK,CAAC,GAAA,EAAK,KAAK,CAAA,KAAM;AACrB,MAAA,MAAM,OAAA,GAAU,MAAA,CAAO,MAAA,CAAO,CAAC,GAAG,CAAC,CAAA;AAEnC,MAAA,IAAI,UAAA,CAAW,KAAK,CAAA,EAAG;AACrB,QAAA,OAAO;AAAA,UACL,GAAG,GAAA;AAAA,UACH,CAAC,GAAG,GAAG;AAAA,YACL,MAAM,CAAA,GAAI,IAAA,KAAoB,CAAC,GAAG,OAAA,EAAS,GAAG,IAAI,CAAA;AAAA,YAClD,MAAM,CAAA,GAAI,IAAA,KAAmC,CAAC,GAAG,OAAA,EAAS,GAAG,IAAI;AAAA;AACnE,SACF;AAAA,MACF;AAEA,MAAA,MAAM,IAAA,GAAO;AAAA,QACX,MAAM,CAAA,GAAI,IAAA,KAAoB,CAAC,GAAG,OAAA,EAAS,GAAG,IAAI,CAAA;AAAA,QAClD,MAAM,MAAM;AAAA,OACd;AAEA,MAAA,OAAO;AAAA,QACL,GAAG,GAAA;AAAA,QACH,CAAC,GAAG,GAAG,YAAA,CAAa,KAAK,CAAA,GACrB,MAAA,CAAO,MAAA,CAAO,IAAA,EAAM,cAAA,CAAe,KAAA,EAAO,OAAO,CAAC,CAAA,GAClD;AAAA,OACN;AAAA,IACF,CAAA;AAAA,IACA;AAAC,GACH;AAEA,EAAA,OAAO,QAAA;AACT;;;ACnCO,SAAS,aAAA,CAKd,UACA,OAAA,EAIA;AACA,EAAA,MAAM,EAAE,SAAS,EAAC,EAAG,YAAY,GAAA,EAAI,GAAI,EAAE,GAAG,OAAA,EAAQ;AACtD,EAAA,MAAM,QAAA,GAAW,cAAA,CAAe,QAAA,EAAU,MAAM,CAAA;AAEhD,EAAA,OAAO,MAAA,CAAO,OAAO,QAAA,EAAU;AAAA,IAC7B,QAAQ,IAAA,EAAiB;AACvB,MAAA,IAAI,IAAA,CAAK,MAAA,EAAQ,OAAO,IAAA,CAAK,KAAK,SAAS,CAAA;AAC3C,MAAA,OAAO,MAAA;AAAA,IACT,CAAA;AAAA,IACA,IAAI,IAAA,GAAO;AACT,MAAA,OAAO,QAAA;AAAA,IACT;AAAA,GACD,CAAA;AACH","file":"index.cjs","sourcesContent":["import { Dictionary } from \"@/core/types\";\r\n\r\n/**\r\n * Check if the value is a dictionary.\r\n *\r\n * @param value - The value to check.\r\n *\r\n * @returns A boolean indicating whether the value is a dictionary.\r\n */\r\nexport function isDictionary(value: any): value is Dictionary {\r\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\r\n}\r\n","/**\n * Check if the value is a function.\n *\n * @param value - The value to check.\n * @returns A boolean indicating whether the value is a function.\n */\nexport function isFunction(value: any): value is (...args: any) => any {\n return typeof value === \"function\";\n}\n","import type { Dictionary, KeyBuilder } from \"../types\";\r\n\nimport { isDictionary, isFunction } from \"@/utilities\";\r\n\r\n/**\r\n * Helper function to create branches that represent the nested keys of the provided object.\r\n *\r\n * @template Register The type of the object.\r\n * @template Prefix The type of the prefix array.\r\n *\r\n * @param {Register} register The object to traverse and retrieve the nested keys.\r\n * @param {string[]} [prefix=[]] An optional prefix to prepend to keys array in the builder object.\r\n *\r\n * @returns {Builder<Register, Prefix>} A builder object with callable functions representing the nested keys.\r\n */\r\nexport function createBranches<\r\n Register extends Dictionary,\r\n const Prefix extends string[] = []\r\n>(register: Register, prefix: Prefix = [] as unknown as Prefix) {\r\n const entries = Object.entries(register);\r\n\r\n const branches: KeyBuilder<Register, Prefix> = entries.reduce(\r\n (acc, [key, value]) => {\r\n const newPath = prefix.concat([key]);\r\n\r\n if (isFunction(value)) {\r\n return {\r\n ...acc,\r\n [key]: {\r\n $get: (...args: unknown[]) => [...newPath, ...args],\r\n $use: (...args: Parameters<typeof value>) => [...newPath, ...args],\r\n },\r\n };\r\n }\r\n\r\n const root = {\r\n $get: (...args: unknown[]) => [...newPath, ...args],\r\n $use: () => newPath,\r\n };\r\n\r\n return {\r\n ...acc,\r\n [key]: isDictionary(value)\r\n ? Object.assign(root, createBranches(value, newPath))\r\n : root,\r\n };\r\n },\r\n {} as KeyBuilder<Register, Prefix>\r\n );\r\n\r\n return branches;\r\n}\r\n","import type { Builder, Dictionary } from \"../types\";\r\n\nimport { createBranches } from \"../create-branches\";\r\n\r\n/**\r\n * Returns a builder object that represents the nested keys of the provided object.\r\n *\r\n * @template Register The type of the object.\r\n * @template Prefix The type of the prefix array.\r\n *\r\n * @param {Register} register The object to traverse and retrieve the nested keys.\r\n * @param {string[]} [prefix=[]] An optional prefix to prepend to keys array in the builder object.\r\n * @param {string} [separator=\".\"] An optional separator to join the keys in the builder object.\r\n *\r\n * @returns {Builder<Register, Prefix>} A builder object with callable functions representing the nested keys.\r\n */\r\nexport function createBuilder<\r\n Register extends Dictionary,\r\n const Prefix extends string[] = [],\r\n const Separator extends string = \".\"\r\n>(\r\n register: Register,\r\n options?: {\r\n prefix?: Prefix;\r\n separator?: Separator;\r\n }\r\n) {\r\n const { prefix = [], separator = \".\" } = { ...options };\r\n const branches = createBranches(register, prefix);\r\n\r\n return Object.assign(branches, {\r\n $get(...path: unknown[]) {\r\n if (path.length) return path.join(separator);\r\n return prefix;\r\n },\r\n get $use() {\r\n return register;\r\n },\r\n }) as Builder<Register, Prefix, Separator>;\r\n}\r\n"]}