Skip to content

Commit

Permalink
feat(zipObject): Add zipObject function (#92)
Browse files Browse the repository at this point in the history
* feat(zipObject): Add `zipObject` function that combines keys and values into an object

* feat(zipObject): Add `zipObject` function test code

* feat(zipObject): Add `zipObject` function bench

* feat(zipObject): Add `zipObject` function docs

* Update docs/ko/reference/array/zipObject.md

---------

Co-authored-by: Sojin Park <raon0211@gmail.com>
  • Loading branch information
hautest and raon0211 authored Jun 29, 2024
1 parent a8d7e03 commit 5377450
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 0 deletions.
13 changes: 13 additions & 0 deletions benchmarks/zipObject.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { bench, describe } from 'vitest';
import { zipObject as zipObjectToolkit } from 'es-toolkit';
import { zipObject as zipObjectLodash } from 'lodash';

describe('zipObject', () => {
bench('es-toolkit/zipObject', () => {
zipObjectToolkit([1, 2, 3, 4], ['a', 'b', 'c', 'd']);
});

bench('lodash/zipObject', () => {
zipObjectLodash([1, 2, 3, 4], ['a', 'b', 'c', 'd']);
});
});
39 changes: 39 additions & 0 deletions docs/ko/reference/array/zipObject.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# zipObject

두 배열을 하나의 객체로 결합해요. 첫 번째 배열은 프로퍼티 이름을 나타내고, 두 번째 배열은 값을 나타내요.

프로퍼티 이름을 나타내는 배열이 값을 나타내는 배열보다 길면, 값들은 `undefined`로 채워져요.

## 인터페이스

```typescript
function zipObject<P extends string | number | symbol, V>(keys: P[], values: V[]): { [K in P]: V };
```

### 파라미터

- `keys` (`P[]`): 속성 이름이 포함된 배열이에요.
- `values` (`V[]`): 속성 이름에 대응되는 값이 포함된 배열이에요.

### 반환 값

(`{ [K in P]: V }`): 주어진 속성 이름과 값으로 구성된 새로운 객체예요.

## 예시

```typescript
const keys = ['a', 'b', 'c'];
const values = [1, 2, 3];
const result = zipObject(keys, values);
// result는 { a: 1, b: 2, c: 3 }가 돼요.

const keys2 = ['a', 'b', 'c'];
const values2 = [1, 2];
const result2 = zipObject(keys2, values2);
// result2는 { a: 1, b: 2, c: undefined }가 돼요.

const keys3 = ['a', 'b'];
const values3 = [1, 2, 3];
const result3 = zipObject(keys3, values3);
// result3는 { a: 1, b: 2 }가 돼요.
```
39 changes: 39 additions & 0 deletions docs/reference/array/zipObject.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# zipObject

Combines two arrays, one of property names and one of corresponding values, into a single object.

This function takes two arrays: one containing property names and another containing corresponding values. It returns a new object where the property names from the first array are keys, and the corresponding elements from the second array are values. If the `keys` array is longer than the `values` array, the remaining keys will have `undefined` as their values.

## Signature

```typescript
function zipObject<P extends string | number | symbol, V>(keys: P[], values: V[]): { [K in P]: V };
```

### Parameters

- `keys` (`P[]`): An array of property names.
- `values` (`V[]`): An array of values corresponding to the property names.

### Returns

(`{ [K in P]: V }`): A new object composed of the given property names and values.

## Examples

```typescript
const keys = ['a', 'b', 'c'];
const values = [1, 2, 3];
const result = zipObject(keys, values);
// result will be { a: 1, b: 2, c: 3 }

const keys2 = ['a', 'b', 'c'];
const values2 = [1, 2];
const result2 = zipObject(keys2, values2);
// result2 will be { a: 1, b: 2, c: undefined }

const keys3 = ['a', 'b'];
const values3 = [1, 2, 3];
const result3 = zipObject(keys3, values3);
// result3 will be { a: 1, b: 2 }
```
1 change: 1 addition & 0 deletions src/array/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ export { xorBy } from './xorBy';
export { xorWith } from './xorWith';
export { zip } from './zip';
export { zipWith } from './zipWith';
export { zipObject } from './zipObject';
12 changes: 12 additions & 0 deletions src/array/zipObject.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { describe, expect, it } from 'vitest';
import { zipObject } from './zipObject';

describe('zipObject', () => {
it('creates an object from two arrays of keys and values', () => {
expect(zipObject(['a', 'b', 'c'], [1, 2, 3])).toEqual({ a: 1, b: 2, c: 3 });

expect(zipObject(['a', 'b', 'c'], [1, 2])).toEqual({ a: 1, b: 2, c: undefined });

expect(zipObject(['a', 'b'], [1, 2, 3])).toEqual({ a: 1, b: 2 });
});
});
37 changes: 37 additions & 0 deletions src/array/zipObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Combines two arrays, one of property names and one of corresponding values, into a single object.
*
* This function takes two arrays: one containing property names and another containing corresponding values.
* It returns a new object where the property names from the first array are keys, and the corresponding elements
* from the second array are values. If the `keys` array is longer than the `values` array, the remaining keys will
* have `undefined` as their values.
*
* @param {P[]} keys - An array of property names.
* @param {V[]} values - An array of values corresponding to the property names.
* @returns {{ [K in P]: V }} A new object composed of the given property names and values.
*
* @example
* const keys = ['a', 'b', 'c'];
* const values = [1, 2, 3];
* const result = zipObject(keys, values);
* // result will be { a: 1, b: 2, c: 3 }
*
* const keys2 = ['a', 'b', 'c'];
* const values2 = [1, 2];
* const result2 = zipObject(keys2, values2);
* // result2 will be { a: 1, b: 2, c: undefined }
*
* const keys2 = ['a', 'b'];
* const values2 = [1, 2, 3];
* const result2 = zipObject(keys2, values2);
* // result2 will be { a: 1, b: 2 }
*/
export function zipObject<P extends string | number | symbol, V>(keys: P[], values: V[]): { [K in P]: V } {
const result = {} as { [K in P]: V };

for (let i = 0; i < keys.length; i++) {
result[keys[i]] = values[i];
}

return result;
}

0 comments on commit 5377450

Please sign in to comment.