@contrail/util 1.2.0-alpha-otel-2 → 1.2.0-dev-flatten-object-2
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/flattenObject/flattenObject.d.ts +4 -0
- package/lib/object-util/flattenObject/flattenObject.js +60 -0
- package/lib/object-util/object-util.d.ts +2 -0
- package/lib/object-util/object-util.js +2 -0
- package/lib/string-util/string-util.d.ts +4 -4
- package/lib/string-util/string-util.js +2 -2
- package/package.json +1 -1
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.flattenObject = flattenObject;
|
|
4
|
+
const MAX_DEPTH = 10;
|
|
5
|
+
function flattenObject(obj) {
|
|
6
|
+
const seen = new WeakSet();
|
|
7
|
+
return flattenRecursive(obj, '', seen, 0);
|
|
8
|
+
}
|
|
9
|
+
function flattenRecursive(obj, prefix, ancestors, depth) {
|
|
10
|
+
const result = {};
|
|
11
|
+
if (depth > MAX_DEPTH) {
|
|
12
|
+
result[prefix || 'value'] = '[max depth exceeded]';
|
|
13
|
+
return result;
|
|
14
|
+
}
|
|
15
|
+
ancestors.add(obj);
|
|
16
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
17
|
+
const fullKey = prefix ? `${prefix}.${key}` : key;
|
|
18
|
+
if (value === null || value === undefined) {
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
if (typeof value === 'object') {
|
|
22
|
+
if (ancestors.has(value)) {
|
|
23
|
+
result[fullKey] = '[circular reference]';
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (Array.isArray(value)) {
|
|
27
|
+
result[fullKey] = JSON.stringify(value);
|
|
28
|
+
}
|
|
29
|
+
else if (value instanceof Date) {
|
|
30
|
+
result[fullKey] = value.toISOString();
|
|
31
|
+
}
|
|
32
|
+
else if (value instanceof Error) {
|
|
33
|
+
result[fullKey] = value.message;
|
|
34
|
+
if (value.stack) {
|
|
35
|
+
result[`${fullKey}.stack`] = value.stack;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
Object.assign(result, flattenRecursive(value, fullKey, ancestors, depth + 1));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
else if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
|
43
|
+
result[fullKey] = value;
|
|
44
|
+
}
|
|
45
|
+
else if (typeof value === 'bigint') {
|
|
46
|
+
result[fullKey] = value.toString();
|
|
47
|
+
}
|
|
48
|
+
else if (typeof value === 'symbol') {
|
|
49
|
+
result[fullKey] = value.toString();
|
|
50
|
+
}
|
|
51
|
+
else if (typeof value === 'function') {
|
|
52
|
+
result[fullKey] = '[function]';
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
result[fullKey] = String(value);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
ancestors.delete(obj);
|
|
59
|
+
return result;
|
|
60
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { cloneDeep } from './cloneDeep/cloneDeep';
|
|
2
2
|
import { cloneDeepPreserveDates } from './cloneDeepPreserveDates/cloneDeepPreserveDates';
|
|
3
|
+
import { flattenObject } from './flattenObject/flattenObject';
|
|
3
4
|
import { isObject } from './isObject/isObject';
|
|
4
5
|
import { mergeDeep } from './mergeDeep/mergeDeep';
|
|
5
6
|
import { getObjectDiffs } from './compareDeep/compareDeep';
|
|
@@ -21,4 +22,5 @@ export declare class ObjectUtil {
|
|
|
21
22
|
static determineChangesIfEqual: typeof determineChangesIfEqual;
|
|
22
23
|
static areItemPropertyValuesEqual: typeof areItemPropertyValuesEqual;
|
|
23
24
|
static retainOnlyProperties: typeof retainOnlyProperties;
|
|
25
|
+
static flattenObject: typeof flattenObject;
|
|
24
26
|
}
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ObjectUtil = void 0;
|
|
4
4
|
const cloneDeep_1 = require("./cloneDeep/cloneDeep");
|
|
5
5
|
const cloneDeepPreserveDates_1 = require("./cloneDeepPreserveDates/cloneDeepPreserveDates");
|
|
6
|
+
const flattenObject_1 = require("./flattenObject/flattenObject");
|
|
6
7
|
const isObject_1 = require("./isObject/isObject");
|
|
7
8
|
const mergeDeep_1 = require("./mergeDeep/mergeDeep");
|
|
8
9
|
const compareDeep_1 = require("./compareDeep/compareDeep");
|
|
@@ -87,3 +88,4 @@ ObjectUtil.applyChangesIfEqual = applyChangesIfEqual_1.applyChangesIfEqual;
|
|
|
87
88
|
ObjectUtil.determineChangesIfEqual = applyChangesIfEqual_1.determineChangesIfEqual;
|
|
88
89
|
ObjectUtil.areItemPropertyValuesEqual = applyChangesIfEqual_1.areItemPropertyValuesEqual;
|
|
89
90
|
ObjectUtil.retainOnlyProperties = retainOnlyProperties_1.retainOnlyProperties;
|
|
91
|
+
ObjectUtil.flattenObject = flattenObject_1.flattenObject;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
export declare class StringUtil {
|
|
2
2
|
static convertToHyphenCase(entityName: string, transformToLowerCase?: boolean): string;
|
|
3
3
|
static parseVariables(variableString: string, data: any, quoteStrings?: boolean): string;
|
|
4
|
-
static convertToCamelCase(str:
|
|
5
|
-
static convertToTitleCase(str:
|
|
6
|
-
static isString(value:
|
|
7
|
-
static isArray(value:
|
|
4
|
+
static convertToCamelCase(str: any): any;
|
|
5
|
+
static convertToTitleCase(str: any): any;
|
|
6
|
+
static isString(value: any): value is string | String;
|
|
7
|
+
static isArray(value: any): value is any[];
|
|
8
8
|
static decodeEncodedString(encodedString: string): string;
|
|
9
9
|
static getStringSizeInMB(str: string): number;
|
|
10
10
|
}
|
|
@@ -14,7 +14,7 @@ class StringUtil {
|
|
|
14
14
|
if (!variableString) {
|
|
15
15
|
return variableString;
|
|
16
16
|
}
|
|
17
|
-
const replacedString = variableString.replace(regExp, (
|
|
17
|
+
const replacedString = variableString.replace(regExp, (match, group) => {
|
|
18
18
|
const variableLevels = group.split('.');
|
|
19
19
|
let replacementValue = data;
|
|
20
20
|
for (let i = 0; i < variableLevels.length; i++) {
|
|
@@ -62,7 +62,7 @@ class StringUtil {
|
|
|
62
62
|
return withSpaces.charAt(0).toUpperCase() + withSpaces.slice(1);
|
|
63
63
|
}
|
|
64
64
|
static isString(value) {
|
|
65
|
-
return typeof value === 'string';
|
|
65
|
+
return typeof value === 'string' || value instanceof String;
|
|
66
66
|
}
|
|
67
67
|
static isArray(value) {
|
|
68
68
|
return Array.isArray(value);
|