mirror of
https://github.com/toss/es-toolkit.git
synced 2025-01-05 23:43:29 +03:00
feat(fromPairs): Add fromPairs (#473)
Co-authored-by: Sojin Park <raon0211@toss.im>
This commit is contained in:
parent
1d4e55d571
commit
fd2658e216
23
benchmarks/performance/fromPairs.bench.ts
Normal file
23
benchmarks/performance/fromPairs.bench.ts
Normal 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);
|
||||
});
|
||||
});
|
@ -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' },
|
||||
],
|
||||
},
|
||||
{
|
||||
|
@ -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' },
|
||||
],
|
||||
},
|
||||
{
|
||||
|
37
docs/reference/compat/object/fromPairs.md
Normal file
37
docs/reference/compat/object/fromPairs.md
Normal 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 isn’t 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 }
|
||||
```
|
38
docs/zh_hans/reference/compat/object/fromPairs.md
Normal file
38
docs/zh_hans/reference/compat/object/fromPairs.md
Normal 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 }
|
||||
```
|
@ -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';
|
||||
|
46
src/compat/object/fromPairs.spec.ts
Normal file
46
src/compat/object/fromPairs.spec.ts
Normal 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);
|
||||
});
|
||||
});
|
27
src/compat/object/fromPairs.ts
Normal file
27
src/compat/object/fromPairs.ts
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user