remeda
Version:
A utility library for JavaScript and Typescript.
1 lines • 4.22 kB
Source Map (JSON)
{"version":3,"file":"pullObject.cjs","names":["purry","result: Partial<Record<K, V>>"],"sources":["../src/pullObject.ts"],"sourcesContent":["import type { BoundedPartial } from \"./internal/types/BoundedPartial\";\nimport type { IterableContainer } from \"./internal/types/IterableContainer\";\nimport { purry } from \"./purry\";\n\n/**\n * Creates an object that maps the result of `valueExtractor` with a key\n * resulting from running `keyExtractor` on each item in `data`. Duplicate keys\n * are overwritten, guaranteeing that the extractor functions are run on each\n * item in `data`.\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 values.\n * * `indexBy` - Builds an object from an array of *values* and a mapper for keys.\n * * `fromEntries` - Builds an object from an array of key-value pairs.\n * Refer to the docs for more details.\n *\n * @param data - The items used to pull/extract the keys and values from.\n * @param keyExtractor - Computes the key for item.\n * @param valueExtractor - Computes the value for the item.\n * @signature\n * R.pullObject(data, keyExtractor, valueExtractor);\n * @example\n * R.pullObject(\n * [\n * { name: \"john\", email: \"john@remedajs.com\" },\n * { name: \"jane\", email: \"jane@remedajs.com\" }\n * ],\n * R.prop(\"name\"),\n * R.prop(\"email\"),\n * ); // => { john: \"john@remedajs.com\", jane: \"jane@remedajs.com\" }\n * @dataFirst\n * @category Object\n */\nexport function pullObject<\n T extends IterableContainer,\n K extends PropertyKey,\n V,\n>(\n data: T,\n keyExtractor: (item: T[number], index: number, data: T) => K,\n valueExtractor: (item: T[number], index: number, data: T) => V,\n): BoundedPartial<Record<K, V>>;\n\n/**\n * Creates an object that maps the result of `valueExtractor` with a key\n * resulting from running `keyExtractor` on each item in `data`. Duplicate keys\n * are overwritten, guaranteeing that the extractor functions are run on each\n * item in `data`.\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 values.\n * * `indexBy` - Builds an object from an array of *values* and a mapper for keys.\n * * `fromEntries` - Builds an object from an array of key-value pairs.\n * Refer to the docs for more details.\n *\n * @param keyExtractor - Computes the key for item.\n * @param valueExtractor - Computes the value for the item.\n * @signature\n * R.pullObject(keyExtractor, valueExtractor)(data);\n * @example\n * R.pipe(\n * [\n * { name: \"john\", email: \"john@remedajs.com\" },\n * { name: \"jane\", email: \"jane@remedajs.com\" }\n * ],\n * R.pullObject(R.prop(\"email\"), R.prop(\"name\")),\n * ); // => { john: \"john@remedajs.com\", jane: \"jane@remedajs.com\" }\n * @dataLast\n * @category Object\n */\nexport function pullObject<\n T extends IterableContainer,\n K extends PropertyKey,\n V,\n>(\n keyExtractor: (item: T[number], index: number, data: T) => K,\n valueExtractor: (item: T[number], index: number, data: T) => V,\n): (data: T) => BoundedPartial<Record<K, V>>;\n\nexport function pullObject(...args: ReadonlyArray<unknown>): unknown {\n return purry(pullObjectImplementation, args);\n}\n\nfunction pullObjectImplementation<\n T extends IterableContainer,\n K extends PropertyKey,\n V,\n>(\n data: T,\n keyExtractor: (item: T[number], index: number, data: T) => K,\n valueExtractor: (item: T[number], index: number, data: T) => V,\n): Partial<Record<K, V>> {\n const result: Partial<Record<K, V>> = {};\n\n for (const [index, item] of data.entries()) {\n const key = keyExtractor(item, index, data);\n const value = valueExtractor(item, index, data);\n result[key] = value;\n }\n\n return result;\n}\n"],"mappings":"wCAiFA,SAAgB,EAAW,GAAG,EAAuC,CACnE,OAAOA,EAAAA,EAAM,EAA0B,EAAK,CAG9C,SAAS,EAKP,EACA,EACA,EACuB,CACvB,IAAMC,EAAgC,EAAE,CAExC,IAAK,GAAM,CAAC,EAAO,KAAS,EAAK,SAAS,CAAE,CAC1C,IAAM,EAAM,EAAa,EAAM,EAAO,EAAK,CAE3C,EAAO,GADO,EAAe,EAAM,EAAO,EAAK,CAIjD,OAAO"}