feat(fromPairs): Ensure compatibility with lodash
Some checks are pending
CI / codecov (push) Waiting to run
Release / release (push) Waiting to run

This commit is contained in:
Sojin Park 2024-09-08 00:20:45 +09:00
parent fd2658e216
commit 452480335c
4 changed files with 74 additions and 27 deletions

View File

@ -11,14 +11,12 @@ Convert a two-dimensional array or Map into an object.
## Signature
```typescript
function fromPairs<T extends string | number | symbol, U>(
data: Array<[string | number | symbol, U]> | Map<string | number | symbol, U>
): { [key in T]: U };
function fromPairs<T extends string | number | symbol, U>(data: Array<[T, U]> | Map<T, U>): { [key in T]: U };
```
### Parameters
- `data` (`Array<[string | number | symbol, U]> | Map<string | number | symbol, U>`): 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<T, U>`): 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

View File

@ -12,14 +12,12 @@
## 签名
```typescript
function fromPairs<T extends string | number | symbol, U>(
data: Array<[string | number | symbol, U]> | Map<string | number | symbol, U>
): { [key in T]: U };
function fromPairs<T extends string | number | symbol, U>(data: Array<[T, U]> | Map<T, U>): { [key in T]: U };
```
### 参数
- `data` (`Array<[string | number | symbol, U]> | Map<string | number | symbol, U>`): 需要转换的二维数组或Map类型数据。二维数组的每个子数组应该有两个元素第一个元素作为键第二个元素作为值。
- `data` (`Array<[T, U]> | Map<T, U>`): 需要转换的二维数组或Map类型数据。二维数组的每个子数组应该有两个元素第一个元素作为键第二个元素作为值。
### 返回值

View File

@ -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);
// });
});

View File

@ -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<T, U>} - 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<string, any>} - 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<T extends PropertyKey, U>(
data: Array<[PropertyKey, U]> | Map<PropertyKey, U>
): { [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<string, any>;
/**
* 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<T extends PropertyKey, U>(pairs: Array<[T, U]> | Map<T, U>): { [key in T]: U };
export function fromPairs<T extends PropertyKey, U>(data: Array<[T, U]> | Map<T, U>): { [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;
}