28 lines
911 B
JavaScript
28 lines
911 B
JavaScript
import { isArrayLike } from "../predicate/isArrayLike.mjs";
|
|
//#region src/compat/object/fromPairs.ts
|
|
/**
|
|
* Converts an array of key-value pairs into an object.
|
|
*
|
|
* @template T - The type of the keys in the resulting object. It must extend `PropertyKey`.
|
|
* @template U - The type of the values in the resulting object.
|
|
*
|
|
* @param {Array<[T, U]>} pairs - An array of key-value pairs where each key is a `PropertyKey` and each value is of type `U`.
|
|
* @returns {Record<T, U>} - An object where the keys are of type `T` and the values are of type `U`.
|
|
*
|
|
* @example
|
|
* const pairs = [['a', 1], ['b', 2]];
|
|
* const result = fromPairs(pairs);
|
|
* // result will be: { a: 1, b: 2 }
|
|
*/
|
|
function fromPairs(pairs) {
|
|
if (!isArrayLike(pairs)) return {};
|
|
const result = {};
|
|
for (let i = 0; i < pairs.length; i++) {
|
|
const [key, value] = pairs[i];
|
|
result[key] = value;
|
|
}
|
|
return result;
|
|
}
|
|
//#endregion
|
|
export { fromPairs };
|