import {getArrayCallbacks} from '../internal/array/callbacks'; import type {PlainObject} from '../models'; // #region Functions /** * Create a _Set_ from an array of items using a callback * * @param array Array to convert * @param callback Callback to get an item's value * @returns _Set_ of values * * @example * ```typescript * toSet( * [{id: 1}, {id: 2}, {id: 3}], * item => item.id, * ); // => Set { 1, 2, 3 } * ``` */ export function toSet unknown>( array: Item[], callback: Callback, ): Set>; /** * Create a _Set_ from an array of items using a key * * @param array Array to convert * @param key Key to use for value * @returns _Set_ of values * * @example * ```typescript * toSet( * [{id: 1}, {id: 2}, {id: 3}], * 'id', * ); // => Set { 1, 2, 3 } * ``` */ export function toSet( array: Item[], key: ItemKey, ): Set; /** * Create a _Set_ from an array of items * * @param array Array to convert * @returns _Set_ of items * * @example * ```typescript * toSet([1, 2, 3]); // => Set { 1, 2, 3 } * ``` */ export function toSet(array: Item[]): Set; export function toSet(array: unknown[], value?: unknown): Set { if (!Array.isArray(array)) { return new Set(); } const callbacks = getArrayCallbacks(undefined, undefined, value); if (callbacks?.value == null) { return new Set(array); } const {length} = array; const set = new Set(); for (let index = 0; index < length; index += 1) { set.add(callbacks.value(array[index], index, array)); } return set; } // #endregion