UNPKG

typedash

Version:

modern, type-safe collection of utility functions

1 lines 1.39 kB
{"version":3,"sources":["../../src/functions/objectFromEntries/objectFromEntries.ts","../../src/functions/toObject/toObject.ts"],"names":[],"mappings":";AAAO,IAAM,oBAAuC,OAAO;;;ACWpD,SAAS,SAA2B,OAAmC;AAC5E,SAAO,kBAAkB,MAAM,IAAI,CAAC,UAAU,CAAC,OAAO,KAAK,CAAC,CAAC;AAC/D","sourcesContent":["export const objectFromEntries: ObjectFromEntries = Object.fromEntries;\n\n/**\n * Returns a new object from an iterable of key-value pairs.\n * Same as `Object.fromEntries()` but returns a typed object.\n * @param entries An iterable object that contains key-value entries.\n * @returns A new object from the given iterable of key-value pairs.\n * @example\n * ```ts\n * objectFromEntries([\n * ['a', 1],\n * ['b', 2],\n * ['c', 3]\n * ]);\n * // { a: 1, b: 2, c: 3 }\n */\ntype ObjectFromEntries = <K extends PropertyKey, V>(\n entries: Iterable<readonly [K, V]>\n) => Record<K, V>;\n","import { objectFromEntries } from '../objectFromEntries';\n\n/**\n * Converts an array of strings to an object with the same values as keys and values.\n * @param array The array to convert to an object.\n * @returns An object with the same values as keys and values.\n * @example\n * ```ts\n * toObject(['a', 'b']) // { a: 'a', b: 'b' }\n * ```\n */\nexport function toObject<T extends string>(array: readonly T[]): Record<T, T> {\n return objectFromEntries(array.map((value) => [value, value]));\n}\n"]}