@contrail/util 1.2.0-dev-flatten-object-1 → 1.2.0

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 ADDED
@@ -0,0 +1,17 @@
1
+ # Changelog
2
+
3
+ ## 1.2.0 (2026-03-09)
4
+
5
+ ### Added
6
+
7
+ - `ObjectUtil.flattenObject` — Recursively flattens a nested object into a single-level object with dot-notation keys. Designed for use with structured logging (OpenTelemetry log attributes).
8
+ - Converts nested objects to dot-separated keys (e.g. `user.profile.name`)
9
+ - JSON-stringifies arrays
10
+ - Converts `Date` to ISO string, extracts `Error` message and stack
11
+ - Handles circular references and enforces a max depth of 10
12
+ - Converts `bigint`, `symbol`, and `function` values to strings
13
+ - Skips `null` and `undefined` values
14
+
15
+ ## 1.1.19
16
+
17
+ - Previous stable release
@@ -6,20 +6,20 @@ function flattenObject(obj) {
6
6
  const seen = new WeakSet();
7
7
  return flattenRecursive(obj, '', seen, 0);
8
8
  }
9
- function flattenRecursive(obj, prefix, seen, depth) {
9
+ function flattenRecursive(obj, prefix, ancestors, depth) {
10
10
  const result = {};
11
11
  if (depth > MAX_DEPTH) {
12
12
  result[prefix || 'value'] = '[max depth exceeded]';
13
13
  return result;
14
14
  }
15
- seen.add(obj);
15
+ ancestors.add(obj);
16
16
  for (const [key, value] of Object.entries(obj)) {
17
17
  const fullKey = prefix ? `${prefix}.${key}` : key;
18
18
  if (value === null || value === undefined) {
19
19
  continue;
20
20
  }
21
21
  if (typeof value === 'object') {
22
- if (seen.has(value)) {
22
+ if (ancestors.has(value)) {
23
23
  result[fullKey] = '[circular reference]';
24
24
  continue;
25
25
  }
@@ -36,7 +36,7 @@ function flattenRecursive(obj, prefix, seen, depth) {
36
36
  }
37
37
  }
38
38
  else {
39
- Object.assign(result, flattenRecursive(value, fullKey, seen, depth + 1));
39
+ Object.assign(result, flattenRecursive(value, fullKey, ancestors, depth + 1));
40
40
  }
41
41
  }
42
42
  else if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
@@ -55,5 +55,6 @@ function flattenRecursive(obj, prefix, seen, depth) {
55
55
  result[fullKey] = String(value);
56
56
  }
57
57
  }
58
+ ancestors.delete(obj);
58
59
  return result;
59
60
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/util",
3
- "version": "1.2.0-dev-flatten-object-1",
3
+ "version": "1.2.0",
4
4
  "description": "General JavaScript utilities",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",