docs(set): Update documentation for set

This commit is contained in:
raon0211 2024-07-18 20:10:56 +09:00
parent 5dcde9f159
commit bb063fbcf6
5 changed files with 70 additions and 14 deletions

View File

@ -160,6 +160,12 @@ function sidebar(): DefaultTheme.Sidebar {
{ text: 'capitalize', link: '/zh_hans/reference/string/capitalize' },
],
},
{
"text": "兼容性库",
"items": [
{ "text": "set", "link": "/zh_hans/reference/compat/object/set" }
]
}
],
},
];

View File

@ -6,23 +6,23 @@
지정된 경로에 주어진 값을 설정해요. 경로의 일부가 존재하지 않으면 생성됩니다.
## Signature
## 인터페이스
```typescript
function set<T>(obj: Settable, path: Path, value: any): T;
```
### Parameters
### 파라미터
- `obj` (Settable): 값을 설정할 객체예요.
- `path` (Path): 값을 설정할 속성의 경로예요.
- `value` (any): 설정할 값이에요.
- `obj` (`T`): 값을 설정할 객체.
- `path` (`string | number | symbol | Array<string | number | symbol>`): 값을 설정할 프로퍼티 경로.
- `value` (`unknown`): 설정할 값.
### Returns
### 반환 값
(`T`): 수정된 객체를 반환해요. T를 지정하지 않으면 unknown이에요.
## Examples
## 예시
```typescript
import { set } from 'es-toolkit/compat';

View File

@ -9,14 +9,14 @@ Sets the given value at the specified path of the object. If any part of the pat
## Signature
```typescript
function set<T>(obj: Settable, path: Path, value: any): T;
function set<T extends object>(obj: T, path: string | number | symbol | Array<string | number | symbol>, value: unknown): T
```
### Parameters
- `obj` (Settable): The object to modify.
- `path` (Path): The path of the property to set.
- `value` (any): The value to set.
- `obj` (`T`): The object to modify.
- `path` (`string | number | symbol | Array<string | number | symbol>`): The path of the property to set.
- `value` (`unknown`): The value to set.
### Returns

View File

@ -0,0 +1,52 @@
# set
::: info
此功能在我们的[兼容性库](../../compatibility.md)中可用,`es-toolkit/compat`。
:::
在对象的指定路径设置给定值。如果路径的任何部分不存在,将会创建它。
## 签名
```typescript
function set<T extends object>(obj: T, path: string | number | symbol | Array<string | number | symbol>, value: unknown): T
```
### 参数
- `obj` (`T`): 要修改的对象。
- `path` (`string | number | symbol | Array<string | number | symbol>`): 要设置的属性路径。
- `value` (`unknown`): 要设置的值。
### 返回值
(`T`): 返回修改后的对象。如果未指定 T默认值为 unknown。
## 示例
```typescript
import { set } from 'es-toolkit/compat';
// Set a value in a nested object
const obj = { a: { b: { c: 3 } } };
set(obj, 'a.b.c', 4);
console.log(obj.a.b.c); // 4
// Set a value in an array
const arr = [1, 2, 3];
set(arr, 1, 4);
console.log(arr[1]); // 4
// Create non-existent path and set value
const obj2 = {};
set(obj2, 'a.b.c', 4);
console.log(obj2); // { a: { b: { c: 4 } } }
// Use with interface
interface O {
a: number;
}
const obj3 = {};
const result = set<O>(obj3, 'a', 1); // typeof result = { a: number }
console.log(result); // { a: 1 }
```

View File

@ -28,10 +28,8 @@ import { toPath } from "../_internal/toPath";
* set(obj, 'a.b.c', 4);
* console.log(obj); // { a: { b: { c: 4 } } }
*/
export function set<T extends object>(obj: T, path: PropertyKey | readonly PropertyKey[], value: unknown): T;
export function set<T>(obj: object, path: PropertyKey | readonly PropertyKey[], value: unknown): T;
export function set<T>(obj: T, path: PropertyKey | readonly PropertyKey[], value: unknown): T {
export function set<T extends object>(obj: T, path: PropertyKey | readonly PropertyKey[], value: unknown): T {
const resolvedPath = Array.isArray(path)
? path
: typeof path === 'string'