remeda
Version:
A utility library for JavaScript and Typescript.
1 lines • 4.36 kB
Source Map (JSON)
{"version":3,"file":"mapToObj.cjs","names":["purry","out: Record<PropertyKey, unknown>"],"sources":["../src/mapToObj.ts"],"sourcesContent":["import { purry } from \"./purry\";\n\n// TODO [>2]: Once we allow fromEntries to run lazily on iterables we need to remove this utility as it's completely replaceable by `fromEntries(map(...))`.\n/**\n * Map each element of an array into an object using a defined mapper that\n * converts each item into an object entry (a tuple of `[<key>, <value>]`).\n *\n * There are several other functions that could be used to build an object from\n * an array:\n * - `fromKeys` - Builds an object from an array of *keys* and a mapper for\n * values.\n * - `indexBy` - Builds an object from an array of *values* and a mapper for\n * keys.\n * - `pullObject` - Builds an object from an array of items with a mapper for\n * values and another mapper for keys.\n * - `fromEntries` - Builds an object from an array of key-value pairs.\n *\n * **Warning**: We strongly advise against using this function unless it is\n * used with a huge input array and your app has stringent memory/gc\n * constraints. We recommend that in most cases you should use `pullObject`,\n * or the composition `fromEntries(map(array, fn))`. This function will be\n * deprecated and **removed** in future versions of the library!\n *\n * @param array - The array to map.\n * @param fn - The mapping function, which should return a tuple of [key, value], similar to Object.fromEntries.\n * @returns The new mapped object.\n * @signature\n * R.mapToObj(array, fn)\n * @example\n * R.mapToObj([1, 2, 3], x => [String(x), x * 2]) // => {1: 2, 2: 4, 3: 6}\n * @dataFirst\n * @category Array\n */\nexport function mapToObj<T, K extends PropertyKey, V>(\n array: ReadonlyArray<T>,\n fn: (value: T, index: number, data: ReadonlyArray<T>) => [K, V],\n): Record<K, V>;\n\n// TODO [>2]: Once we allow fromEntries to run lazily on iterables we need to remove this utility as it's completely replaceable by `fromEntries(map(...))`.\n/**\n * Map each element of an array into an object using a defined mapper that\n * converts each item into an object entry (a tuple of `[<key>, <value>]`).\n *\n * There are several other functions that could be used to build an object from\n * an array:\n * - `fromKeys` - Builds an object from an array of *keys* and a mapper for\n * values.\n * - `indexBy` - Builds an object from an array of *values* and a mapper for\n * keys.\n * - `pullObject` - Builds an object from an array of items with a mapper for\n * values and another mapper for keys.\n * - `fromEntries` - Builds an object from an array of key-value pairs.\n *\n * **Warning**: We strongly advise against using this function unless it is\n * used with a huge input array and your app has stringent memory/gc\n * constraints. We recommend that in most cases you should use `pullObject`,\n * or the composition `fromEntries(map(array, fn))`. This function will be\n * deprecated and **removed** in future versions of the library!\n *\n * @param fn - The mapping function, which should return a tuple of [key, value], similar to Object.fromEntries.\n * @returns The new mapped object.\n * @signature\n * R.mapToObj(fn)(array)\n * @example\n * R.pipe(\n * [1, 2, 3],\n * R.mapToObj(x => [String(x), x * 2])\n * ) // => {1: 2, 2: 4, 3: 6}\n * @dataLast\n * @category Array\n */\nexport function mapToObj<T, K extends PropertyKey, V>(\n fn: (value: T, index: number, data: ReadonlyArray<T>) => [K, V],\n): (array: ReadonlyArray<T>) => Record<K, V>;\n\nexport function mapToObj(...args: ReadonlyArray<unknown>): unknown {\n return purry(mapToObjImplementation, args);\n}\n\nfunction mapToObjImplementation(\n array: ReadonlyArray<unknown>,\n fn: (\n value: unknown,\n index: number,\n data: ReadonlyArray<unknown>,\n ) => [PropertyKey, unknown],\n): Record<PropertyKey, unknown> {\n const out: Record<PropertyKey, unknown> = {};\n\n for (const [index, element] of array.entries()) {\n const [key, value] = fn(element, index, array);\n out[key] = value;\n }\n\n return out;\n}\n"],"mappings":"wCA2EA,SAAgB,EAAS,GAAG,EAAuC,CACjE,OAAOA,EAAAA,EAAM,EAAwB,EAAK,CAG5C,SAAS,EACP,EACA,EAK8B,CAC9B,IAAMC,EAAoC,EAAE,CAE5C,IAAK,GAAM,CAAC,EAAO,KAAY,EAAM,SAAS,CAAE,CAC9C,GAAM,CAAC,EAAK,GAAS,EAAG,EAAS,EAAO,EAAM,CAC9C,EAAI,GAAO,EAGb,OAAO"}