Skip to content

Commit

Permalink
docs(mergeWith): Improve docs for mergeWith
Browse files Browse the repository at this point in the history
  • Loading branch information
raon0211 committed Aug 10, 2024
1 parent bf33cfb commit 8912165
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 34 deletions.
10 changes: 2 additions & 8 deletions docs/ko/reference/object/mergeWith.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@

`source`가 가지고 있는 값들을 `target` 객체로 병합해요.

어떻게 프로퍼티를 병합하는지 지정하기 위해서 `merge` 함수 인자를 정의하세요. `merge` 함수는 병합되는 모든 프로퍼티에 대해서, 다음과 같은 인자를 가지고 호출돼요.
어떻게 프로퍼티를 병합하는지 지정하기 위해서 `merge` 함수 인자를 정의하세요. `merge` 함수 인자는 `target` 객체에 설정될 값을 반환해야 해요.

- `targetValue`: `target` 객체가 가지고 있는 값.
- `sourceValue`: `source` 객체가 가지고 있는 값.
- `key`: 병합되고 있는 프로퍼티 이름.
- `target`: `target` 객체.
- `source`: `source` 객체.

`merge` 함수 인자는 `target` 객체에 설정될 값을 반환해야 해요. 만약 `undefined`를 반환한다면, 기본적으로 두 값을 깊이 병합해요. 깊은 병합에서는, 중첩된 객체나 배열을 다음과 같이 재귀적으로 병합해요.
만약 `undefined`를 반환한다면, 기본적으로 두 값을 깊이 병합해요. 깊은 병합에서는, 중첩된 객체나 배열을 다음과 같이 재귀적으로 병합해요.

- `source``target`의 프로퍼티가 모두 객체 또는 배열이라면, 두 객체와 배열은 병합돼요.
- 만약에 `source`의 프로퍼티가 `undefined` 라면, `target`의 프로퍼티를 덮어씌우지 않아요.
Expand Down
13 changes: 3 additions & 10 deletions docs/reference/object/mergeWith.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@

Merges the properties of the source object into the target object.

You can provide a custom `merge` function to control how properties are merged. The `merge` function is called for each property that is being merged and receives the following arguments:
You can provide a custom `merge` function to control how properties are merged. It should return the value to be set in the target object.

- `targetValue`: The current value of the property in the target object.
- `sourceValue`: The value of the property in the source object.
- `key`: The key of the property being merged.
- `target`: The target object.
- `source`: The source object.

The `merge` function should return the value to be set in the target object. If it returns `undefined`, a default deep merge will be applied for arrays and objects:
If it returns `undefined`, a default deep merge will be applied for arrays and objects:

- If a property in the source object is an array or an object and the corresponding property in the target object is also an array or object, they will be merged.
- If a property in the source object is undefined, it will not overwrite a defined property in the target object.
Expand Down Expand Up @@ -60,12 +54,11 @@ mergeWith(target, source, (targetValue, sourceValue) => {
const target = { a: [1], b: [2] };
const source = { a: [3], b: [4] };

const result = mergeWith(target, source, (objValue, srcValue) => {
mergeWith(target, source, (objValue, srcValue) => {
if (Array.isArray(objValue)) {
return objValue.concat(srcValue);
}
});

// Returns { a: [1, 3], b: [2, 4] })
```

Expand Down
10 changes: 2 additions & 8 deletions docs/zh_hans/reference/object/mergeWith.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@

将源对象的属性合并到目标对象中。

您可以提供自定义的 `merge` 函数来控制属性的合并方式。`merge` 函数会在每个属性被合并时调用,并接收以下参数:
您可以提供自定义的 `merge` 函数来控制属性的合并方式。`merge` 函数应返回要在目标对象中设置的值。

- `targetValue`:目标对象中属性的当前值。
- `sourceValue`:源对象中属性的值。
- `key`:被合并的属性的键。
- `target`:目标对象。
- `source`:源对象。

`merge` 函数应返回要在目标对象中设置的值。如果返回 `undefined`,则会对数组和对象应用默认的深度合并:
如果返回 `undefined`,则会对数组和对象应用默认的深度合并:

- 如果源对象中的属性是数组或对象,而目标对象中对应的属性也是数组或对象,它们将被合并。
- 如果源对象中的属性是 `undefined`,它不会覆盖目标对象中已定义的属性。
Expand Down
10 changes: 2 additions & 8 deletions src/object/mergeWith.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@ import { isObjectLike } from '../compat/predicate/isObjectLike.ts';
/**
* Merges the properties of the source object into the target object.
*
* You can provide a custom `merge` function to control how properties are merged. The `merge` function is called for each property that is being merged and receives the following arguments:
* You can provide a custom `merge` function to control how properties are merged. It should return the value to be set in the target object.
*
* - `targetValue`: The current value of the property in the target object.
* - `sourceValue`: The value of the property in the source object.
* - `key`: The key of the property being merged.
* - `target`: The target object.
* - `source`: The source object.
*
* The `merge` function should return the value to be set in the target object. If it returns `undefined`, a default deep merge will be applied for arrays and objects:
* If it returns `undefined`, a default deep merge will be applied for arrays and objects:
*
* - If a property in the source object is an array or an object and the corresponding property in the target object is also an array or object, they will be merged.
* - If a property in the source object is undefined, it will not overwrite a defined property in the target object.
Expand Down

0 comments on commit 8912165

Please sign in to comment.