some-common-functions-js 1.1.7 → 1.1.9
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/CHANGELOG.md +8 -1
- package/index.js +22 -23
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -61,4 +61,11 @@ Added the outstanding change log for the previous version.
|
|
|
61
61
|
Removed unnecessary use of the getPaths() function in the get() function to improve effieciency.
|
|
62
62
|
|
|
63
63
|
## Version 1.1.7 - 2026/01//01 - ITA
|
|
64
|
-
Changed recursive functions to iterative functions, in order to overcome stack limits imposed by the front-end (browsers).
|
|
64
|
+
Changed recursive functions to iterative functions, in order to overcome stack limits imposed by the front-end (browsers).
|
|
65
|
+
|
|
66
|
+
## Version 1.1.8 - 2026/01//01 - ITA
|
|
67
|
+
Changed an additional recursive function to an iterative function.
|
|
68
|
+
|
|
69
|
+
## Version 1.1.9 - 2026/01/03 - ITA
|
|
70
|
+
- During object assignment, deepClone() function uses object spread to remove references to the original object.
|
|
71
|
+
- get() function replaces falsy-value evaluation (!obj) with 'in' operator for field existence checks. Because the falsy-value evaluation also evaluates to true with existent fields with values that are empty strings or null, which is not desirable.
|
package/index.js
CHANGED
|
@@ -14,6 +14,10 @@
|
|
|
14
14
|
* Updated test.js file accordingly.
|
|
15
15
|
* 2025/12/29 ITA 1.06 Removed unnecessary use of the getPaths() function in the get() function to improve effieciency.
|
|
16
16
|
* 2026/01/01 ITA 1.07 Changed recursive functions to iterative functions, so as to overcome stack limits when functions are used in the front-end (browsers).
|
|
17
|
+
* 2026/01/01 ITA 1.08 Changed an additional recursive function to an iterative function.
|
|
18
|
+
* 2026/01/03 ITA 1.09 deepClone(): Used object spread to prevent reference sharing during object assignment.
|
|
19
|
+
* unset(): Replaced falsy-value evaluation with the `in` operator to correctly detect existing fields.
|
|
20
|
+
|
|
17
21
|
*/
|
|
18
22
|
|
|
19
23
|
/**Return true if userName is valid
|
|
@@ -208,6 +212,9 @@ function deepClone(obj) {
|
|
|
208
212
|
if (element[key] instanceof Date) { // Date instance
|
|
209
213
|
element[key] = new Date(element[key]);
|
|
210
214
|
}
|
|
215
|
+
else if (Object.prototype.toString.call(element[key]) === '[object Object]') {
|
|
216
|
+
element[key] = {...element[key]}; // This helps to remove reference to the original object.
|
|
217
|
+
}
|
|
211
218
|
stack.push(element[key]);
|
|
212
219
|
}
|
|
213
220
|
}
|
|
@@ -248,34 +255,25 @@ module.exports.getPaths = getPaths;
|
|
|
248
255
|
|
|
249
256
|
|
|
250
257
|
/** Return an object with sorted fields, ordered by field name ascending.
|
|
258
|
+
*
|
|
259
|
+
* The returned object is independent of the source object.
|
|
251
260
|
*
|
|
252
261
|
* NB. For comparison of objects, please see objCompare() function.
|
|
253
262
|
* @param {object} pObject
|
|
254
263
|
* @returns {object} an object with fields sorted in ascending order of field names.
|
|
255
264
|
*/
|
|
256
265
|
function getSortedObject(pObject) {
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
const
|
|
266
|
+
let cloneObj = deepClone(pObject);
|
|
267
|
+
let idx = 0;
|
|
268
|
+
const paths = getPaths(cloneObj).toSorted();
|
|
269
|
+
sortedObj = {};
|
|
260
270
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
271
|
+
for (idx in paths) {
|
|
272
|
+
const path = paths[idx];
|
|
273
|
+
const value = get(cloneObj, path);
|
|
274
|
+
set(sortedObj, path, value);
|
|
264
275
|
}
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
// Assign the sorted fields to the new object.
|
|
268
|
-
for (let index in paths) {
|
|
269
|
-
const field = paths[index];
|
|
270
|
-
if (Object.prototype.toString.call(objClone[field]) === '[object Object]') {
|
|
271
|
-
sortedObject[field] = getSortedObject(objClone[field]);
|
|
272
|
-
}
|
|
273
|
-
else {
|
|
274
|
-
sortedObject[field] = objClone[field];
|
|
275
|
-
} //
|
|
276
|
-
} // for (let field in paths) {
|
|
277
|
-
|
|
278
|
-
return sortedObject;
|
|
276
|
+
return sortedObj;
|
|
279
277
|
} // function getSortedObject(pObject) {
|
|
280
278
|
module.exports.getSortedObject = getSortedObject;
|
|
281
279
|
|
|
@@ -290,9 +288,10 @@ function get(anObject, path, defaultVal = undefined) {
|
|
|
290
288
|
let tempObj = anObject;
|
|
291
289
|
for (let idx in paths) {
|
|
292
290
|
let key = paths[idx];
|
|
293
|
-
tempObj
|
|
294
|
-
if (!tempObj) // undefined or null
|
|
291
|
+
if (!(key in tempObj)) // key not found.
|
|
295
292
|
return defaultVal;
|
|
293
|
+
|
|
294
|
+
tempObj = tempObj[key];
|
|
296
295
|
}
|
|
297
296
|
return tempObj;
|
|
298
297
|
}
|
|
@@ -330,7 +329,7 @@ function unset(anObject, path) {
|
|
|
330
329
|
let tempObj = anObject;
|
|
331
330
|
for (let idx in paths) {
|
|
332
331
|
let key = paths[idx];
|
|
333
|
-
if (!tempObj)
|
|
332
|
+
if (!(key in tempObj))
|
|
334
333
|
return;
|
|
335
334
|
if (idx < paths.length - 1)
|
|
336
335
|
tempObj = tempObj[key];
|