@inglorious/utils 3.6.3 → 3.7.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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/utils",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.7.0",
|
|
4
4
|
"description": "A set of general-purpose utility functions designed with functional programming principles in mind.",
|
|
5
5
|
"author": "IceOnFire <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
|
|
6
6
|
"license": "MIT",
|
|
@@ -2,16 +2,29 @@ import { v } from "../v.js"
|
|
|
2
2
|
import { isArray } from "./array.js"
|
|
3
3
|
|
|
4
4
|
const INITIAL_LEVEL = 0
|
|
5
|
+
const LAST_KEY_OFFSET = 1
|
|
6
|
+
const NEXT_KEY_OFFSET = 1
|
|
5
7
|
const NEXT_LEVEL = 2
|
|
6
8
|
|
|
7
9
|
/**
|
|
8
|
-
* Creates a deep clone of
|
|
10
|
+
* Creates a deep clone of a given value using the structured clone algorithm.
|
|
11
|
+
* This is a modern and efficient way to deep-clone objects.
|
|
9
12
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
13
|
+
* Note: `structuredClone` has some limitations. It cannot clone functions,
|
|
14
|
+
* DOM nodes, property descriptors, getters, setters, or certain other
|
|
15
|
+
* non-serializable objects. It will throw a `DataCloneError` in such cases.
|
|
16
|
+
*
|
|
17
|
+
* For more details, see:
|
|
18
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
|
|
19
|
+
*
|
|
20
|
+
* This is similar to Lodash's `_.cloneDeep`.
|
|
21
|
+
*
|
|
22
|
+
* @template T
|
|
23
|
+
* @param {T} obj - The value to clone.
|
|
24
|
+
* @returns {T} A deep clone of the input value.
|
|
12
25
|
*/
|
|
13
26
|
export function clone(obj) {
|
|
14
|
-
return
|
|
27
|
+
return structuredClone(obj)
|
|
15
28
|
}
|
|
16
29
|
|
|
17
30
|
/**
|
|
@@ -83,6 +96,42 @@ export function find(obj, callback) {
|
|
|
83
96
|
])
|
|
84
97
|
}
|
|
85
98
|
|
|
99
|
+
/**
|
|
100
|
+
* Gets the value at a specified path of an object. If the resolved value is
|
|
101
|
+
* `undefined`, the `defaultValue` is returned in its place. This function
|
|
102
|
+
* supports dot-notation for nested properties and numbers for array indices.
|
|
103
|
+
*
|
|
104
|
+
* This is similar to Lodash's `_.get`.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* const object = { 'a': [{ 'b': { 'c': 3 } }] };
|
|
108
|
+
* get(object, 'a.0.b.c');
|
|
109
|
+
* // => 3
|
|
110
|
+
*
|
|
111
|
+
* get(object, 'a.0.d', 'default');
|
|
112
|
+
* // => 'default'
|
|
113
|
+
*
|
|
114
|
+
* @param {Object} obj - The object to query.
|
|
115
|
+
* @param {string} path - The path of the property to retrieve.
|
|
116
|
+
* @param {*} [defaultValue=undefined] - The value returned for `undefined` resolved values.
|
|
117
|
+
* @returns {*} Returns the resolved value, else the `defaultValue`.
|
|
118
|
+
*/
|
|
119
|
+
export function get(obj, path, defaultValue = undefined) {
|
|
120
|
+
if (!path) return defaultValue
|
|
121
|
+
|
|
122
|
+
const keys = path.split(".")
|
|
123
|
+
let result = obj
|
|
124
|
+
|
|
125
|
+
for (const key of keys) {
|
|
126
|
+
if (result == null || typeof result !== "object") {
|
|
127
|
+
return defaultValue
|
|
128
|
+
}
|
|
129
|
+
result = result[key]
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return result !== undefined ? result : defaultValue
|
|
133
|
+
}
|
|
134
|
+
|
|
86
135
|
/**
|
|
87
136
|
* Checks if a value is a plain object.
|
|
88
137
|
*
|
|
@@ -185,6 +234,50 @@ export function serialize(obj) {
|
|
|
185
234
|
return JSON.stringify(obj, replacer)
|
|
186
235
|
}
|
|
187
236
|
|
|
237
|
+
/**
|
|
238
|
+
* Sets the value at a specified path of an object. If a portion of the path
|
|
239
|
+
* doesn't exist, it's created. Arrays are created for missing index properties
|
|
240
|
+
* while objects are created for all other missing properties. This function
|
|
241
|
+
* mutates the object.
|
|
242
|
+
*
|
|
243
|
+
* This is similar to Lodash's `_.set`.
|
|
244
|
+
*
|
|
245
|
+
* @example
|
|
246
|
+
* const object = { 'a': [{ 'b': { 'c': 3 } }] };
|
|
247
|
+
* set(object, 'a.0.b.d', 4);
|
|
248
|
+
* // object is now { 'a': [{ 'b': { 'c': 3, 'd': 4 } }] }
|
|
249
|
+
*
|
|
250
|
+
* set(object, 'x.0.y', 5);
|
|
251
|
+
* // object is now { 'a': [...], 'x': [{ 'y': 5 }] }
|
|
252
|
+
*
|
|
253
|
+
* @param {Object} obj - The object to modify.
|
|
254
|
+
* @param {string} path - The path of the property to set.
|
|
255
|
+
* @param {*} value - The value to set.
|
|
256
|
+
* @returns {Object} Returns the modified object.
|
|
257
|
+
*/
|
|
258
|
+
export function set(obj, path, value) {
|
|
259
|
+
if (!path) return obj
|
|
260
|
+
|
|
261
|
+
const keys = path.split(".")
|
|
262
|
+
let current = obj
|
|
263
|
+
|
|
264
|
+
for (let i = 0; i < keys.length - LAST_KEY_OFFSET; i++) {
|
|
265
|
+
const key = keys[i]
|
|
266
|
+
const nextKey = keys[i + NEXT_KEY_OFFSET]
|
|
267
|
+
|
|
268
|
+
// Create missing intermediate objects
|
|
269
|
+
if (!(key in current) || typeof current[key] !== "object") {
|
|
270
|
+
// Create array if next key is a number, object otherwise
|
|
271
|
+
current[key] = /^\d+$/.test(nextKey) ? [] : {}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
current = current[key]
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
current[keys[keys.length - LAST_KEY_OFFSET]] = value
|
|
278
|
+
return obj
|
|
279
|
+
}
|
|
280
|
+
|
|
188
281
|
/**
|
|
189
282
|
* Converts an object or array to a formatted string representation.
|
|
190
283
|
*
|
|
@@ -6,10 +6,12 @@ import {
|
|
|
6
6
|
deserialize,
|
|
7
7
|
filter,
|
|
8
8
|
find,
|
|
9
|
+
get,
|
|
9
10
|
isObject,
|
|
10
11
|
map,
|
|
11
12
|
produce,
|
|
12
13
|
serialize,
|
|
14
|
+
set,
|
|
13
15
|
toString,
|
|
14
16
|
} from "./object.js"
|
|
15
17
|
|
|
@@ -63,6 +65,62 @@ test("it should behave like Array.prototype.find, but on an object", () => {
|
|
|
63
65
|
expect(find(obj, callback)).toStrictEqual(expectedResult)
|
|
64
66
|
})
|
|
65
67
|
|
|
68
|
+
test("get should retrieve a value from a nested object", () => {
|
|
69
|
+
const obj = { a: { b: { c: 1 } }, d: [{ e: 2 }] }
|
|
70
|
+
|
|
71
|
+
expect(get(obj, "a.b.c")).toBe(1)
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
test("get should retrieve a value from an array within an object", () => {
|
|
75
|
+
const obj = { a: { b: { c: 1 } }, d: [{ e: 2 }] }
|
|
76
|
+
|
|
77
|
+
expect(get(obj, "d.0.e")).toBe(2)
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test("get should return a default value for a non-existent path", () => {
|
|
81
|
+
const obj = { a: { b: { c: 1 } } }
|
|
82
|
+
|
|
83
|
+
expect(get(obj, "a.x.y", "default")).toBe("default")
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
test("get should return undefined for a non-existent path without a default value", () => {
|
|
87
|
+
const obj = { a: { b: { c: 1 } } }
|
|
88
|
+
|
|
89
|
+
expect(get(obj, "a.x.y")).toBeUndefined()
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
test("get should handle null or undefined paths gracefully", () => {
|
|
93
|
+
const obj = { a: 1 }
|
|
94
|
+
|
|
95
|
+
expect(get(obj, null, "default")).toBe("default")
|
|
96
|
+
expect(get(obj, undefined, "default")).toBe("default")
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
test("set should modify a value at a given path", () => {
|
|
100
|
+
const obj = { a: { b: 1 } }
|
|
101
|
+
set(obj, "a.b", 2)
|
|
102
|
+
expect(obj.a.b).toBe(2)
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
test("set should create nested objects if they do not exist", () => {
|
|
106
|
+
const obj = {}
|
|
107
|
+
set(obj, "a.b.c", 3)
|
|
108
|
+
expect(obj).toStrictEqual({ a: { b: { c: 3 } } })
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
test("set should create nested arrays for numeric keys", () => {
|
|
112
|
+
const obj = {}
|
|
113
|
+
set(obj, "a.0.b", "value")
|
|
114
|
+
expect(obj).toStrictEqual({ a: [{ b: "value" }] })
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
test("set should return the mutated object", () => {
|
|
118
|
+
const obj = { a: 1 }
|
|
119
|
+
const result = set(obj, "b", 2)
|
|
120
|
+
expect(result).toBe(obj)
|
|
121
|
+
expect(result).toStrictEqual({ a: 1, b: 2 })
|
|
122
|
+
})
|
|
123
|
+
|
|
66
124
|
test("it correctly should check if a value is an object", () => {
|
|
67
125
|
expect(isObject(1)).toBe(false)
|
|
68
126
|
expect(isObject("a")).toBe(false)
|