@ptolemy2002/list-object-utils 2.0.3 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -160,6 +160,30 @@ A simple implementation of `JSON.stringify` that converts undefined values to `n
160
160
  #### Returns
161
161
  `string` - The string representation of the object.
162
162
 
163
+ ### listClone<T>
164
+ #### Description
165
+ Clones a list. Uses recursion to clone nested lists and the `objectClone` function to clone nested objects.
166
+
167
+ #### Parameters
168
+ - `list` (`T[]`): The list to be cloned.
169
+ - `maxDepth` (`number`): The maximum depth to which the list should be cloned. Default is `Infinity`.
170
+ - `currentDepth` (`number`): The current depth of the list. This should not be specified on the initial call, and is only used for recursion purposes. Default is `0`.
171
+
172
+ #### Returns
173
+ `T[]` - The cloned list.
174
+
175
+ ### objectClone<T>
176
+ #### Description
177
+ Clones an object. Uses recursion to clone nested objects and the `listClone` function to clone nested lists.
178
+
179
+ #### Parameters
180
+ - `obj` (`T`): The object to be cloned.
181
+ - `maxDepth` (`number`): The maximum depth to which the object should be cloned. Default is `Infinity`.
182
+ - `currentDepth` (`number`): The current depth of the object. This should not be specified on the initial call, and is only used for recursion purposes. Default is `0`.
183
+
184
+ #### Returns
185
+ `T` - The cloned object.
186
+
163
187
  ## Peer Dependencies
164
188
  - @ptolemy2002/ts-utils^2.0.0
165
189
 
package/dist/index.d.ts CHANGED
@@ -17,3 +17,5 @@ export declare function flattenKeys<T>(obj: T, prefix?: string): Record<string,
17
17
  export type SortCallback = (a: number, b: number) => number;
18
18
  export declare function sortWithIndeces<T>(toSort: T[], sort?: SortCallback, descending?: boolean): T[];
19
19
  export declare function objectToString(obj: Object): string;
20
+ export declare function listClone<T>(list: T[], maxDepth?: number, depth?: number): T[];
21
+ export declare function objectClone<T>(obj: T, maxDepth?: number, depth?: number): T;
package/dist/index.js CHANGED
@@ -12,6 +12,8 @@ exports.objectDifference = objectDifference;
12
12
  exports.flattenKeys = flattenKeys;
13
13
  exports.sortWithIndeces = sortWithIndeces;
14
14
  exports.objectToString = objectToString;
15
+ exports.listClone = listClone;
16
+ exports.objectClone = objectClone;
15
17
  function listsEqual(a, b) {
16
18
  if (a === b)
17
19
  return true;
@@ -184,3 +186,31 @@ function sortWithIndeces(toSort, sort = () => 0, descending = false) {
184
186
  function objectToString(obj) {
185
187
  return JSON.stringify(obj, (_, v) => v === undefined ? null : v);
186
188
  }
189
+ function listClone(list, maxDepth = Infinity, depth = 0) {
190
+ if (depth >= maxDepth)
191
+ return list;
192
+ return list.map((item) => {
193
+ if (Array.isArray(item))
194
+ return listClone(item, maxDepth, depth + 1);
195
+ if (typeof item === "object")
196
+ return objectClone(item, maxDepth, depth + 1);
197
+ return item;
198
+ });
199
+ }
200
+ function objectClone(obj, maxDepth = Infinity, depth = 0) {
201
+ if (depth >= maxDepth)
202
+ return obj;
203
+ return Object.keys(obj).reduce((acc, key) => {
204
+ const value = obj[key];
205
+ if (Array.isArray(value)) {
206
+ acc[key] = listClone(value, maxDepth, depth + 1);
207
+ }
208
+ else if (typeof value === "object") {
209
+ acc[key] = objectClone(value, maxDepth, depth + 1);
210
+ }
211
+ else {
212
+ acc[key] = value;
213
+ }
214
+ return acc;
215
+ }, {});
216
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/list-object-utils",
3
- "version": "2.0.3",
3
+ "version": "2.1.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",