From 452480335cf2b73948b7136abbe9ca1e3b654915 Mon Sep 17 00:00:00 2001 From: Sojin Park Date: Sun, 8 Sep 2024 00:20:45 +0900 Subject: [PATCH] feat(fromPairs): Ensure compatibility with lodash --- docs/reference/compat/object/fromPairs.md | 6 +-- .../reference/compat/object/fromPairs.md | 6 +-- src/compat/object/fromPairs.spec.ts | 36 +++++++++++++ src/compat/object/fromPairs.ts | 53 ++++++++++++------- 4 files changed, 74 insertions(+), 27 deletions(-) diff --git a/docs/reference/compat/object/fromPairs.md b/docs/reference/compat/object/fromPairs.md index 5967c7cd..4fad8956 100644 --- a/docs/reference/compat/object/fromPairs.md +++ b/docs/reference/compat/object/fromPairs.md @@ -11,14 +11,12 @@ Convert a two-dimensional array or Map into an object. ## Signature ```typescript -function fromPairs( - data: Array<[string | number | symbol, U]> | Map -): { [key in T]: U }; +function fromPairs(data: Array<[T, U]> | Map): { [key in T]: U }; ``` ### Parameters -- `data` (`Array<[string | number | symbol, U]> | Map`): The two-dimensional array or Map to be converted. Each sub-array in the two-dimensional array should have two elements, with the first element as the key and the second as the value. +- `data` (`Array<[T, U]> | Map`): The two-dimensional array or Map to be converted. Each sub-array in the two-dimensional array should have two elements, with the first element as the key and the second as the value. ### Returns diff --git a/docs/zh_hans/reference/compat/object/fromPairs.md b/docs/zh_hans/reference/compat/object/fromPairs.md index 2ca0f345..bf263769 100644 --- a/docs/zh_hans/reference/compat/object/fromPairs.md +++ b/docs/zh_hans/reference/compat/object/fromPairs.md @@ -12,14 +12,12 @@ ## 签名 ```typescript -function fromPairs( - data: Array<[string | number | symbol, U]> | Map -): { [key in T]: U }; +function fromPairs(data: Array<[T, U]> | Map): { [key in T]: U }; ``` ### 参数 -- `data` (`Array<[string | number | symbol, U]> | Map`): 需要转换的二维数组或Map类型数据。二维数组的每个子数组应该有两个元素,第一个元素作为键,第二个元素作为值。 +- `data` (`Array<[T, U]> | Map`): 需要转换的二维数组或Map类型数据。二维数组的每个子数组应该有两个元素,第一个元素作为键,第二个元素作为值。 ### 返回值 diff --git a/src/compat/object/fromPairs.spec.ts b/src/compat/object/fromPairs.spec.ts index 5acee3d1..24c2a7fe 100644 --- a/src/compat/object/fromPairs.spec.ts +++ b/src/compat/object/fromPairs.spec.ts @@ -1,3 +1,4 @@ +import { falsey } from '../_internal/falsey'; import { describe, it, expect } from 'vitest'; import { fromPairs } from './fromPairs'; @@ -43,4 +44,39 @@ describe('fromPairs', () => { const expected = { a: 1, b: 2, c: 3 }; expect(result).toEqual(expected); }); + + it('should accept a two dimensional array', () => { + const array = [ + ['a', 1], + ['b', 2], + ]; + const object = { a: 1, b: 2 }; + const actual = fromPairs(array); + + expect(actual).toEqual(object); + }); + + it('should accept a falsey `array`', () => { + const expected = falsey.map(() => ({})); + + const actual = falsey.map((array, index) => { + try { + // eslint-disable-next-line + // @ts-ignore + return index ? fromPairs(array) : fromPairs(); + } catch (e) {} + }); + + expect(actual).toEqual(expected); + }); + + it('should not support deep paths', () => { + const actual = fromPairs([['a.b', 1]]); + expect(actual).toEqual({ 'a.b': 1 }); + }); + + // it('should support consuming the return value of `_.toPairs`', () => { + // const object = { 'a.b': 1 }; + // expect(fromPairs(toPairs(object))).toEqual(object); + // }); }); diff --git a/src/compat/object/fromPairs.ts b/src/compat/object/fromPairs.ts index 85a3f531..c69c78e9 100644 --- a/src/compat/object/fromPairs.ts +++ b/src/compat/object/fromPairs.ts @@ -1,27 +1,42 @@ +import { isArrayLike } from '../predicate/isArrayLike.ts'; + /** - * Transforms an array of key-value pairs into an object. + * Converts an array of key-value pairs into an object. * - * @param {Array} data - An array of key-value pairs. - * @returns {Record} - An object with keys and values from the input array. + * @param {any[]} pairs - An array of key-value pairs where each key is a `PropertyKey` and each value is of type `U`. + * @returns {Record} - An object where the keys are from the first element and values are from the second element. * * @example * const pairs = [['a', 1], ['b', 2]]; - * - * const object = fromPairs(pairs); - * console.log(object); - * // Output: - * // { - * // a: 1, - * // b: 2 - * // } + * const result = fromPairs(pairs); + * // result will be: { a: 1, b: 2 } */ - -export function fromPairs( - data: Array<[PropertyKey, U]> | Map -): { [key in T]: U } { - const obj = {} as { [key in T]: U }; - for (const [key, value] of data) { - obj[key as T] = value; +export function fromPairs(pairs: any[]): Record; +/** + * 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 {{ [key in 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 } + */ +export function fromPairs(pairs: Array<[T, U]> | Map): { [key in T]: U }; +export function fromPairs(data: Array<[T, U]> | Map): { [key in T]: U } { + if (!isArrayLike(data) && !(data instanceof Map)) { + return {} as { [key in T]: U }; } - return obj; + + const result = {} as { [key in T]: U }; + + for (const [key, value] of data) { + result[key as T] = value; + } + + return result; }