/* * @nevware21/ts-utils * https://github.com/nevware21/ts-utils * * Copyright (c) 2026 NevWare21 Solutions LLC * Licensed under the MIT license. */ import { isArrayLike } from "../helpers/base"; import { isUnsafePropKey } from "../object/isUnsafePropKey"; import { iterForOf } from "../iterator/forOf"; import { ArrToMapKeySelectorFn, ArrToMapValueSelectorFn } from "../iterator/types"; import { arrForEach } from "./forEach"; /** * Creates an object map from array-like, iterator or iterable values. * * Keys are generated in stable source order via `keySelector`; later duplicate keys overwrite earlier values. * Unsafe keys (`__proto__`, `constructor`, `prototype`) are ignored. * * Like {@link https://nevware21.github.io/ts-utils/typedoc/functions/arrForEach.html | arrForEach} an empty slot of an * array (or a missing index of an array-like) is skipped, so the keySelector isn't called for it. * @since 0.15.0 * @group Array * @example * ```ts * const users = [ * { id: "u1", name: "Ada" }, * { id: "u2", name: "Lin" }, * { id: "u1", name: "Ada Updated" } * ]; * * arrToMap(users, (value) => value.id, (value) => value.name); * // { u1: "Ada Updated", u2: "Lin" } * ``` */ /*#__NO_SIDE_EFFECTS__*/ export function arrToMap(values: ArrayLike | Iterator | Iterable, keySelector: ArrToMapKeySelectorFn): { [key: string]: T }; /*#__NO_SIDE_EFFECTS__*/ export function arrToMap(values: ArrayLike | Iterator | Iterable, keySelector: ArrToMapKeySelectorFn, valueSelector: ArrToMapValueSelectorFn): { [key: string]: V }; /*#__NO_SIDE_EFFECTS__*/ export function arrToMap(values: ArrayLike | Iterator | Iterable, keySelector: ArrToMapKeySelectorFn, valueSelector?: ArrToMapValueSelectorFn): { [key: string]: V } { let result: { [key: string]: V } = {}; function _processValue(value: T, index?: number) { let valueIndex = index || 0; let key = keySelector(value, valueIndex); let keyValue = key + ""; if (!isUnsafePropKey(keyValue)) { result[keyValue] = valueSelector ? valueSelector(value, valueIndex) : (value as any as V); } } if (isArrayLike(values)) { arrForEach(values, _processValue); } else { iterForOf(values as Iterator | Iterable, _processValue); } return result; }