feat(fromPairs): Add fromPairs (#473)

Co-authored-by: Sojin Park <raon0211@toss.im>
This commit is contained in:
knott 2024-09-07 23:07:27 +08:00 committed by GitHub
parent 1d4e55d571
commit fd2658e216
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 174 additions and 0 deletions

View File

@ -0,0 +1,23 @@
import { bench, describe } from 'vitest';
import { fromPairs as fromPairsToolkit } from 'es-toolkit/compat';
import { fromPairs as fromPairsLodash } from 'lodash';
describe('fromPairs', () => {
const data = [
['a', 1],
['b', 2],
['c', 3],
];
bench('es-toolkit/fromPairs', () => {
fromPairsToolkit(data);
});
bench('lodash/fromPairs', () => {
fromPairsLodash(data);
});
bench('javascript/fromEntries', () => {
Object.fromEntries(data);
});
});

View File

@ -173,6 +173,7 @@ function sidebar(): DefaultTheme.Sidebar {
{ text: 'set (compat)', link: '/reference/compat/object/set' },
{ text: 'has (compat)', link: '/reference/compat/object/has' },
{ text: 'property (compat)', link: '/reference/compat/object/property' },
{ text: 'fromPairs (compat)', link: '/reference/compat/object/fromPairs' },
],
},
{

View File

@ -170,6 +170,7 @@ function sidebar(): DefaultTheme.Sidebar {
{ text: 'set (兼容性)', link: '/zh_hans/reference/compat/object/set' },
{ text: 'has (兼容性)', link: '/zh_hans/reference/compat/object/has' },
{ text: 'property (兼容性)', link: '/zh_hans/reference/compat/object/property' },
{ text: 'fromPairs (兼容性)', link: '/zh_hans/reference/compat/object/fromPairs' },
],
},
{

View File

@ -0,0 +1,37 @@
# fromPairs
::: info
This function is only available in `es-toolkit/compat` for compatibility reasons. It either has alternative native JavaScript APIs or isnt fully optimized yet.
When imported from `es-toolkit/compat`, it behaves exactly like lodash and provides the same functionalities, as detailed [here](../../../compatibility.md).
:::
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 };
```
### 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.
### Returns
(`{ [key in T]: U }`): The converted object with the same keys and values as the input parameters.
## Examples
```typescript
const data = [
['a', 1],
['b', 2],
['c', 3],
];
const result = fromPairs(data);
// result will be { a: 1, b: 2, c: 3 }
```

View File

@ -0,0 +1,38 @@
# fromPairs
::: info
出于兼容性原因,此函数仅在 `es-toolkit/compat` 中提供。它可能具有替代的原生 JavaScript API或者尚未完全优化。
`es-toolkit/compat` 导入时,它的行为与 lodash 完全一致,并提供相同的功能,详情请见 [这里](../../../compatibility.md)。
:::
将一个二维数组转或Map类型数据转换成对象。
## 签名
```typescript
function fromPairs<T extends string | number | symbol, U>(
data: Array<[string | number | symbol, U]> | Map<string | number | symbol, U>
): { [key in T]: U };
```
### 参数
- `data` (`Array<[string | number | symbol, U]> | Map<string | number | symbol, U>`): 需要转换的二维数组或Map类型数据。二维数组的每个子数组应该有两个元素第一个元素作为键第二个元素作为值。
### 返回值
(`{ [key in T]: U }`): 转换后的对象,具有与输入参数相同的键和值。
## 示例
```typescript
const data = [
['a', 1],
['b', 2],
['c', 3],
];
const result = fromPairs(data);
// result 将会是 { a: 1, b: 2, c: 3 }
```

View File

@ -53,6 +53,7 @@ export { mapKeys } from './object/mapKeys.ts';
export { mapValues } from './object/mapValues.ts';
export { merge } from './object/merge.ts';
export { mergeWith } from './object/mergeWith.ts';
export { fromPairs } from './object/fromPairs.ts';
export { isPlainObject } from './predicate/isPlainObject.ts';
export { isArray } from './predicate/isArray.ts';

View File

@ -0,0 +1,46 @@
import { describe, it, expect } from 'vitest';
import { fromPairs } from './fromPairs';
describe('fromPairs', () => {
it('should convert an array of key-value pairs into an object', () => {
const result = fromPairs([
['a', 1],
['b', 2],
['c', 3],
]);
const expected = { a: 1, b: 2, c: 3 };
expect(result).toEqual(expected);
});
it('should handle different types of keys', () => {
const result = fromPairs([
[1, 'one'],
[2, 'two'],
[3, 'three'],
]);
const expected = { 1: 'one', 2: 'two', 3: 'three' };
expect(result).toEqual(expected);
});
it('should handle Symbol type keys', () => {
const sym1 = Symbol('sym1');
const sym2 = Symbol('sym2');
const result = fromPairs([
[sym1, 'value1'],
[sym2, 'value2'],
]);
const expected = { [sym1]: 'value1', [sym2]: 'value2' };
expect(result).toEqual(expected);
});
it('should handle Map objects', () => {
const map = new Map([
['a', 1],
['b', 2],
['c', 3],
]);
const result = fromPairs(map);
const expected = { a: 1, b: 2, c: 3 };
expect(result).toEqual(expected);
});
});

View File

@ -0,0 +1,27 @@
/**
* Transforms 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.
*
* @example
* const pairs = [['a', 1], ['b', 2]];
*
* const object = fromPairs(pairs);
* console.log(object);
* // Output:
* // {
* // 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;
}
return obj;
}