mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-24 11:45:26 +03:00
feat(fromPairs): Ensure compatibility with lodash
This commit is contained in:
parent
fd2658e216
commit
452480335c
@ -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
|
||||
|
||||
|
@ -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类型数据。二维数组的每个子数组应该有两个元素,第一个元素作为键,第二个元素作为值。
|
||||
|
||||
### 返回值
|
||||
|
||||
|
@ -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);
|
||||
// });
|
||||
});
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user