@onecx/accelerator 6.17.0 → 6.18.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": "6.
|
|
3
|
+
"version": "6.18.0",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"rxjs": "^7.8.1",
|
|
12
12
|
"@nx/devkit": "^20.3.0",
|
|
13
13
|
"@nx/module-federation": "20.4.0",
|
|
14
|
-
"@onecx/nx-migration-utils": "^6.
|
|
14
|
+
"@onecx/nx-migration-utils": "^6.18.0"
|
|
15
15
|
},
|
|
16
16
|
"type": "commonjs",
|
|
17
17
|
"main": "./src/index.js",
|
|
@@ -18,41 +18,72 @@ type SetPathTuple<T, Path extends ReadonlyArray<string | number>, Value> = Path
|
|
|
18
18
|
* about intermediate objects being missing. It will not overwrite existing values - only
|
|
19
19
|
* null or undefined values are replaced.
|
|
20
20
|
*
|
|
21
|
+
* **Returns the typed object** - you should use the return value to access the properties
|
|
22
|
+
* with full type safety.
|
|
23
|
+
*
|
|
24
|
+
* Supports property names with special characters that can only be accessed via bracket notation.
|
|
25
|
+
*
|
|
21
26
|
* @template T - The object type
|
|
22
27
|
* @template Path - The path as a tuple of keys
|
|
23
28
|
* @template Value - The type of the value to set
|
|
24
29
|
* @param obj - The object to modify
|
|
25
30
|
* @param path - An array representing the path to the property (e.g., ['user', 'profile', 'name'])
|
|
26
31
|
* @param initialValue - The value to set if the property is currently null or undefined
|
|
32
|
+
* @returns The same object with updated type information including the new property
|
|
27
33
|
*
|
|
28
34
|
* @example
|
|
29
|
-
* //
|
|
30
|
-
* ensureProperty(globalThis, ['myApp', 'config', 'debug'], false);
|
|
31
|
-
*
|
|
32
|
-
* if (globalThis.myApp.config.debug) {
|
|
35
|
+
* // Recommended: Use the return value for full type safety
|
|
36
|
+
* const g = ensureProperty(globalThis, ['myApp', 'config', 'debug'], false);
|
|
37
|
+
* if (g.myApp.config.debug) {
|
|
33
38
|
* console.log('Debug mode enabled');
|
|
34
39
|
* }
|
|
35
40
|
*
|
|
36
41
|
* @example
|
|
37
|
-
* //
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
* ensureProperty(config, ['database', '
|
|
41
|
-
*
|
|
42
|
+
* // For regular property names (no special chars), both ways work:
|
|
43
|
+
* // 1. Using return value (recommended for consistency)
|
|
44
|
+
* let config = {};
|
|
45
|
+
* config = ensureProperty(config, ['database', 'host'], 'localhost');
|
|
46
|
+
* console.log(config.database.host); // TypeScript knows about this
|
|
47
|
+
*
|
|
48
|
+
* // 2. Without using return value (works for non-special chars)
|
|
49
|
+
* const settings = {};
|
|
50
|
+
* ensureProperty(settings, ['server', 'port'], 3000);
|
|
51
|
+
* console.log(settings.server.port); // Also works, but less type-safe
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* // For globalThis with regular names, you can declare it globally:
|
|
55
|
+
* declare global {
|
|
56
|
+
* var myApp: { config: { debug: boolean } };
|
|
57
|
+
* }
|
|
58
|
+
* ensureProperty(globalThis, ['myApp', 'config', 'debug'], false);
|
|
59
|
+
* if (globalThis.myApp.config.debug) { // Works without using return value
|
|
60
|
+
* console.log('Debug enabled');
|
|
61
|
+
* }
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* // For special characters, MUST use return value:
|
|
65
|
+
* const g = ensureProperty(globalThis, ['@onecx/accelerator', 'version'], '1.0.0');
|
|
66
|
+
* console.log(g['@onecx/accelerator'].version); // TypeScript knows this exists
|
|
67
|
+
* // globalThis['@onecx/accelerator'].version won't work without the return value
|
|
42
68
|
*
|
|
43
69
|
* @example
|
|
44
70
|
* // Won't overwrite existing values
|
|
45
|
-
*
|
|
46
|
-
* ensureProperty(obj, ['name'], 'Jane');
|
|
47
|
-
*
|
|
71
|
+
* let obj = { name: 'John' };
|
|
72
|
+
* obj = ensureProperty(obj, ['name'], 'Jane');
|
|
73
|
+
* console.log(obj.name); // Still 'John'
|
|
48
74
|
*
|
|
49
75
|
* @example
|
|
50
76
|
* // Replaces null values
|
|
51
|
-
*
|
|
52
|
-
* ensureProperty(obj, ['name'], 'Default');
|
|
53
|
-
*
|
|
77
|
+
* let obj = { name: null };
|
|
78
|
+
* obj = ensureProperty(obj, ['name'], 'Default');
|
|
79
|
+
* console.log(obj.name); // Now 'Default'
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* // Nested paths with special characters
|
|
83
|
+
* const g = ensureProperty(globalThis, ['@myapp/config', 'feature-flags', 'enabled'], true);
|
|
84
|
+
* console.log(g['@myapp/config']['feature-flags'].enabled);
|
|
54
85
|
*/
|
|
55
|
-
export declare function ensureProperty<const Path extends ReadonlyArray<string | number>, Value>(obj: typeof globalThis, path: Path, initialValue: Value):
|
|
56
|
-
export declare function ensureProperty<T extends object, const Path extends ValidPaths<T>, Value>(obj: T, path: Path, initialValue: Value):
|
|
57
|
-
export declare function ensureProperty<T extends object, const Path extends ReadonlyArray<string | number>, Value>(obj: T, path: Path, initialValue: Value):
|
|
86
|
+
export declare function ensureProperty<const Path extends ReadonlyArray<string | number>, Value>(obj: typeof globalThis, path: Path, initialValue: Value): typeof globalThis & SetPathTuple<typeof globalThis, Path, Value>;
|
|
87
|
+
export declare function ensureProperty<T extends object, const Path extends ValidPaths<T>, Value>(obj: T, path: Path, initialValue: Value): T & SetPathTuple<T, Path, Value>;
|
|
88
|
+
export declare function ensureProperty<T extends object, const Path extends ReadonlyArray<string | number>, Value>(obj: T, path: Path, initialValue: Value): T & SetPathTuple<T, Path, Value>;
|
|
58
89
|
export {};
|
|
@@ -12,8 +12,9 @@ function ensureProperty(obj, path, initialValue) {
|
|
|
12
12
|
}
|
|
13
13
|
const lastKey = path.at(-1);
|
|
14
14
|
if (lastKey === undefined) {
|
|
15
|
-
return;
|
|
15
|
+
return obj;
|
|
16
16
|
}
|
|
17
17
|
current[lastKey] ??= initialValue;
|
|
18
|
+
return obj;
|
|
18
19
|
}
|
|
19
20
|
//# sourceMappingURL=ensure-property.utils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ensure-property.utils.js","sourceRoot":"","sources":["../../../../../../libs/accelerator/src/lib/utils/ensure-property.utils.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"ensure-property.utils.js","sourceRoot":"","sources":["../../../../../../libs/accelerator/src/lib/utils/ensure-property.utils.ts"],"names":[],"mappings":";;AAmJA,wCAqBC;AArBD,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,GAAuC,CAAC;IACnD,CAAC;IACD,OAAO,CAAC,OAAO,CAAC,KAAK,YAAY,CAAC;IAClC,OAAO,GAAuC,CAAC;AACnD,CAAC"}
|