@tmlmobilidade/utils 20250917.1305.2 → 20250917.1446.59

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.
@@ -1 +1,2 @@
1
1
  export * from './get-value-at-path.js';
2
+ export * from './set-value-at-path.js';
@@ -1 +1,2 @@
1
1
  export * from './get-value-at-path.js';
2
+ export * from './set-value-at-path.js';
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Generates all possible dot-separated paths for a given object type, including paths through arrays.
3
+ * Turns `{ a: { b: string }, arr: { c: number }[] }` into `"a" | "a.b" | "arr" | "arr.0" | "arr.0.c"`
4
+ * @template T The object type to generate paths for.
5
+ * @template Prev A helper type to accumulate the current path prefix.
6
+ */
7
+ export type DotPath<T, Prev extends string = ''> = {
8
+ [K in keyof T & string]: T[K] extends (infer U)[] ? `${Prev}${K}.${number}` | `${Prev}${K}` | DotPath<U, `${Prev}${K}.${number}.`> : T[K] extends Record<string, any> ? `${Prev}${K}` | DotPath<T[K], `${Prev}${K}.`> : `${Prev}${K}`;
9
+ }[keyof T & string];
10
+ /**
11
+ * Retrieves the type of the value located at a specified dot-separated path within an object type.
12
+ * Supports paths through nested objects and arrays.
13
+ * @template T The object type to retrieve the value from.
14
+ * @template P The dot-separated path to the value.
15
+ */
16
+ export type PathValue<T, P extends string> = P extends `${infer Key}.${infer Rest}` ? Key extends keyof T ? T[Key] extends (infer U)[] ? Rest extends `${number}.${infer SubRest}` ? PathValue<U, SubRest> : Rest extends `${number}` ? U : never : PathValue<T[Key], Rest> : never : P extends keyof T ? T[P] : T extends (infer U)[] ? P extends `${number}` ? U : never : never;
17
+ /**
18
+ * Sets a value at a specified dot-separated path within an object.
19
+ * If intermediate objects or arrays do not exist, they are created.
20
+ * @param obj The object to set the value in.
21
+ * @param path The dot-separated path where the value should be set.
22
+ * @param value The value to set at the specified path.
23
+ * @returns The updated object with the value set at the specified path.
24
+ */
25
+ export declare function setValueAtPath<T extends object, P extends DotPath<T>>(obj: T, path: P, value: PathValue<T, P>): T;
@@ -0,0 +1,23 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ /**
3
+ * Sets a value at a specified dot-separated path within an object.
4
+ * If intermediate objects or arrays do not exist, they are created.
5
+ * @param obj The object to set the value in.
6
+ * @param path The dot-separated path where the value should be set.
7
+ * @param value The value to set at the specified path.
8
+ * @returns The updated object with the value set at the specified path.
9
+ */
10
+ export function setValueAtPath(obj, path, value) {
11
+ const keys = path.split('.');
12
+ let current = obj;
13
+ keys.slice(0, -1).forEach((key) => {
14
+ if (!(key in current)) {
15
+ // If numeric key, initialize as array
16
+ current[key] = /^\d+$/.test(key) ? [] : {};
17
+ }
18
+ current = current[key];
19
+ });
20
+ const lastKey = keys[keys.length - 1];
21
+ current[lastKey] = value;
22
+ return obj;
23
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmlmobilidade/utils",
3
- "version": "20250917.1305.2",
3
+ "version": "20250917.1446.59",
4
4
  "author": "João de Vasconcelos & Jusi Monteiro",
5
5
  "license": "AGPL-3.0-or-later",
6
6
  "homepage": "https://github.com/tmlmobilidade/services#readme",