@contrail/util 1.1.15 → 1.1.16
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/lib/object-util/cloneDeep/cloneDeep.d.ts +5 -1
- package/lib/object-util/cloneDeep/cloneDeep.js +2 -9
- package/lib/object-util/cloneDeepPreserveDates/cloneDeepPreserveDates.d.ts +6 -0
- package/lib/object-util/cloneDeepPreserveDates/cloneDeepPreserveDates.js +37 -0
- package/lib/object-util/object-util.d.ts +2 -0
- package/lib/object-util/object-util.js +2 -0
- package/lib/object-util/retainOnlyProperties/retainOnlyProperties.js +1 -1
- package/package.json +1 -1
|
@@ -1 +1,5 @@
|
|
|
1
|
-
export declare function cloneDeep<T>(obj: T): T | null;
|
|
1
|
+
export declare function cloneDeep<T>(obj: T): DatesAsStrings<T> | null;
|
|
2
|
+
type DatesAsStrings<T> = T extends Date ? string : T extends (infer U)[] ? DatesAsStrings<U>[] : T extends object ? {
|
|
3
|
+
[K in keyof T]: DatesAsStrings<T[K]>;
|
|
4
|
+
} : T;
|
|
5
|
+
export {};
|
|
@@ -2,14 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.cloneDeep = cloneDeep;
|
|
4
4
|
function cloneDeep(obj) {
|
|
5
|
-
if (obj
|
|
5
|
+
if (obj == null)
|
|
6
6
|
return null;
|
|
7
|
-
return JSON.parse(JSON.stringify(obj)
|
|
8
|
-
if (typeof value === 'string' && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/.test(value)) {
|
|
9
|
-
const date = new Date(value);
|
|
10
|
-
if (!isNaN(date.getTime()))
|
|
11
|
-
return date;
|
|
12
|
-
}
|
|
13
|
-
return value;
|
|
14
|
-
});
|
|
7
|
+
return JSON.parse(JSON.stringify(obj));
|
|
15
8
|
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare function cloneDeepPreserveDates<T>(obj: T): T | null;
|
|
2
|
+
declare const DATE_TAG = "__$date";
|
|
3
|
+
export declare function tagDates(input: unknown): unknown;
|
|
4
|
+
export declare function reviveTaggedDates(_key: string, value: unknown): unknown;
|
|
5
|
+
export declare function isTaggedDate(v: unknown): v is Record<typeof DATE_TAG, string>;
|
|
6
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.cloneDeepPreserveDates = cloneDeepPreserveDates;
|
|
4
|
+
exports.tagDates = tagDates;
|
|
5
|
+
exports.reviveTaggedDates = reviveTaggedDates;
|
|
6
|
+
exports.isTaggedDate = isTaggedDate;
|
|
7
|
+
function cloneDeepPreserveDates(obj) {
|
|
8
|
+
if (obj == null)
|
|
9
|
+
return null;
|
|
10
|
+
const tagged = tagDates(obj);
|
|
11
|
+
const json = JSON.stringify(tagged);
|
|
12
|
+
return JSON.parse(json, reviveTaggedDates);
|
|
13
|
+
}
|
|
14
|
+
const DATE_TAG = '__$date';
|
|
15
|
+
function tagDates(input) {
|
|
16
|
+
if (input instanceof Date)
|
|
17
|
+
return { [DATE_TAG]: input.toISOString() };
|
|
18
|
+
if (Array.isArray(input))
|
|
19
|
+
return input.map(tagDates);
|
|
20
|
+
if (input && typeof input === 'object') {
|
|
21
|
+
const out = {};
|
|
22
|
+
for (const [k, v] of Object.entries(input))
|
|
23
|
+
out[k] = tagDates(v);
|
|
24
|
+
return out;
|
|
25
|
+
}
|
|
26
|
+
return input;
|
|
27
|
+
}
|
|
28
|
+
function reviveTaggedDates(_key, value) {
|
|
29
|
+
return isTaggedDate(value) ? new Date(value[DATE_TAG]) : value;
|
|
30
|
+
}
|
|
31
|
+
function isTaggedDate(v) {
|
|
32
|
+
return (!!v &&
|
|
33
|
+
typeof v === 'object' &&
|
|
34
|
+
v !== null &&
|
|
35
|
+
DATE_TAG in v &&
|
|
36
|
+
typeof v[DATE_TAG] === 'string');
|
|
37
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { cloneDeep } from './cloneDeep/cloneDeep';
|
|
2
|
+
import { cloneDeepPreserveDates } from './cloneDeepPreserveDates/cloneDeepPreserveDates';
|
|
2
3
|
import { isObject } from './isObject/isObject';
|
|
3
4
|
import { mergeDeep } from './mergeDeep/mergeDeep';
|
|
4
5
|
import { getObjectDiffs } from './compareDeep/compareDeep';
|
|
@@ -14,6 +15,7 @@ export declare class ObjectUtil {
|
|
|
14
15
|
static isObject: typeof isObject;
|
|
15
16
|
static mergeDeep: typeof mergeDeep;
|
|
16
17
|
static cloneDeep: typeof cloneDeep;
|
|
18
|
+
static cloneDeepPreserveDates: typeof cloneDeepPreserveDates;
|
|
17
19
|
static compareDeep: typeof getObjectDiffs;
|
|
18
20
|
static applyChangesIfEqual: typeof applyChangesIfEqual;
|
|
19
21
|
static determineChangesIfEqual: typeof determineChangesIfEqual;
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ObjectUtil = void 0;
|
|
4
4
|
const cloneDeep_1 = require("./cloneDeep/cloneDeep");
|
|
5
|
+
const cloneDeepPreserveDates_1 = require("./cloneDeepPreserveDates/cloneDeepPreserveDates");
|
|
5
6
|
const isObject_1 = require("./isObject/isObject");
|
|
6
7
|
const mergeDeep_1 = require("./mergeDeep/mergeDeep");
|
|
7
8
|
const compareDeep_1 = require("./compareDeep/compareDeep");
|
|
@@ -80,6 +81,7 @@ exports.ObjectUtil = ObjectUtil;
|
|
|
80
81
|
ObjectUtil.isObject = isObject_1.isObject;
|
|
81
82
|
ObjectUtil.mergeDeep = mergeDeep_1.mergeDeep;
|
|
82
83
|
ObjectUtil.cloneDeep = cloneDeep_1.cloneDeep;
|
|
84
|
+
ObjectUtil.cloneDeepPreserveDates = cloneDeepPreserveDates_1.cloneDeepPreserveDates;
|
|
83
85
|
ObjectUtil.compareDeep = compareDeep_1.getObjectDiffs;
|
|
84
86
|
ObjectUtil.applyChangesIfEqual = applyChangesIfEqual_1.applyChangesIfEqual;
|
|
85
87
|
ObjectUtil.determineChangesIfEqual = applyChangesIfEqual_1.determineChangesIfEqual;
|
|
@@ -25,6 +25,6 @@ function retainOnlyProperties(obj, keysToKeep, options = {}) {
|
|
|
25
25
|
}
|
|
26
26
|
return target;
|
|
27
27
|
};
|
|
28
|
-
const objToModify = options.shouldDeleteInPlace ? obj : object_util_1.ObjectUtil.
|
|
28
|
+
const objToModify = options.shouldDeleteInPlace ? obj : object_util_1.ObjectUtil.cloneDeepPreserveDates(obj);
|
|
29
29
|
return retain(objToModify);
|
|
30
30
|
}
|