{"version":3,"file":"index.umd.production.min.cjs","sources":["../src/index.ts"],"sourcesContent":["import { typeNarrowed } from 'ts-type-predicates';\nimport { ITSPropertyKey } from 'ts-type/lib/type/base';\n\nexport type IRecordLike<K extends ITSPropertyKey, V extends any> = Record<K, V> | Pick<Map<K, V>, 'get'>;\n\nexport type IKeyOfRecordLike<T extends IRecordLike<any, any>> = T extends Pick<Map<infer K, any>, 'get'>\n\t? K\n\t: T extends Record<infer K, any> ? K : never;\n\nexport type IExtractKeyOfRecordLike<D extends IRecordLike<any, any>, K extends IKeyOfRecordLikeInput<D>> = K extends IKeyOfRecordLike<D>\n\t? K\n\t: IKeyOfRecordLike<D>;\n\nexport type IValueOfRecordLike<T extends IRecordLike<any, any>> = T extends Pick<Map<any, infer V>, 'get'>\n\t? V\n\t: T extends Record<any, infer V> ? V : never;\n\nexport type IValueOfRecordLikeByKey<D extends IRecordLike<any, any>, K extends IKeyOfRecordLikeInput<D>, V extends IValueOfRecordLike<D> = IValueOfRecordLike<D>> = D extends Pick<Map<any, infer V>, 'get'>\n\t? V\n\t: D extends Record<K, any> ? D[K] : V;\n\nexport type IKeyOfRecordLikeInput<D extends IRecordLike<any, any>> = IKeyOfRecordLike<D> | string;\n\nfunction defaultKeyHandler(key: any): string\n{\n\treturn key?.toLowerCase()\n}\n\nfunction defaultGetKeys<K extends string>(record: unknown): Iterable<K>\n{\n\tif (typeNarrowed<Map<any, any>>(record, typeof (record as Map<any, any>).keys === 'function'))\n\t{\n\t\treturn record.keys();\n\t}\n\n\treturn Object.keys(record) as K[]\n}\n\nfunction defaultGetValue<V>(key: any, record: unknown): V\n{\n\tif (typeNarrowed<Map<any, any>>(record, typeof (record as Map<any, any>).get === 'function'))\n\t{\n\t\treturn record.get(key);\n\t}\n\n\treturn record[key]\n}\n\nfunction defaultSetValue<D extends IRecordLike<any, any>, V>(value: V, key: any, record: D): D\n{\n\tif (typeNarrowed<Map<any, any>>(record, typeof (record as Map<any, any>).set === 'function'))\n\t{\n\t\trecord.set(key, value);\n\t}\n\telse\n\t{\n\t\trecord[key] = value\n\t}\n\n\treturn record\n}\n\nfunction defaultGetEntries<K, V>(record: unknown): Iterable<[K, V]>\n{\n\tif (typeNarrowed<Map<any, any>>(record, typeof (record as Map<any, any>).entries === 'function'))\n\t{\n\t\treturn record.entries();\n\t}\n\n\treturn Object.entries(record) as any\n}\n\nfunction defaultExistsKey(key: any, record: unknown): boolean\n{\n\tif (typeNarrowed<Map<any, any>>(record, typeof (record as Map<any, any>).has === 'function'))\n\t{\n\t\treturn record.has(key)\n\t}\n\n\t// @ts-ignore\n\treturn key in record\n}\n\nfunction checkUndefinedRecord(record: unknown, options?: IOptions): record is void\n{\n\tif (typeof record === 'undefined' || record === null)\n\t{\n\t\tif (options?.allowUndefinedRecord)\n\t\t{\n\t\t\treturn true\n\t\t}\n\n\t\tthrow new TypeError(`Invalid record`)\n\t}\n}\n\nexport {\n\tdefaultKeyHandler,\n\tdefaultGetKeys,\n\tdefaultExistsKey,\n\tdefaultGetValue,\n\tdefaultSetValue,\n\tcheckUndefinedRecord,\n\tdefaultGetEntries,\n}\n\nexport interface IOptions\n{\n\thandleKey?(key: string): string;\n\n\tgetKeys?<T extends string>(record: unknown): Iterable<T>,\n\n\tgetValue?<T>(key: any, record: unknown): T,\n\n\tsetValue?<D, V>(value: V, key: any, record: D): D,\n\n\tgetEntries?<K, V>(record: unknown): Iterable<[K, V]>,\n\n\texistsKey?(key: any, record: unknown): boolean,\n\n\treverse?: boolean,\n\n\tallowUndefinedRecord?: boolean,\n}\n\nfunction keysOfRecord<D extends IRecordLike<any, any> = IRecordLike<any, any>, K extends IKeyOfRecordLikeInput<D> = IKeyOfRecordLike<D>>(record: D,\n\toptions?: IOptions,\n): Iterable<K>\n{\n\tif (checkUndefinedRecord(record, options))\n\t{\n\t\treturn;\n\t}\n\n\tconst getKeys = options?.getKeys ?? defaultGetKeys;\n\n\treturn getKeys(record)\n}\n\n/**\n * get first match key of record\n */\nfunction keyFromRecord<D extends IRecordLike<any, any> = IRecordLike<any, any>, K extends IKeyOfRecordLikeInput<D> = IKeyOfRecordLike<D>>(key: K,\n\trecord: D,\n\toptions?: IOptions,\n): IExtractKeyOfRecordLike<D, K>\n{\n\tif (checkUndefinedRecord(record, options))\n\t{\n\t\treturn;\n\t}\n\n\toptions = options ?? {};\n\n\tconst existsKey = options.existsKey ?? defaultExistsKey;\n\n\tif (!existsKey(key, record))\n\t{\n\t\tif (typeof key === 'string')\n\t\t{\n\t\t\tconst getKeys = options.getKeys ?? defaultGetKeys;\n\t\t\tconst keyHandler = options.handleKey ?? defaultKeyHandler;\n\n\t\t\tlet keys = getKeys(record);\n\t\t\tkey = keyHandler(key) as any;\n\n\t\t\tif (options.reverse)\n\t\t\t{\n\t\t\t\t// @ts-ignore\n\t\t\t\tkeys = [...keys].reverse()\n\t\t\t}\n\n\t\t\tfor (const _key of keys)\n\t\t\t{\n\t\t\t\t// @ts-ignore\n\t\t\t\tif (keyHandler(_key) === key)\n\t\t\t\t{\n\t\t\t\t\t// @ts-ignore\n\t\t\t\t\treturn _key\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\telse\n\t{\n\t\t// @ts-ignore\n\t\treturn key\n\t}\n}\n\n/**\n * get value of record with first match key\n */\nfunction valueFromRecord<V = never, D extends IRecordLike<any, any> = IRecordLike<any, any>, K extends IKeyOfRecordLikeInput<D> = IKeyOfRecordLike<D>>(key: K,\n\trecord: D,\n\toptions?: IOptions,\n): [V] extends [never] ? IValueOfRecordLikeByKey<D, K> : V\n{\n\tif (checkUndefinedRecord(record, options))\n\t{\n\t\treturn;\n\t}\n\n\tconst getValue = options?.getValue ?? defaultGetValue;\n\tconst _key = keyFromRecord(key, record, options);\n\n\treturn getValue(_key, record)\n}\n\nfunction setRecordValue<V, D extends IRecordLike<any, any> = IRecordLike<any, any>, K extends IKeyOfRecordLikeInput<D> = IKeyOfRecordLike<D>>(value: V,\n\tkey: K,\n\trecord: D,\n\toptions?: IOptions,\n): D\n{\n\tif (checkUndefinedRecord(record, options))\n\t{\n\t\treturn;\n\t}\n\n\tconst setValue = options?.setValue ?? defaultSetValue;\n\tconst _key = keyFromRecord(key, record, options);\n\n\treturn setValue(value, _key, record)\n}\n\nfunction entriesOfRecord<D extends IRecordLike<any, any> = IRecordLike<any, any>, K extends IKeyOfRecordLikeInput<D> = IKeyOfRecordLike<D>, V extends IValueOfRecordLike<D> = IValueOfRecordLike<D>>(record: D,\n\toptions?: IOptions,\n): Iterable<[K, V]>\n{\n\tif (checkUndefinedRecord(record, options))\n\t{\n\t\treturn;\n\t}\n\n\tconst getEntries = options?.getEntries ?? defaultGetEntries;\n\n\treturn getEntries(record)\n}\n\nfunction toRecord<K extends ITSPropertyKey, V extends any>(record: IRecordLike<K, V>): Record<K, V>\n{\n\tif (typeNarrowed<Map<any, any>>(record, typeof (record as Map<any, any>).entries === 'function'))\n\t{\n\t\trecord = Object.fromEntries(entriesOfRecord(record)) as Record<K, V>\n\t}\n\n\treturn record as any\n}\n\nfunction toRecordMap<K extends ITSPropertyKey, V extends any>(record: IRecordLike<K, V>): Map<K, V>\n{\n\tif (typeNarrowed<Record<any, any>>(record, typeof (record as Map<any, any>).entries !== 'function'))\n\t{\n\t\trecord = new Map(entriesOfRecord(record))\n\t}\n\n\treturn record as any\n}\n\nexport {\n\tkeysOfRecord,\n\tkeyFromRecord,\n\tvalueFromRecord,\n\tsetRecordValue,\n\tentriesOfRecord,\n\ttoRecord,\n\ttoRecordMap,\n}\n\nexport default valueFromRecord\n"],"names":["defaultKeyHandler","key","toLowerCase","defaultGetKeys","record","typeNarrowed","keys","Object","defaultGetValue","get","defaultSetValue","value","set","defaultGetEntries","entries","defaultExistsKey","has","checkUndefinedRecord","options","allowUndefinedRecord","TypeError","keyFromRecord","_options","_options$existsKey","existsKey","_options$getKeys2","_options$handleKey","getKeys","keyHandler","handleKey","reverse","_key","valueFromRecord","_options$getValue","getValue","entriesOfRecord","_options$getEntries","getEntries","keysOfRecord","_options$getKeys","setR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