@bemedev/decompose
Version:
Decompose object and so more
1 lines • 2.43 kB
Source Map (JSON)
{"version":3,"file":"get.cjs","names":["decompose","DEFAULT_OPTIONS"],"sources":["../../src/contexts/get.ts"],"sourcesContent":["import { DEFAULT_OPTIONS } from './constants';\nimport { decompose } from '../decompose';\nimport type { Decompose, DecomposeOptions, Ru } from '../types.types';\nimport type { NotUndefined } from '@bemedev/types/lib/types/commons.types';\n\n// #region type GetByKey_F\nexport type GetByKey_F = <\n const T extends Ru,\n const K extends keyof Decompose<T, typeof DEFAULT_OPTIONS>,\n>(\n obj: T,\n key: Extract<K, string>,\n) => Decompose<T, typeof DEFAULT_OPTIONS>[K];\nexport type GetByKeyDefined_F = <\n const T extends Ru,\n const K extends keyof Decompose<T, typeof DEFAULT_OPTIONS>,\n>(\n obj: T,\n key: Extract<K, string>,\n) => NotUndefined<Decompose<T, typeof DEFAULT_OPTIONS>[K]>;\n\nexport type GetByKeyOption_F = <\n const O extends DecomposeOptions = typeof DEFAULT_OPTIONS,\n>(\n val: O,\n) => <const T extends Ru, const K extends keyof Decompose<T, O>>(\n obj: T,\n key: Extract<K, string>,\n) => Decompose<T, O>[K];\n// #endregion\n\nexport interface GetByKey {\n (obj: any, key: string): any;\n low: (obj: any, key: string) => any;\n typed: GetByKey_F;\n defined: GetByKeyDefined_F;\n options: GetByKeyOption_F;\n}\n\nconst _getByKey: GetByKey['low'] = (obj, key) => {\n const decomposed = decompose.low(obj, DEFAULT_OPTIONS);\n return (decomposed as any)[key];\n};\n\n/**\n * Retrieves a value from an object by a specified key.\n * @param obj The object to retrieve the value from\n * @param key The key to retrieve the value for, can be a nested key (e.g. 'a.b.c')\n * @returns The value associated with the specified key in the object\n *\n * @see {@linkcode Decompose} for more details on object decomposition.\n */\nexport const getByKey: GetByKey = (obj, key) => _getByKey(obj, key);\n\ngetByKey.low = getByKey;\ngetByKey.typed = getByKey;\ngetByKey.defined = getByKey;\n\ngetByKey.options = options => (obj, key) => {\n const decomposed = decompose.low(obj, {\n ...DEFAULT_OPTIONS,\n ...options,\n });\n return decomposed[key];\n};\n"],"mappings":";;;;AAuCA,MAAM,aAA8B,KAAK,QAAQ;AAE/C,QADmBA,kBAAAA,UAAU,IAAI,KAAKC,2BAAAA,gBAAgB,CAC3B;;;;;;;;;;AAW7B,MAAa,YAAsB,KAAK,QAAQ,UAAU,KAAK,IAAI;AAEnE,SAAS,MAAM;AACf,SAAS,QAAQ;AACjB,SAAS,UAAU;AAEnB,SAAS,WAAU,aAAY,KAAK,QAAQ;AAK1C,QAJmBD,kBAAAA,UAAU,IAAI,KAAK;EACpC,GAAGC,2BAAAA;EACH,GAAG;EACJ,CAAC,CACgB"}