Skip to content

Commit 0386855

Browse files
committed
docs: update flattenObject documentation with Map support
1 parent 819e187 commit 0386855

File tree

3 files changed

+132
-12
lines changed

3 files changed

+132
-12
lines changed

docs/ja/reference/object/flattenObject.md

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,38 @@
11
# flattenObject
22

3-
ネストされたオブジェクトを単純なオブジェクトに平坦化します
3+
ネストされたオブジェクトを単純なオブジェクトに平坦化するか、Mapに直接変換します
44

55
- `Array`は平坦化されます。
66
- `Buffer``TypedArray`のような純粋なオブジェクトでないものは平坦化されません。
77

88
## インターフェース
99

1010
```typescript
11-
function flattenObject(object: object, { delimiter = '.' }: FlattenObjectOptions = {}): Record<string, any>;
11+
// Record<string, any>を返す
12+
function flattenObject(object: object, options?: { delimiter?: string }): Record<string, any>;
13+
14+
// Map<string, any>を返す(レガシー構文)
15+
function flattenObject(object: object, target: Map<string, any>): Map<string, any>;
16+
17+
// Map<string, any>を返す(区切り文字付き)
18+
function flattenObject(object: object, options: { delimiter?: string; target: Map<string, any> }): Map<string, any>;
1219
```
1320

1421
### パラメータ
1522

1623
- `object` (`object`): 平坦化するオブジェクト。
17-
- `delimiter` (`string`): ネストされたキーの区切り文字。デフォルトは `'.'`
24+
- `options` (`object`, オプション): オプションオブジェクト。
25+
- `delimiter` (`string`): ネストされたキーの区切り文字。デフォルトは `'.'`
26+
- `target` (`Map<string, any>`): 平坦化されたキーと値のペアで満たすMap。
1827

1928
### 戻り値
2029

21-
(`T`): 平坦化されたオブジェクト
30+
(`Record<string, any> | Map<string, any>`): 平坦化されたオブジェクトまたはMap
2231

2332
##
2433

34+
### 基本的な使用法(Record)
35+
2536
```typescript
2637
const nestedObject = {
2738
a: {
@@ -42,6 +53,8 @@ console.log(flattened);
4253
// }
4354
```
4455

56+
### カスタム区切り文字(Record)
57+
4558
```typescript
4659
const flattened = flattenObject(nestedObject, { delimiter: '/' });
4760
console.log(flattened);
@@ -52,3 +65,26 @@ console.log(flattened);
5265
// 'd/1': 3
5366
// }
5467
```
68+
69+
### Mapターゲット(レガシー構文)
70+
71+
```typescript
72+
const map = flattenObject(nestedObject, new Map());
73+
console.log(map);
74+
// 出力: Map { 'a.b.c' => 1, 'd.0' => 2, 'd.1' => 3 }
75+
76+
// 値へのアクセス
77+
console.log(map.get('a.b.c')); // 1
78+
console.log(map.size); // 3
79+
```
80+
81+
### Map + カスタム区切り文字
82+
83+
```typescript
84+
const customMap = flattenObject(nestedObject, {
85+
delimiter: '_',
86+
target: new Map(),
87+
});
88+
console.log(customMap);
89+
// 出力: Map { 'a_b_c' => 1, 'd_0' => 2, 'd_1' => 3 }
90+
```

docs/ko/reference/object/flattenObject.md

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,38 @@
11
# flattenObject
22

3-
중첩된 객체를 단순한 객체로 평탄화해요.
3+
중첩된 객체를 단순한 객체로 평탄화하거나 Map으로 직접 변환해요.
44

55
- `Array`는 평탄화돼요.
66
- 순수 객체가 아닌 `Buffer``TypedArray`는 평탄화되지 않아요.
77

88
## 인터페이스
99

1010
```typescript
11-
function flattenObject(object: object, { delimiter = '.' }: FlattenObjectOptions = {}): Record<string, any>;
11+
// Record<string, any> 반환
12+
function flattenObject(object: object, options?: { delimiter?: string }): Record<string, any>;
13+
14+
// Map<string, any> 반환 (기존 문법)
15+
function flattenObject(object: object, target: Map<string, any>): Map<string, any>;
16+
17+
// Map<string, any> 반환 (구분자 포함)
18+
function flattenObject(object: object, options: { delimiter?: string; target: Map<string, any> }): Map<string, any>;
1219
```
1320

1421
### 파라미터
1522

1623
- `object` (`object`): 평탄화할 객체.
17-
- `delimiter` (`string`): 중첩된 키의 구분자. 기본값은 `.`.
24+
- `options` (`object`, 선택): 옵션 객체.
25+
- `delimiter` (`string`): 중첩된 키의 구분자. 기본값은 `.`.
26+
- `target` (`Map<string, any>`): 평탄화된 키-값 쌍으로 채울 Map.
1827

1928
### 반환 값
2029

21-
(`T`): 평탄화된 객체.
30+
(`Record<string, any> | Map<string, any>`): 평탄화된 객체 또는 Map.
2231

2332
## 예시
2433

34+
### 기본 사용법 (Record)
35+
2536
```typescript
2637
const nestedObject = {
2738
a: {
@@ -42,6 +53,8 @@ console.log(flattened);
4253
// }
4354
```
4455

56+
### 구분자 지정 (Record)
57+
4558
```typescript
4659
const flattened = flattenObject(nestedObject, { delimiter: '/' });
4760
console.log(flattened);
@@ -52,3 +65,26 @@ console.log(flattened);
5265
// 'd/1': 3
5366
// }
5467
```
68+
69+
### Map 대상 (기존 문법)
70+
71+
```typescript
72+
const map = flattenObject(nestedObject, new Map());
73+
console.log(map);
74+
// Output: Map { 'a.b.c' => 1, 'd.0' => 2, 'd.1' => 3 }
75+
76+
// 값 접근
77+
console.log(map.get('a.b.c')); // 1
78+
console.log(map.size); // 3
79+
```
80+
81+
### Map + 구분자 지정
82+
83+
```typescript
84+
const customMap = flattenObject(nestedObject, {
85+
delimiter: '_',
86+
target: new Map(),
87+
});
88+
console.log(customMap);
89+
// Output: Map { 'a_b_c' => 1, 'd_0' => 2, 'd_1' => 3 }
90+
```

docs/reference/object/flattenObject.md

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,38 @@
11
# flattenObject
22

3-
Flattens a nested object into a single-level object with dot-separated keys.
3+
Flattens a nested object into a single-level object with dot-separated keys or directly into a Map.
44

55
- `Array`s are flattened.
66
- Non-plain objects, like `Buffer`s or `TypedArray`s, are not flattened.
77

88
## Signature
99

1010
```typescript
11-
function flattenObject(object: object, { delimiter = '.' }: FlattenObjectOptions = {}): Record<string, any>;
11+
// Return Record<string, any>
12+
function flattenObject(object: object, options?: { delimiter?: string }): Record<string, any>;
13+
14+
// Return Map<string, any> (legacy syntax)
15+
function flattenObject(object: object, target: Map<string, any>): Map<string, any>;
16+
17+
// Return Map<string, any> (with custom delimiter)
18+
function flattenObject(object: object, options: { delimiter?: string; target: Map<string, any> }): Map<string, any>;
1219
```
1320

1421
### Parameters
1522

1623
- `object` (`object`): The object to flatten.
17-
- `delimiter` (`string`): The delimiter to use between nested keys. Defaults to `'.'`.
24+
- `options` (`object`, optional): The options object.
25+
- `delimiter` (`string`): The delimiter to use between nested keys. Defaults to `'.'`.
26+
- `target` (`Map<string, any>`): The target Map to populate with flattened key-value pairs.
1827

1928
### Returns
2029

21-
(`T`): The flattened object.
30+
(`Record<string, any> | Map<string, any>`): The flattened object or Map.
2231

2332
## Examples
2433

34+
### Basic Usage (Record)
35+
2536
```typescript
2637
const nestedObject = {
2738
a: {
@@ -42,6 +53,8 @@ console.log(flattened);
4253
// }
4354
```
4455

56+
### Custom Delimiter (Record)
57+
4558
```typescript
4659
const flattened = flattenObject(nestedObject, { delimiter: '/' });
4760
console.log(flattened);
@@ -52,3 +65,38 @@ console.log(flattened);
5265
// 'd/1': 3
5366
// }
5467
```
68+
69+
### Map Target (Legacy Syntax)
70+
71+
```typescript
72+
const map = flattenObject(nestedObject, new Map());
73+
console.log(map);
74+
// Output: Map { 'a.b.c' => 1, 'd.0' => 2, 'd.1' => 3 }
75+
76+
// Access values
77+
console.log(map.get('a.b.c')); // 1
78+
console.log(map.size); // 3
79+
```
80+
81+
### Map with Custom Delimiter
82+
83+
```typescript
84+
const customMap = flattenObject(nestedObject, {
85+
delimiter: '_',
86+
target: new Map(),
87+
});
88+
console.log(customMap);
89+
// Output: Map { 'a_b_c' => 1, 'd_0' => 2, 'd_1' => 3 }
90+
```
91+
92+
### Performance Comparison
93+
94+
Using Map targets provides better performance compared to converting Records to Maps:
95+
96+
```typescript
97+
// Slower: Object → Array → Map (3 steps)
98+
const slowMap = new Map(Object.entries(flattenObject(largeObject)));
99+
100+
// Faster: Object → Map (1 step)
101+
const fastMap = flattenObject(largeObject, new Map());
102+
```

0 commit comments

Comments
 (0)