@contrail/util 1.1.14-alpha-2 → 1.1.15-alpha-3

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/index.d.ts CHANGED
@@ -4,6 +4,7 @@ export * from './date-util/date-util';
4
4
  export * from './map-util/map-util';
5
5
  export * from './object-util/object-util';
6
6
  export * from './order-util/order-util';
7
+ export * from './performance-util/performance-util';
7
8
  export * from './promise-util/promise-util';
8
9
  export * from './retry-util/retry-util';
9
10
  export * from './string-util/string-util';
package/lib/index.js CHANGED
@@ -20,6 +20,7 @@ __exportStar(require("./date-util/date-util"), exports);
20
20
  __exportStar(require("./map-util/map-util"), exports);
21
21
  __exportStar(require("./object-util/object-util"), exports);
22
22
  __exportStar(require("./order-util/order-util"), exports);
23
+ __exportStar(require("./performance-util/performance-util"), exports);
23
24
  __exportStar(require("./promise-util/promise-util"), exports);
24
25
  __exportStar(require("./retry-util/retry-util"), exports);
25
26
  __exportStar(require("./string-util/string-util"), exports);
@@ -1,32 +1 @@
1
- type PerformanceComparisonEvent = {
2
- durationMsJson: number;
3
- durationMsRFDC: number;
4
- input: any;
5
- };
6
- type PerformanceSummary = {
7
- eventForWhichJsonWasMostFaster: PerformanceComparisonEvent | null;
8
- eventForWhichRFDCWasMostFaster: PerformanceComparisonEvent | null;
9
- totalDurationMsJson: number;
10
- totalDurationMsRFDC: number;
11
- invocationsInWhichJsonWasFaster: PerformanceComparisonEvent[];
12
- invocationsInWhichRFDCWasFaster: PerformanceComparisonEvent[];
13
- };
14
- type CloneMethods = {
15
- cloneRfdc: <T>(obj: T) => T;
16
- cloneJson: <T>(obj: T) => T;
17
- withTiming: (fn: () => any) => {
18
- result: any;
19
- durationMs: number;
20
- };
21
- };
22
- declare global {
23
- interface Window {
24
- performanceSummary: PerformanceSummary;
25
- performanceEvents: PerformanceComparisonEvent[];
26
- cloneMethods: CloneMethods;
27
- }
28
- }
29
- export declare function cloneDeep<T>(obj: T, options?: {
30
- shouldPreserveCircularReferences?: boolean;
31
- }): T;
32
- export {};
1
+ export declare function cloneDeep<T>(obj: T): T | null;
@@ -1,68 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.cloneDeep = cloneDeep;
4
- const cloneDefault = require('rfdc')();
5
- const cloneWithCircles = require('rfdc')({ circles: true });
6
- function cloneRfdc(obj, options) {
7
- var _a;
8
- const shouldPreserveCircles = (_a = options === null || options === void 0 ? void 0 : options.shouldPreserveCircularReferences) !== null && _a !== void 0 ? _a : false;
9
- const clone = shouldPreserveCircles ? cloneWithCircles : cloneDefault;
10
- return clone(obj);
11
- }
12
- function withTiming(fn) {
13
- const start = performance.now();
14
- const result = fn();
15
- const durationMs = performance.now() - start;
16
- return { result, durationMs };
17
- }
18
- if (typeof window === 'undefined') {
19
- var window = {};
20
- }
21
- window.cloneMethods = {
22
- cloneRfdc,
23
- cloneJson: (obj) => JSON.parse(JSON.stringify(obj)),
24
- withTiming,
25
- };
26
- function cloneDeep(obj, options) {
27
- var _a;
28
- const shouldPreserveCircles = (_a = options === null || options === void 0 ? void 0 : options.shouldPreserveCircularReferences) !== null && _a !== void 0 ? _a : false;
29
- const clone = shouldPreserveCircles ? cloneWithCircles : cloneDefault;
30
- const { durationMs: durationMsJson } = withTiming(() => JSON.parse(JSON.stringify(obj)));
31
- const { durationMs: durationMsRFDC } = withTiming(() => clone(obj));
32
- const rfdcClone = clone(obj);
33
- if (!window.performanceEvents) {
34
- window.performanceEvents = [];
35
- }
36
- const performanceEvent = {
37
- durationMsJson,
38
- durationMsRFDC,
39
- input: obj,
40
- };
41
- window.performanceEvents.push(performanceEvent);
42
- window.performanceSummary = window.performanceSummary || {
43
- eventForWhichJsonWasMostFaster: null,
44
- eventForWhichRFDCWasMostFaster: null,
45
- totalDurationMsJson: 0,
46
- totalDurationMsRFDC: 0,
47
- invocationsInWhichJsonWasFaster: [],
48
- invocationsInWhichRFDCWasFaster: [],
49
- };
50
- window.performanceSummary.totalDurationMsJson += durationMsJson;
51
- window.performanceSummary.totalDurationMsRFDC += durationMsRFDC;
52
- const isJsonFaster = durationMsJson < durationMsRFDC;
53
- if (isJsonFaster) {
54
- window.performanceSummary.invocationsInWhichJsonWasFaster.push(performanceEvent);
55
- if (!window.performanceSummary.eventForWhichJsonWasMostFaster ||
56
- durationMsJson < window.performanceSummary.eventForWhichJsonWasMostFaster.durationMsJson) {
57
- window.performanceSummary.eventForWhichJsonWasMostFaster = performanceEvent;
58
- }
59
- }
60
- else {
61
- window.performanceSummary.invocationsInWhichRFDCWasFaster.push(performanceEvent);
62
- if (!window.performanceSummary.eventForWhichRFDCWasMostFaster ||
63
- durationMsRFDC < window.performanceSummary.eventForWhichRFDCWasMostFaster.durationMsRFDC) {
64
- window.performanceSummary.eventForWhichRFDCWasMostFaster = performanceEvent;
4
+ function cloneDeep(obj) {
5
+ if (obj === null || obj === undefined)
6
+ return null;
7
+ return JSON.parse(JSON.stringify(obj), (_key, value) => {
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;
65
12
  }
66
- }
67
- return rfdcClone;
13
+ return value;
14
+ });
68
15
  }
@@ -0,0 +1,17 @@
1
+ type TimingSpans = TimingNode[];
2
+ type TimingStack = TimingNode[];
3
+ type TimingNode = {
4
+ label: string;
5
+ durationMs: number;
6
+ children: TimingNode[];
7
+ };
8
+ type TimingProfile = {
9
+ timingSpans: TimingSpans;
10
+ timingStack: TimingStack;
11
+ };
12
+ export declare function getDefaultTimingProfile(): TimingProfile;
13
+ export declare function clearDefaultTimingProfile(): void;
14
+ export declare function withTimingAsync<T>(label: string, fn: () => Promise<T>): Promise<T>;
15
+ export declare function withTimingSync<T>(label: string, fn: () => T): T;
16
+ export declare function displayTimingTree(nodes?: TimingSpans, depth?: number): string;
17
+ export {};
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.getDefaultTimingProfile = getDefaultTimingProfile;
13
+ exports.clearDefaultTimingProfile = clearDefaultTimingProfile;
14
+ exports.withTimingAsync = withTimingAsync;
15
+ exports.withTimingSync = withTimingSync;
16
+ exports.displayTimingTree = displayTimingTree;
17
+ const DEFAULT_TIMING_PROFILE = {
18
+ timingSpans: [],
19
+ timingStack: [],
20
+ };
21
+ function getDefaultTimingProfile() {
22
+ return DEFAULT_TIMING_PROFILE;
23
+ }
24
+ function getTimingStack() {
25
+ return DEFAULT_TIMING_PROFILE.timingStack;
26
+ }
27
+ function getTimingSpans() {
28
+ return DEFAULT_TIMING_PROFILE.timingSpans;
29
+ }
30
+ function clearDefaultTimingProfile() {
31
+ DEFAULT_TIMING_PROFILE.timingSpans = [];
32
+ DEFAULT_TIMING_PROFILE.timingStack = [];
33
+ }
34
+ function withTimingAsync(label, fn) {
35
+ return __awaiter(this, void 0, void 0, function* () {
36
+ const start = performance.now();
37
+ const timingNode = { label, durationMs: 0, children: [] };
38
+ const parent = getTimingStack().length > 0 ? getTimingStack()[getTimingStack().length - 1] : null;
39
+ if (parent) {
40
+ parent.children.push(timingNode);
41
+ }
42
+ else {
43
+ getTimingSpans().push(timingNode);
44
+ }
45
+ getTimingStack().push(timingNode);
46
+ return fn().finally(() => {
47
+ const end = performance.now();
48
+ timingNode.durationMs = end - start;
49
+ getTimingStack().pop();
50
+ });
51
+ });
52
+ }
53
+ function withTimingSync(label, fn) {
54
+ const start = performance.now();
55
+ const timingNode = { label, durationMs: 0, children: [] };
56
+ const parent = getTimingStack().length > 0 ? getTimingStack()[getTimingStack().length - 1] : null;
57
+ if (parent) {
58
+ parent.children.push(timingNode);
59
+ }
60
+ else {
61
+ getTimingSpans().push(timingNode);
62
+ }
63
+ getTimingStack().push(timingNode);
64
+ try {
65
+ return fn();
66
+ }
67
+ finally {
68
+ const end = performance.now();
69
+ timingNode.durationMs = end - start;
70
+ getTimingStack().pop();
71
+ }
72
+ }
73
+ function displayTimingTree(nodes = DEFAULT_TIMING_PROFILE.timingSpans, depth = 0) {
74
+ const lines = [];
75
+ const indent = ' '.repeat(depth);
76
+ const totalWidth = 80;
77
+ const timeWidth = 10;
78
+ nodes.forEach((node) => {
79
+ const timeStr = `${node.durationMs.toFixed(2)}ms`;
80
+ const labelWithIndent = `${indent}${node.label}`;
81
+ const availableWidth = totalWidth - timeWidth - labelWithIndent.length;
82
+ const dots = '.'.repeat(Math.max(2, availableWidth));
83
+ lines.push(`${labelWithIndent}${dots}${timeStr.padStart(timeWidth)}`);
84
+ if (node.children && node.children.length > 0) {
85
+ lines.push(displayTimingTree(node.children, depth + 1));
86
+ }
87
+ });
88
+ const result = lines.join('\n');
89
+ if (depth === 0) {
90
+ console.log('\n--- Performance Timing Tree ---');
91
+ console.log(result);
92
+ console.log('--- End Timing ---\n');
93
+ }
94
+ return result;
95
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@contrail/util",
3
- "version": "1.1.14-alpha-2",
4
- "description": "General javascript utilities",
3
+ "version": "1.1.15-alpha-3",
4
+ "description": "General JavaScript utilities",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
7
7
  "scripts": {
@@ -41,7 +41,6 @@
41
41
  "dependencies": {
42
42
  "@contrail/types": "^3.0.95",
43
43
  "fflate": "^0.8.2",
44
- "lodash": "^4.17.21",
45
- "rfdc": "^1.4.1"
44
+ "lodash": "^4.17.21"
46
45
  }
47
46
  }