@contrail/util 1.0.48 → 1.0.50

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.
Files changed (30) hide show
  1. package/README.md +1 -1
  2. package/lib/app-util/app-util.d.ts +25 -25
  3. package/lib/app-util/app-util.js +72 -72
  4. package/lib/date-util/date-util.d.ts +4 -4
  5. package/lib/date-util/date-util.js +8 -8
  6. package/lib/date-util/getIsoDatePart/getIsoDatePart.d.ts +1 -1
  7. package/lib/date-util/getIsoDatePart/getIsoDatePart.js +17 -17
  8. package/lib/index.d.ts +6 -5
  9. package/lib/index.js +22 -21
  10. package/lib/map-util/map-util.d.ts +3 -3
  11. package/lib/map-util/map-util.js +18 -18
  12. package/lib/object-util/applyChangesIfEqual/applyChangesIfEqual.d.ts +4 -4
  13. package/lib/object-util/applyChangesIfEqual/applyChangesIfEqual.js +84 -84
  14. package/lib/object-util/cloneDeep/cloneDeep.d.ts +1 -1
  15. package/lib/object-util/cloneDeep/cloneDeep.js +10 -10
  16. package/lib/object-util/compareDeep/compareDeep.d.ts +7 -7
  17. package/lib/object-util/compareDeep/compareDeep.js +96 -96
  18. package/lib/object-util/compareDeep/mockData.d.ts +88 -88
  19. package/lib/object-util/compareDeep/mockData.js +91 -91
  20. package/lib/object-util/isObject/isObject.d.ts +1 -1
  21. package/lib/object-util/isObject/isObject.js +7 -7
  22. package/lib/object-util/mergeDeep/mergeDeep.d.ts +1 -1
  23. package/lib/object-util/mergeDeep/mergeDeep.js +26 -26
  24. package/lib/object-util/object-util.d.ts +17 -17
  25. package/lib/object-util/object-util.js +41 -41
  26. package/lib/string-util/string-util.d.ts +8 -8
  27. package/lib/string-util/string-util.js +71 -71
  28. package/lib/timer-util/timer-util.d.ts +16 -0
  29. package/lib/timer-util/timer-util.js +65 -0
  30. package/package.json +43 -43
@@ -1,41 +1,41 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ObjectUtil = void 0;
4
- const cloneDeep_1 = require("./cloneDeep/cloneDeep");
5
- const isObject_1 = require("./isObject/isObject");
6
- const mergeDeep_1 = require("./mergeDeep/mergeDeep");
7
- const compareDeep_1 = require("./compareDeep/compareDeep");
8
- const applyChangesIfEqual_1 = require("./applyChangesIfEqual/applyChangesIfEqual");
9
- class ObjectUtil {
10
- static getByPath(obj, path, def = null) {
11
- const pathUnits = path.replace(/\[/g, '.').replace(/]/g, '').split('.');
12
- pathUnits.forEach((level) => {
13
- if (obj) {
14
- obj = obj[level];
15
- }
16
- });
17
- if (obj === undefined) {
18
- return def;
19
- }
20
- return obj;
21
- }
22
- static setByPath(obj, path, value) {
23
- const a = path.split('.');
24
- let o = obj;
25
- while (a.length - 1) {
26
- const n = a.shift();
27
- if (!(n in o))
28
- o[n] = {};
29
- o = o[n];
30
- }
31
- o[a[0]] = value;
32
- }
33
- }
34
- exports.ObjectUtil = ObjectUtil;
35
- ObjectUtil.isObject = isObject_1.isObject;
36
- ObjectUtil.mergeDeep = mergeDeep_1.mergeDeep;
37
- ObjectUtil.cloneDeep = cloneDeep_1.cloneDeep;
38
- ObjectUtil.compareDeep = compareDeep_1.getObjectDiffs;
39
- ObjectUtil.applyChangesIfEqual = applyChangesIfEqual_1.applyChangesIfEqual;
40
- ObjectUtil.determineChangesIfEqual = applyChangesIfEqual_1.determineChangesIfEqual;
41
- ObjectUtil.areItemPropertyValuesEqual = applyChangesIfEqual_1.areItemPropertyValuesEqual;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ObjectUtil = void 0;
4
+ const cloneDeep_1 = require("./cloneDeep/cloneDeep");
5
+ const isObject_1 = require("./isObject/isObject");
6
+ const mergeDeep_1 = require("./mergeDeep/mergeDeep");
7
+ const compareDeep_1 = require("./compareDeep/compareDeep");
8
+ const applyChangesIfEqual_1 = require("./applyChangesIfEqual/applyChangesIfEqual");
9
+ class ObjectUtil {
10
+ static getByPath(obj, path, def = null) {
11
+ const pathUnits = path.replace(/\[/g, '.').replace(/]/g, '').split('.');
12
+ pathUnits.forEach((level) => {
13
+ if (obj) {
14
+ obj = obj[level];
15
+ }
16
+ });
17
+ if (obj === undefined) {
18
+ return def;
19
+ }
20
+ return obj;
21
+ }
22
+ static setByPath(obj, path, value) {
23
+ const a = path.split('.');
24
+ let o = obj;
25
+ while (a.length - 1) {
26
+ const n = a.shift();
27
+ if (!(n in o))
28
+ o[n] = {};
29
+ o = o[n];
30
+ }
31
+ o[a[0]] = value;
32
+ }
33
+ }
34
+ exports.ObjectUtil = ObjectUtil;
35
+ ObjectUtil.isObject = isObject_1.isObject;
36
+ ObjectUtil.mergeDeep = mergeDeep_1.mergeDeep;
37
+ ObjectUtil.cloneDeep = cloneDeep_1.cloneDeep;
38
+ ObjectUtil.compareDeep = compareDeep_1.getObjectDiffs;
39
+ ObjectUtil.applyChangesIfEqual = applyChangesIfEqual_1.applyChangesIfEqual;
40
+ ObjectUtil.determineChangesIfEqual = applyChangesIfEqual_1.determineChangesIfEqual;
41
+ ObjectUtil.areItemPropertyValuesEqual = applyChangesIfEqual_1.areItemPropertyValuesEqual;
@@ -1,8 +1,8 @@
1
- export declare class StringUtil {
2
- static convertToHyphenCase(entityName: string, transformToLowerCase?: boolean): string;
3
- static parseVariables(variableString: string, data: any, quoteStrings?: boolean): string;
4
- static convertToCamelCase(str: any): any;
5
- static convertToTitleCase(str: any): any;
6
- static isString(value: any): boolean;
7
- static isArray(value: any): boolean;
8
- }
1
+ export declare class StringUtil {
2
+ static convertToHyphenCase(entityName: string, transformToLowerCase?: boolean): string;
3
+ static parseVariables(variableString: string, data: any, quoteStrings?: boolean): string;
4
+ static convertToCamelCase(str: any): any;
5
+ static convertToTitleCase(str: any): any;
6
+ static isString(value: any): boolean;
7
+ static isArray(value: any): boolean;
8
+ }
@@ -1,71 +1,71 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StringUtil = void 0;
4
- class StringUtil {
5
- static convertToHyphenCase(entityName, transformToLowerCase = true) {
6
- const slugName = entityName === null || entityName === void 0 ? void 0 : entityName.split(/(?=[A-Z])/).join('-');
7
- if (slugName && transformToLowerCase) {
8
- return slugName.toLowerCase();
9
- }
10
- return slugName;
11
- }
12
- static parseVariables(variableString, data, quoteStrings = false) {
13
- const regExp = /\{([^}]+)\}/g;
14
- if (!variableString) {
15
- return variableString;
16
- }
17
- const replacedString = variableString.replace(regExp, (match, group) => {
18
- const variableLevels = group.split('.');
19
- let replacementValue = data;
20
- for (let i = 0; i < variableLevels.length; i++) {
21
- if (replacementValue) {
22
- replacementValue = replacementValue[variableLevels[i]];
23
- }
24
- else {
25
- replacementValue = undefined;
26
- }
27
- }
28
- if (quoteStrings && StringUtil.isString(replacementValue)) {
29
- replacementValue = JSON.stringify(replacementValue);
30
- }
31
- if (StringUtil.isArray(replacementValue)) {
32
- if (quoteStrings) {
33
- const newArray = [];
34
- for (const val of replacementValue) {
35
- newArray.push(JSON.stringify(val));
36
- }
37
- replacementValue = newArray;
38
- }
39
- replacementValue = '[' + replacementValue + ']';
40
- }
41
- if (Object.prototype.toString.call(replacementValue) === '[object Object]') {
42
- replacementValue = JSON.stringify(replacementValue);
43
- }
44
- return replacementValue;
45
- });
46
- return replacedString;
47
- }
48
- static convertToCamelCase(str) {
49
- if (!str) {
50
- return str;
51
- }
52
- str = str.replace(/-/g, ' ');
53
- return str
54
- .replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
55
- return index === 0 ? word.toLowerCase() : word.toUpperCase();
56
- })
57
- .replace(/\s+/g, '');
58
- }
59
- static convertToTitleCase(str) {
60
- const camelCase = this.convertToCamelCase(str);
61
- const withSpaces = camelCase.replace(/([A-Z])/g, ' $1');
62
- return withSpaces.charAt(0).toUpperCase() + withSpaces.slice(1);
63
- }
64
- static isString(value) {
65
- return typeof value === 'string' || value instanceof String;
66
- }
67
- static isArray(value) {
68
- return Array.isArray(value);
69
- }
70
- }
71
- exports.StringUtil = StringUtil;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StringUtil = void 0;
4
+ class StringUtil {
5
+ static convertToHyphenCase(entityName, transformToLowerCase = true) {
6
+ const slugName = entityName === null || entityName === void 0 ? void 0 : entityName.split(/(?=[A-Z])/).join('-');
7
+ if (slugName && transformToLowerCase) {
8
+ return slugName.toLowerCase();
9
+ }
10
+ return slugName;
11
+ }
12
+ static parseVariables(variableString, data, quoteStrings = false) {
13
+ const regExp = /\{([^}]+)\}/g;
14
+ if (!variableString) {
15
+ return variableString;
16
+ }
17
+ const replacedString = variableString.replace(regExp, (match, group) => {
18
+ const variableLevels = group.split('.');
19
+ let replacementValue = data;
20
+ for (let i = 0; i < variableLevels.length; i++) {
21
+ if (replacementValue) {
22
+ replacementValue = replacementValue[variableLevels[i]];
23
+ }
24
+ else {
25
+ replacementValue = undefined;
26
+ }
27
+ }
28
+ if (quoteStrings && StringUtil.isString(replacementValue)) {
29
+ replacementValue = JSON.stringify(replacementValue);
30
+ }
31
+ if (StringUtil.isArray(replacementValue)) {
32
+ if (quoteStrings) {
33
+ const newArray = [];
34
+ for (const val of replacementValue) {
35
+ newArray.push(JSON.stringify(val));
36
+ }
37
+ replacementValue = newArray;
38
+ }
39
+ replacementValue = '[' + replacementValue + ']';
40
+ }
41
+ if (Object.prototype.toString.call(replacementValue) === '[object Object]') {
42
+ replacementValue = JSON.stringify(replacementValue);
43
+ }
44
+ return replacementValue;
45
+ });
46
+ return replacedString;
47
+ }
48
+ static convertToCamelCase(str) {
49
+ if (!str) {
50
+ return str;
51
+ }
52
+ str = str.replace(/-/g, ' ');
53
+ return str
54
+ .replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
55
+ return index === 0 ? word.toLowerCase() : word.toUpperCase();
56
+ })
57
+ .replace(/\s+/g, '');
58
+ }
59
+ static convertToTitleCase(str) {
60
+ const camelCase = this.convertToCamelCase(str);
61
+ const withSpaces = camelCase.replace(/([A-Z])/g, ' $1');
62
+ return withSpaces.charAt(0).toUpperCase() + withSpaces.slice(1);
63
+ }
64
+ static isString(value) {
65
+ return typeof value === 'string' || value instanceof String;
66
+ }
67
+ static isArray(value) {
68
+ return Array.isArray(value);
69
+ }
70
+ }
71
+ exports.StringUtil = StringUtil;
@@ -0,0 +1,16 @@
1
+ export declare enum TimeUnit {
2
+ MILLISECOND = "ms",
3
+ SECOND = "s"
4
+ }
5
+ export declare class Timer {
6
+ private startTime?;
7
+ private endTime?;
8
+ start(): void;
9
+ stop(): void;
10
+ reset(): void;
11
+ getElapsedSeconds(): number;
12
+ getElapsedMilliseconds(): number;
13
+ getElapsedTime(timeUnit?: TimeUnit): number;
14
+ private getCurrentTime;
15
+ }
16
+ export declare function delay(ms: number): Promise<unknown>;
@@ -0,0 +1,65 @@
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.delay = exports.Timer = exports.TimeUnit = void 0;
13
+ var TimeUnit;
14
+ (function (TimeUnit) {
15
+ TimeUnit["MILLISECOND"] = "ms";
16
+ TimeUnit["SECOND"] = "s";
17
+ })(TimeUnit = exports.TimeUnit || (exports.TimeUnit = {}));
18
+ const MILLISECONDS_PER_TIME_UNIT = {
19
+ [TimeUnit.MILLISECOND]: 1,
20
+ [TimeUnit.SECOND]: 1000,
21
+ };
22
+ class Timer {
23
+ start() {
24
+ if (this.startTime) {
25
+ throw new Error('Timer already started');
26
+ }
27
+ this.startTime = this.getCurrentTime();
28
+ }
29
+ stop() {
30
+ if (!this.startTime) {
31
+ throw new Error('Timer not started');
32
+ }
33
+ this.endTime = this.getCurrentTime();
34
+ }
35
+ reset() {
36
+ this.startTime = undefined;
37
+ this.endTime = undefined;
38
+ }
39
+ getElapsedSeconds() {
40
+ return this.getElapsedTime(TimeUnit.SECOND);
41
+ }
42
+ getElapsedMilliseconds() {
43
+ return this.getElapsedTime();
44
+ }
45
+ getElapsedTime(timeUnit = TimeUnit.MILLISECOND) {
46
+ if (!this.startTime) {
47
+ throw new Error('Timer not started');
48
+ }
49
+ const endTime = this.endTime || this.getCurrentTime();
50
+ const duration = endTime - this.startTime;
51
+ return duration / MILLISECONDS_PER_TIME_UNIT[timeUnit];
52
+ }
53
+ getCurrentTime() {
54
+ return performance.now();
55
+ }
56
+ }
57
+ exports.Timer = Timer;
58
+ function delay(ms) {
59
+ return __awaiter(this, void 0, void 0, function* () {
60
+ return new Promise((resolve) => {
61
+ setTimeout(resolve, ms);
62
+ });
63
+ });
64
+ }
65
+ exports.delay = delay;
package/package.json CHANGED
@@ -1,43 +1,43 @@
1
- {
2
- "name": "@contrail/util",
3
- "version": "1.0.48",
4
- "description": "General javascript utilities",
5
- "main": "lib/index.js",
6
- "types": "lib/index.d.ts",
7
- "scripts": {
8
- "build": "tsc",
9
- "format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
10
- "format-check": "prettier --check \"src/**/*.ts\" \"src/**/*.js\"",
11
- "lint": "tslint -p tsconfig.json",
12
- "test": "jest"
13
- },
14
- "keywords": [],
15
- "author": "",
16
- "license": "ISC",
17
- "devDependencies": {
18
- "@types/jest": "^23.3.14",
19
- "jest": "^29.3.1",
20
- "prettier": "^3.2.5",
21
- "ts-jest": "^29.0.3",
22
- "tslint": "^5.11.0",
23
- "tslint-config-prettier": "^1.18.0",
24
- "typescript": "^4.0.0"
25
- },
26
- "jest": {
27
- "moduleFileExtensions": [
28
- "js",
29
- "json",
30
- "ts"
31
- ],
32
- "rootDir": "src",
33
- "testRegex": ".spec.ts$",
34
- "transform": {
35
- "^.+\\.(t|j)s$": "ts-jest"
36
- },
37
- "coverageDirectory": "../coverage",
38
- "testEnvironment": "node"
39
- },
40
- "dependencies": {
41
- "@contrail/types": "^3.0.51"
42
- }
43
- }
1
+ {
2
+ "name": "@contrail/util",
3
+ "version": "1.0.50",
4
+ "description": "General javascript utilities",
5
+ "main": "lib/index.js",
6
+ "types": "lib/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc",
9
+ "format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
10
+ "format-check": "prettier --check \"src/**/*.ts\" \"src/**/*.js\"",
11
+ "lint": "tslint -p tsconfig.json",
12
+ "test": "jest"
13
+ },
14
+ "keywords": [],
15
+ "author": "",
16
+ "license": "ISC",
17
+ "devDependencies": {
18
+ "@types/jest": "^23.3.14",
19
+ "jest": "^29.3.1",
20
+ "prettier": "^3.2.5",
21
+ "ts-jest": "^29.0.3",
22
+ "tslint": "^5.11.0",
23
+ "tslint-config-prettier": "^1.18.0",
24
+ "typescript": "^4.0.0"
25
+ },
26
+ "jest": {
27
+ "moduleFileExtensions": [
28
+ "js",
29
+ "json",
30
+ "ts"
31
+ ],
32
+ "rootDir": "src",
33
+ "testRegex": ".spec.ts$",
34
+ "transform": {
35
+ "^.+\\.(t|j)s$": "ts-jest"
36
+ },
37
+ "coverageDirectory": "../coverage",
38
+ "testEnvironment": "node"
39
+ },
40
+ "dependencies": {
41
+ "@contrail/types": "^3.0.51"
42
+ }
43
+ }