| 1 | import { PropertyPath } from '../_internal/PropertyPath.mjs';
|
|---|
| 2 |
|
|---|
| 3 | /**
|
|---|
| 4 | * Sets the value at the specified path of the given object. If any part of the path does not exist, it will be created.
|
|---|
| 5 | *
|
|---|
| 6 | * @template T - The type of the object.
|
|---|
| 7 | * @param {T} object - The object to modify.
|
|---|
| 8 | * @param {PropertyPath} path - The path of the property to set.
|
|---|
| 9 | * @param {any} value - The value to set.
|
|---|
| 10 | * @returns {T} - The modified object.
|
|---|
| 11 | *
|
|---|
| 12 | * @example
|
|---|
| 13 | * // Set a value in a nested object
|
|---|
| 14 | * const obj = { a: { b: { c: 3 } } };
|
|---|
| 15 | * set(obj, 'a.b.c', 4);
|
|---|
| 16 | * console.log(obj.a.b.c); // 4
|
|---|
| 17 | *
|
|---|
| 18 | * @example
|
|---|
| 19 | * // Set a value in an array
|
|---|
| 20 | * const arr = [1, 2, 3];
|
|---|
| 21 | * set(arr, 1, 4);
|
|---|
| 22 | * console.log(arr[1]); // 4
|
|---|
| 23 | *
|
|---|
| 24 | * @example
|
|---|
| 25 | * // Create non-existent path and set value
|
|---|
| 26 | * const obj = {};
|
|---|
| 27 | * set(obj, 'a.b.c', 4);
|
|---|
| 28 | * console.log(obj); // { a: { b: { c: 4 } } }
|
|---|
| 29 | */
|
|---|
| 30 | declare function set<T extends object>(object: T, path: PropertyPath, value: any): T;
|
|---|
| 31 | /**
|
|---|
| 32 | * Sets the value at the specified path of the given object. If any part of the path does not exist, it will be created.
|
|---|
| 33 | *
|
|---|
| 34 | * @template R - The return type.
|
|---|
| 35 | * @param {object} object - The object to modify.
|
|---|
| 36 | * @param {PropertyPath} path - The path of the property to set.
|
|---|
| 37 | * @param {any} value - The value to set.
|
|---|
| 38 | * @returns {R} - The modified object.
|
|---|
| 39 | *
|
|---|
| 40 | * @example
|
|---|
| 41 | * // Set a value in a nested object
|
|---|
| 42 | * const obj = { a: { b: { c: 3 } } };
|
|---|
| 43 | * set(obj, 'a.b.c', 4);
|
|---|
| 44 | * console.log(obj.a.b.c); // 4
|
|---|
| 45 | *
|
|---|
| 46 | * @example
|
|---|
| 47 | * // Set a value in an array
|
|---|
| 48 | * const arr = [1, 2, 3];
|
|---|
| 49 | * set(arr, 1, 4);
|
|---|
| 50 | * console.log(arr[1]); // 4
|
|---|
| 51 | *
|
|---|
| 52 | * @example
|
|---|
| 53 | * // Create non-existent path and set value
|
|---|
| 54 | * const obj = {};
|
|---|
| 55 | * set(obj, 'a.b.c', 4);
|
|---|
| 56 | * console.log(obj); // { a: { b: { c: 4 } } }
|
|---|
| 57 | */
|
|---|
| 58 | declare function set<R>(object: object, path: PropertyPath, value: any): R;
|
|---|
| 59 |
|
|---|
| 60 | export { set };
|
|---|