@onecx/accelerator 7.0.5 → 7.1.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onecx/accelerator",
3
- "version": "7.0.5",
3
+ "version": "7.1.0",
4
4
  "license": "Apache-2.0",
5
5
  "repository": {
6
6
  "type": "git",
@@ -10,7 +10,7 @@
10
10
  "tslib": "^2.6.3",
11
11
  "rxjs": "^7.8.1",
12
12
  "@nx/devkit": "^22.0.2",
13
- "@onecx/nx-migration-utils": "^7.0.5"
13
+ "@onecx/nx-migration-utils": "^7.1.0"
14
14
  },
15
15
  "type": "commonjs",
16
16
  "main": "./src/index.js",
package/src/index.d.ts CHANGED
@@ -9,3 +9,4 @@ export * from './lib/utils/is-test.utils';
9
9
  export * from './lib/utils/normalize-locales.utils';
10
10
  export * from './lib/utils/get-normalized-browser-locales.utils';
11
11
  export * from './lib/utils/gatherer';
12
+ export * from './lib/utils/ensure-property.utils';
package/src/index.js CHANGED
@@ -12,4 +12,5 @@ tslib_1.__exportStar(require("./lib/utils/is-test.utils"), exports);
12
12
  tslib_1.__exportStar(require("./lib/utils/normalize-locales.utils"), exports);
13
13
  tslib_1.__exportStar(require("./lib/utils/get-normalized-browser-locales.utils"), exports);
14
14
  tslib_1.__exportStar(require("./lib/utils/gatherer"), exports);
15
+ tslib_1.__exportStar(require("./lib/utils/ensure-property.utils"), exports);
15
16
  //# sourceMappingURL=index.js.map
package/src/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../libs/accelerator/src/index.ts"],"names":[],"mappings":";;;AAAA,4DAAiC;AACjC,uEAA4C;AAC5C,mFAAwD;AACxD,qEAA0C;AAC1C,sEAA2C;AAC3C,iEAAsC;AACtC,iEAAsC;AACtC,oEAAyC;AACzC,8EAAmD;AACnD,2FAAgE;AAChE,+DAAoC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../libs/accelerator/src/index.ts"],"names":[],"mappings":";;;AAAA,4DAAiC;AACjC,uEAA4C;AAC5C,mFAAwD;AACxD,qEAA0C;AAC1C,sEAA2C;AAC3C,iEAAsC;AACtC,iEAAsC;AACtC,oEAAyC;AACzC,8EAAmD;AACnD,2FAAgE;AAChE,+DAAoC;AACpC,4EAAiD"}
@@ -0,0 +1,58 @@
1
+ type PathKeys<T, Depth extends number = 5> = [Depth] extends [never] ? never : T extends object ? {
2
+ [K in keyof T]-?: K extends string | number ? T[K] extends object | undefined ? `${K}` | `${K}.${PathKeys<NonNullable<T[K]>, Prev[Depth]>}` : `${K}` : never;
3
+ }[keyof T] : never;
4
+ type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
5
+ type PathToTuple<S extends string> = S extends `${infer First}.${infer Rest}` ? [First, ...PathToTuple<Rest>] : [S];
6
+ type ValidPaths<T> = PathKeys<T> extends infer P ? P extends string ? PathToTuple<P> : never : never;
7
+ type SetPathTuple<T, Path extends ReadonlyArray<string | number>, Value> = Path extends readonly [infer Key, ...infer Rest] ? Key extends string | number ? Rest extends ReadonlyArray<string | number> ? Rest['length'] extends 0 ? T & {
8
+ [K in Key]: Value;
9
+ } : {
10
+ [K in keyof T | Key]-?: K extends Key ? K extends keyof T ? SetPathTuple<NonNullable<T[K]>, Rest, Value> : SetPathTuple<object, Rest, Value> : K extends keyof T ? T[K] : never;
11
+ } : never : never : never;
12
+ /**
13
+ * Ensures that a property exists at the specified path within an object.
14
+ * Creates intermediate objects as needed and sets the final property to the initial value
15
+ * only if the property is currently null or undefined.
16
+ *
17
+ * This function is useful for safely initializing deeply nested properties without worrying
18
+ * about intermediate objects being missing. It will not overwrite existing values - only
19
+ * null or undefined values are replaced.
20
+ *
21
+ * @template T - The object type
22
+ * @template Path - The path as a tuple of keys
23
+ * @template Value - The type of the value to set
24
+ * @param obj - The object to modify
25
+ * @param path - An array representing the path to the property (e.g., ['user', 'profile', 'name'])
26
+ * @param initialValue - The value to set if the property is currently null or undefined
27
+ *
28
+ * @example
29
+ * // Ensure a global debug flag exists
30
+ * ensureProperty(globalThis, ['myApp', 'config', 'debug'], false);
31
+ * // Access it safely:
32
+ * if (globalThis.myApp.config.debug) {
33
+ * console.log('Debug mode enabled');
34
+ * }
35
+ *
36
+ * @example
37
+ * // Initialize nested configuration
38
+ * const config = {};
39
+ * ensureProperty(config, ['database', 'host'], 'localhost');
40
+ * ensureProperty(config, ['database', 'port'], 5432);
41
+ * // config is now { database: { host: 'localhost', port: 5432 } }
42
+ *
43
+ * @example
44
+ * // Won't overwrite existing values
45
+ * const obj = { name: 'John' };
46
+ * ensureProperty(obj, ['name'], 'Jane');
47
+ * // obj.name remains 'John'
48
+ *
49
+ * @example
50
+ * // Replaces null values
51
+ * const obj = { name: null };
52
+ * ensureProperty(obj, ['name'], 'Default');
53
+ * // obj.name is now 'Default'
54
+ */
55
+ export declare function ensureProperty<const Path extends ReadonlyArray<string | number>, Value>(obj: typeof globalThis, path: Path, initialValue: Value): asserts obj is typeof globalThis & SetPathTuple<typeof globalThis, Path, Value>;
56
+ export declare function ensureProperty<T extends object, const Path extends ValidPaths<T>, Value>(obj: T, path: Path, initialValue: Value): asserts obj is T & SetPathTuple<T, Path, Value>;
57
+ export declare function ensureProperty<T extends object, const Path extends ReadonlyArray<string | number>, Value>(obj: T, path: Path, initialValue: Value): asserts obj is T & SetPathTuple<T, Path, Value>;
58
+ export {};
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ensureProperty = ensureProperty;
4
+ function ensureProperty(obj, path, initialValue) {
5
+ let current = obj;
6
+ for (let i = 0; i < path.length - 1; i++) {
7
+ const key = path[i];
8
+ if (current[key] == null || typeof current[key] !== 'object') {
9
+ current[key] = {};
10
+ }
11
+ current = current[key];
12
+ }
13
+ const lastKey = path.at(-1);
14
+ if (lastKey === undefined) {
15
+ return;
16
+ }
17
+ current[lastKey] ??= initialValue;
18
+ }
19
+ //# sourceMappingURL=ensure-property.utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ensure-property.utils.js","sourceRoot":"","sources":["../../../../../../libs/accelerator/src/lib/utils/ensure-property.utils.ts"],"names":[],"mappings":";;AAoHA,wCAoBC;AApBD,SAAgB,cAAc,CAC1B,GAAM,EACN,IAAU,EACV,YAAmB;IAEnB,IAAI,OAAO,GAAQ,GAAG,CAAC;IAEvB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QACvC,MAAM,GAAG,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;QACpB,IAAI,OAAO,CAAC,GAAG,CAAC,IAAI,IAAI,IAAI,OAAO,OAAO,CAAC,GAAG,CAAC,KAAK,QAAQ,EAAE,CAAC;YAC3D,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC;QACtB,CAAC;QACD,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;IAC3B,CAAC;IAED,MAAM,OAAO,GAAG,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IAC5B,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QACxB,OAAO;IACX,CAAC;IACD,OAAO,CAAC,OAAO,CAAC,KAAK,YAAY,CAAC;AACtC,CAAC"}