Skip to content

Commit 21e5def

Browse files
committed
feat: implement {mapValues,mapKeys}Async functions
1 parent 9980a38 commit 21e5def

File tree

13 files changed

+608
-0
lines changed

13 files changed

+608
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# mapKeysAsync
2+
3+
非同期関数を通じてキーを変換した新しいオブジェクトを返します。
4+
```typescript
5+
const newObj = await mapKeysAsync(object, getNewKey);
6+
```
7+
8+
## 使用方法
9+
10+
### `mapKeysAsync(object, getNewKey, options?)`
11+
12+
各キーを非同期に変換して新しいオブジェクトを作成する場合に`mapKeysAsync`を使用します。値はそのまま保持され、キーのみが`getNewKey`関数の解決結果に変更されます。
13+
```typescript
14+
import { mapKeysAsync } from 'es-toolkit/object';
15+
16+
// キーにプレフィックスを追加
17+
const obj = { a: 1, b: 2 };
18+
const prefixed = await mapKeysAsync(obj, (value, key) => `prefix_${key}`);
19+
// prefixed は { prefix_a: 1, prefix_b: 2 } になる
20+
21+
// キーと値を組み合わせて新しいキーを作成
22+
const combined = await mapKeysAsync(obj, (value, key) => `${key}${value}`);
23+
// combined は { a1: 1, b2: 2 } になる
24+
25+
// キーを大文字に変換
26+
const uppercased = await mapKeysAsync(obj, (value, key) => key.toString().toUpperCase());
27+
// uppercased は { A: 1, B: 2 } になる
28+
29+
// 並行処理数を制限
30+
await mapKeysAsync(obj, async (value, key) => await processKey(key, value), { concurrency: 2 });
31+
// 最大2つのキーが同時に処理される
32+
```
33+
34+
#### パラメータ
35+
36+
- `object` (`T extends Record<PropertyKey, any>`): キーを変換する元のオブジェクト。
37+
- `getNewKey` (`(value: T[keyof T], key: keyof T, object: T) => Promise<K>`): 新しいキーを生成する非同期関数。値、キー、オブジェクト全体をパラメータとして受け取る。
38+
- `options` (`MapKeysAsyncOptions`, 省略可): 並行処理数を制御するオプション。
39+
- `concurrency` (`number`, 省略可): 並行処理の最大数。指定しない場合、すべての操作が同時に実行される。
40+
41+
#### 戻り値
42+
43+
(`Promise<Record<K, T[keyof T]>>`): 変換されたキーを持つ新しいオブジェクトに解決されるプロミス。
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# mapValuesAsync
2+
3+
非同期関数を通じて値を変換した新しいオブジェクトを返します。
4+
```typescript
5+
const newObj = await mapValuesAsync(object, getNewValue);
6+
```
7+
8+
## 使用方法
9+
10+
### `mapValuesAsync(object, getNewValue, options?)`
11+
12+
各値を非同期に変換して新しいオブジェクトを作成する場合に`mapValuesAsync`を使用します。キーはそのまま保持され、値のみが`getNewValue`関数の解決結果に変更されます。
13+
```typescript
14+
import { mapValuesAsync } from 'es-toolkit/object';
15+
16+
// すべての値を2倍にする
17+
const numbers = { a: 1, b: 2, c: 3 };
18+
const doubled = await mapValuesAsync(numbers, async value => value * 2);
19+
// doubled は { a: 2, b: 4, c: 6 } になる
20+
21+
// 文字列値を大文字に変換
22+
const strings = { first: 'hello', second: 'world' };
23+
const uppercased = await mapValuesAsync(strings, async value => value.toUpperCase());
24+
// uppercased は { first: 'HELLO', second: 'WORLD' } になる
25+
26+
// キーと値の両方を使用
27+
const scores = { alice: 85, bob: 90, charlie: 95 };
28+
const grades = await mapValuesAsync(scores, async (value, key) => `${key}: ${value >= 90 ? 'A' : 'B'}`);
29+
// grades は { alice: 'alice: B', bob: 'bob: A', charlie: 'charlie: A' } になる
30+
31+
// 並行処理数を制限
32+
const items = { a: 1, b: 2, c: 3 };
33+
await mapValuesAsync(items, async item => await processItem(item), { concurrency: 2 });
34+
// 最大2つの値が同時に処理される
35+
```
36+
37+
#### パラメータ
38+
39+
- `object` (`T extends object`): 値を変換する元のオブジェクト。
40+
- `getNewValue` (`(value: T[K], key: K, object: T) => Promise<V>`): 新しい値を生成する非同期関数。値、キー、オブジェクト全体をパラメータとして受け取る。
41+
- `options` (`MapValuesAsyncOptions`, 省略可): 並行処理数を制御するオプション。
42+
- `concurrency` (`number`, 省略可): 並行処理の最大数。指定しない場合、すべての操作が同時に実行される。
43+
44+
#### 戻り値
45+
46+
(`Promise<Record<K, V>>`): 変換された値を持つ新しいオブジェクトに解決されるプロミス。
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# mapKeysAsync
2+
3+
비동기 함수를 통해 키를 변환한 새 객체를 반환합니다.
4+
```typescript
5+
const newObj = await mapKeysAsync(object, getNewKey);
6+
```
7+
8+
## 사용법
9+
10+
### `mapKeysAsync(object, getNewKey, options?)`
11+
12+
각 키를 비동기적으로 변환하여 새 객체를 만들 때 `mapKeysAsync`를 사용합니다. 값은 그대로 유지되며 키만 `getNewKey` 함수의 해결 결과로 변경됩니다.
13+
```typescript
14+
import { mapKeysAsync } from 'es-toolkit/object';
15+
16+
// 키에 접두사 추가
17+
const obj = { a: 1, b: 2 };
18+
const prefixed = await mapKeysAsync(obj, (value, key) => `prefix_${key}`);
19+
// prefixed는 { prefix_a: 1, prefix_b: 2 }가 됨
20+
21+
// 키와 값을 결합하여 새 키 생성
22+
const combined = await mapKeysAsync(obj, (value, key) => `${key}${value}`);
23+
// combined는 { a1: 1, b2: 2 }가 됨
24+
25+
// 키를 대문자로 변환
26+
const uppercased = await mapKeysAsync(obj, (value, key) => key.toString().toUpperCase());
27+
// uppercased는 { A: 1, B: 2 }가 됨
28+
29+
// 동시 실행 수 제한
30+
await mapKeysAsync(obj, async (value, key) => await processKey(key, value), { concurrency: 2 });
31+
// 최대 2개의 키가 동시에 처리됨
32+
```
33+
34+
#### 매개변수
35+
36+
- `object` (`T extends Record<PropertyKey, any>`): 키를 변환할 원본 객체.
37+
- `getNewKey` (`(value: T[keyof T], key: keyof T, object: T) => Promise<K>`): 새 키를 생성하는 비동기 함수. 값, 키, 전체 객체를 매개변수로 받음.
38+
- `options` (`MapKeysAsyncOptions`, 선택사항): 동시 실행 수를 제어하는 옵션.
39+
- `concurrency` (`number`, 선택사항): 최대 동시 실행 수. 지정하지 않으면 모든 작업이 동시에 실행됨.
40+
41+
#### 반환값
42+
43+
(`Promise<Record<K, T[keyof T]>>`): 변환된 키를 가진 새 객체로 해결되는 프로미스.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# mapValuesAsync
2+
3+
비동기 함수를 통해 값을 변환한 새 객체를 반환합니다.
4+
```typescript
5+
const newObj = await mapValuesAsync(object, getNewValue);
6+
```
7+
8+
## 사용법
9+
10+
### `mapValuesAsync(object, getNewValue, options?)`
11+
12+
각 값을 비동기적으로 변환하여 새 객체를 만들 때 `mapValuesAsync`를 사용합니다. 키는 그대로 유지되며 값만 `getNewValue` 함수의 해결 결과로 변경됩니다.
13+
```typescript
14+
import { mapValuesAsync } from 'es-toolkit/object';
15+
16+
// 모든 값을 2배로 만들기
17+
const numbers = { a: 1, b: 2, c: 3 };
18+
const doubled = await mapValuesAsync(numbers, async value => value * 2);
19+
// doubled는 { a: 2, b: 4, c: 6 }이 됨
20+
21+
// 문자열 값을 대문자로 변환
22+
const strings = { first: 'hello', second: 'world' };
23+
const uppercased = await mapValuesAsync(strings, async value => value.toUpperCase());
24+
// uppercased는 { first: 'HELLO', second: 'WORLD' }가 됨
25+
26+
// 키와 값 모두 사용
27+
const scores = { alice: 85, bob: 90, charlie: 95 };
28+
const grades = await mapValuesAsync(scores, async (value, key) => `${key}: ${value >= 90 ? 'A' : 'B'}`);
29+
// grades는 { alice: 'alice: B', bob: 'bob: A', charlie: 'charlie: A' }가 됨
30+
31+
// 동시 실행 수 제한
32+
const items = { a: 1, b: 2, c: 3 };
33+
await mapValuesAsync(items, async item => await processItem(item), { concurrency: 2 });
34+
// 최대 2개의 값이 동시에 처리됨
35+
```
36+
37+
#### 매개변수
38+
39+
- `object` (`T extends object`): 값을 변환할 원본 객체.
40+
- `getNewValue` (`(value: T[K], key: K, object: T) => Promise<V>`): 새 값을 생성하는 비동기 함수. 값, 키, 전체 객체를 매개변수로 받음.
41+
- `options` (`MapValuesAsyncOptions`, 선택사항): 동시 실행 수를 제어하는 옵션.
42+
- `concurrency` (`number`, 선택사항): 최대 동시 실행 수. 지정하지 않으면 모든 작업이 동시에 실행됨.
43+
44+
#### 반환값
45+
46+
(`Promise<Record<K, V>>`): 변환된 값을 가진 새 객체로 해결되는 프로미스.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# mapKeysAsync
2+
3+
Returns a new object with keys transformed through an async function.
4+
5+
```typescript
6+
const newObj = await mapKeysAsync(object, getNewKey);
7+
```
8+
9+
## Usage
10+
11+
### `mapKeysAsync(object, getNewKey, options?)`
12+
13+
Use `mapKeysAsync` when you want to create a new object by asynchronously transforming each key. Values remain the same, and only the keys are changed to the resolved results of the `getNewKey` function.
14+
15+
```typescript
16+
import { mapKeysAsync } from 'es-toolkit/object';
17+
18+
// Add prefix to keys
19+
const obj = { a: 1, b: 2 };
20+
const prefixed = await mapKeysAsync(obj, (value, key) => `prefix_${key}`);
21+
// prefixed becomes { prefix_a: 1, prefix_b: 2 }
22+
23+
// Combine key and value to create new keys
24+
const combined = await mapKeysAsync(obj, (value, key) => `${key}${value}`);
25+
// combined becomes { a1: 1, b2: 2 }
26+
27+
// Convert keys to uppercase
28+
const uppercased = await mapKeysAsync(obj, (value, key) => key.toString().toUpperCase());
29+
// uppercased becomes { A: 1, B: 2 }
30+
31+
// Limit concurrency.
32+
await mapKeysAsync(obj, async (value, key) => await processKey(key, value), { concurrency: 2 });
33+
// Only 2 keys are processed concurrently at most.
34+
```
35+
36+
#### Parameters
37+
38+
- `object` (`T extends Record<PropertyKey, any>`): The object to transform keys from.
39+
- `getNewKey` (`(value: T[keyof T], key: keyof T, object: T) => Promise<K>`): An async function that generates new keys. Receives value, key, and the entire object as parameters.
40+
- `options` (`MapKeysAsyncOptions`, optional): Options to control concurrency.
41+
- `concurrency` (`number`, optional): Maximum number of concurrent operations. If not specified, all operations run concurrently.
42+
43+
#### Returns
44+
45+
(`Promise<Record<K, T[keyof T]>>`): A promise that resolves to a new object with transformed keys.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# mapValuesAsync
2+
3+
Returns a new object with values transformed through an async function.
4+
5+
```typescript
6+
const newObj = await mapValuesAsync(object, getNewValue);
7+
```
8+
9+
## Usage
10+
11+
### `mapValuesAsync(object, getNewValue, options?)`
12+
13+
Use `mapValuesAsync` when you want to create a new object by asynchronously transforming each value. Keys remain the same, and only the values are changed to the resolved results of the `getNewValue` function.
14+
15+
```typescript
16+
import { mapValuesAsync } from 'es-toolkit/object';
17+
18+
// Double all values
19+
const numbers = { a: 1, b: 2, c: 3 };
20+
const doubled = await mapValuesAsync(numbers, async value => value * 2);
21+
// doubled becomes { a: 2, b: 4, c: 6 }
22+
23+
// Convert string values to uppercase
24+
const strings = { first: 'hello', second: 'world' };
25+
const uppercased = await mapValuesAsync(strings, async value => value.toUpperCase());
26+
// uppercased becomes { first: 'HELLO', second: 'WORLD' }
27+
28+
// Use both key and value
29+
const scores = { alice: 85, bob: 90, charlie: 95 };
30+
const grades = await mapValuesAsync(scores, async (value, key) => `${key}: ${value >= 90 ? 'A' : 'B'}`);
31+
// grades becomes { alice: 'alice: B', bob: 'bob: A', charlie: 'charlie: A' }
32+
33+
// Limit concurrency.
34+
const items = { a: 1, b: 2, c: 3 };
35+
await mapValuesAsync(items, async item => await processItem(item), { concurrency: 2 });
36+
// Only 2 values are processed concurrently at most.
37+
```
38+
39+
#### Parameters
40+
41+
- `object` (`T extends object`): The object to transform values from.
42+
- `getNewValue` (`(value: T[K], key: K, object: T) => Promise<V>`): An async function that generates new values. Receives value, key, and the entire object as parameters.
43+
- `options` (`MapValuesAsyncOptions`, optional): Options to control concurrency.
44+
- `concurrency` (`number`, optional): Maximum number of concurrent operations. If not specified, all operations run concurrently.
45+
46+
#### Returns
47+
48+
(`Promise<Record<K, V>>`): A promise that resolves to a new object with transformed values.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# mapKeysAsync
2+
3+
返回一个通过异步函数转换键后的新对象。
4+
```typescript
5+
const newObj = await mapKeysAsync(object, getNewKey);
6+
```
7+
8+
## 用法
9+
10+
### `mapKeysAsync(object, getNewKey, options?)`
11+
12+
当您想要通过异步转换每个键来创建新对象时使用`mapKeysAsync`。值保持不变,只有键会更改为`getNewKey`函数的解析结果。
13+
```typescript
14+
import { mapKeysAsync } from 'es-toolkit/object';
15+
16+
// 为键添加前缀
17+
const obj = { a: 1, b: 2 };
18+
const prefixed = await mapKeysAsync(obj, (value, key) => `prefix_${key}`);
19+
// prefixed 变为 { prefix_a: 1, prefix_b: 2 }
20+
21+
// 组合键和值来创建新键
22+
const combined = await mapKeysAsync(obj, (value, key) => `${key}${value}`);
23+
// combined 变为 { a1: 1, b2: 2 }
24+
25+
// 将键转换为大写
26+
const uppercased = await mapKeysAsync(obj, (value, key) => key.toString().toUpperCase());
27+
// uppercased 变为 { A: 1, B: 2 }
28+
29+
// 限制并发数
30+
await mapKeysAsync(obj, async (value, key) => await processKey(key, value), { concurrency: 2 });
31+
// 最多同时处理2个键
32+
```
33+
34+
#### 参数
35+
36+
- `object` (`T extends Record<PropertyKey, any>`): 要转换键的源对象。
37+
- `getNewKey` (`(value: T[keyof T], key: keyof T, object: T) => Promise<K>`): 生成新键的异步函数。接收值、键和整个对象作为参数。
38+
- `options` (`MapKeysAsyncOptions`, 可选): 控制并发的选项。
39+
- `concurrency` (`number`, 可选): 最大并发操作数。如果未指定,所有操作将并发运行。
40+
41+
#### 返回值
42+
43+
(`Promise<Record<K, T[keyof T]>>`): 解析为具有转换后键的新对象的 Promise。
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# mapValuesAsync
2+
3+
返回一个通过异步函数转换值后的新对象。
4+
```typescript
5+
const newObj = await mapValuesAsync(object, getNewValue);
6+
```
7+
8+
## 用法
9+
10+
### `mapValuesAsync(object, getNewValue, options?)`
11+
12+
当您想要通过异步转换每个值来创建新对象时使用`mapValuesAsync`。键保持不变,只有值会更改为`getNewValue`函数的解析结果。
13+
```typescript
14+
import { mapValuesAsync } from 'es-toolkit/object';
15+
16+
// 将所有值翻倍
17+
const numbers = { a: 1, b: 2, c: 3 };
18+
const doubled = await mapValuesAsync(numbers, async value => value * 2);
19+
// doubled 变为 { a: 2, b: 4, c: 6 }
20+
21+
// 将字符串值转换为大写
22+
const strings = { first: 'hello', second: 'world' };
23+
const uppercased = await mapValuesAsync(strings, async value => value.toUpperCase());
24+
// uppercased 变为 { first: 'HELLO', second: 'WORLD' }
25+
26+
// 同时使用键和值
27+
const scores = { alice: 85, bob: 90, charlie: 95 };
28+
const grades = await mapValuesAsync(scores, async (value, key) => `${key}: ${value >= 90 ? 'A' : 'B'}`);
29+
// grades 变为 { alice: 'alice: B', bob: 'bob: A', charlie: 'charlie: A' }
30+
31+
// 限制并发数
32+
const items = { a: 1, b: 2, c: 3 };
33+
await mapValuesAsync(items, async item => await processItem(item), { concurrency: 2 });
34+
// 最多同时处理2个值
35+
```
36+
37+
#### 参数
38+
39+
- `object` (`T extends object`): 要转换值的源对象。
40+
- `getNewValue` (`(value: T[K], key: K, object: T) => Promise<V>`): 生成新值的异步函数。接收值、键和整个对象作为参数。
41+
- `options` (`MapValuesAsyncOptions`, 可选): 控制并发的选项。
42+
- `concurrency` (`number`, 可选): 最大并发操作数。如果未指定,所有操作将并发运行。
43+
44+
#### 返回值
45+
46+
(`Promise<Record<K, V>>`): 解析为具有转换后值的新对象的 Promise。

src/object/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ export { findKey } from './findKey.ts';
55
export { flattenObject } from './flattenObject.ts';
66
export { invert } from './invert.ts';
77
export { mapKeys } from './mapKeys.ts';
8+
export { mapKeysAsync } from './mapKeysAsync.ts';
89
export { mapValues } from './mapValues.ts';
10+
export { mapValuesAsync } from './mapValuesAsync.ts';
911
export { merge } from './merge.ts';
1012
export { mergeWith } from './mergeWith.ts';
1113
export { omit } from './omit.ts';

0 commit comments

Comments
 (0)