@tmlmobilidade/utils 20250918.1353.26 → 20250918.1816.45

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/dist/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  export * from './src/batching/index.js';
2
- export * from './src/convert-object.js';
3
2
  export * from './src/css/index.js';
4
3
  export * from './src/dates/index.js';
5
4
  export * from './src/files/files.js';
@@ -9,6 +8,7 @@ export * from './src/http.js';
9
8
  export * from './src/logs/index.js';
10
9
  export * from './src/math/index.js';
11
10
  export * from './src/navigation/index.js';
11
+ export * from './src/objects/index.js';
12
12
  export * from './src/permissions.js';
13
13
  export * from './src/random/index.js';
14
14
  export * from './src/singleton-proxy.js';
package/dist/index.js CHANGED
@@ -1,5 +1,4 @@
1
1
  export * from './src/batching/index.js';
2
- export * from './src/convert-object.js';
3
2
  export * from './src/css/index.js';
4
3
  export * from './src/dates/index.js';
5
4
  export * from './src/files/files.js';
@@ -9,6 +8,7 @@ export * from './src/http.js';
9
8
  export * from './src/logs/index.js';
10
9
  export * from './src/math/index.js';
11
10
  export * from './src/navigation/index.js';
11
+ export * from './src/objects/index.js';
12
12
  export * from './src/permissions.js';
13
13
  export * from './src/random/index.js';
14
14
  export * from './src/singleton-proxy.js';
@@ -0,0 +1,37 @@
1
+ type Diff<T> = {
2
+ [K in keyof T]?: T[K] extends (infer U)[] ? (Diff<U> | {
3
+ curr_value: U;
4
+ prev_value: U;
5
+ })[] : T[K] extends object ? Diff<T[K]> | {
6
+ curr_value: T[K];
7
+ prev_value: T[K];
8
+ } : {
9
+ curr_value: T[K];
10
+ prev_value: T[K];
11
+ };
12
+ };
13
+ /**
14
+ * Compares two objects and returns the differences between them.
15
+ *
16
+ * @template T - The type of the objects being compared.
17
+ * @param {T} prev - The previous state of the object.
18
+ * @param {Partial<T>} curr - The current state of the object.
19
+ * @returns {Diff<T>} - An object representing the differences between the previous and current states.
20
+ *
21
+ * This function iterates over the keys of the current object and compares each value with the corresponding value in the previous object.
22
+ * If the values are arrays, it compares each element in the arrays and records any differences.
23
+ * If the values are objects, it recursively compares the nested objects.
24
+ * If the values are primitive types, it records the current and previous values if they differ.
25
+ *
26
+ * @example
27
+ * const prev = { name: 'Alice', age: 30, hobbies: ['reading', 'hiking'] };
28
+ * const curr = { name: 'Alice', age: 31, hobbies: ['reading', 'swimming'] };
29
+ * const differences = compareObjects(prev, curr);
30
+ * // differences will be:
31
+ * // {
32
+ * // age: { curr_value: 31, prev_value: 30 },
33
+ * // hobbies: [{ curr_value: 'swimming', prev_value: 'hiking' }]
34
+ * // }
35
+ */
36
+ export declare function compareObjects<T extends object>(prev: T, curr: Partial<T>): Diff<T>;
37
+ export {};
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Compares two objects and returns the differences between them.
3
+ *
4
+ * @template T - The type of the objects being compared.
5
+ * @param {T} prev - The previous state of the object.
6
+ * @param {Partial<T>} curr - The current state of the object.
7
+ * @returns {Diff<T>} - An object representing the differences between the previous and current states.
8
+ *
9
+ * This function iterates over the keys of the current object and compares each value with the corresponding value in the previous object.
10
+ * If the values are arrays, it compares each element in the arrays and records any differences.
11
+ * If the values are objects, it recursively compares the nested objects.
12
+ * If the values are primitive types, it records the current and previous values if they differ.
13
+ *
14
+ * @example
15
+ * const prev = { name: 'Alice', age: 30, hobbies: ['reading', 'hiking'] };
16
+ * const curr = { name: 'Alice', age: 31, hobbies: ['reading', 'swimming'] };
17
+ * const differences = compareObjects(prev, curr);
18
+ * // differences will be:
19
+ * // {
20
+ * // age: { curr_value: 31, prev_value: 30 },
21
+ * // hobbies: [{ curr_value: 'swimming', prev_value: 'hiking' }]
22
+ * // }
23
+ */
24
+ export function compareObjects(prev, curr) {
25
+ const diff = {};
26
+ Object.keys(curr).forEach((key) => {
27
+ const prevVal = prev[key];
28
+ const currVal = curr[key];
29
+ if (prevVal === currVal)
30
+ return;
31
+ if (Array.isArray(prevVal) && Array.isArray(currVal)) {
32
+ const arrayDiff = [];
33
+ const maxLength = Math.max(prevVal.length, currVal.length);
34
+ for (let i = 0; i < maxLength; i++) {
35
+ const p = prevVal[i];
36
+ const c = currVal[i];
37
+ if (p === c)
38
+ continue;
39
+ if (typeof p === 'object' && typeof c === 'object' && p && c) {
40
+ const nestedDiff = compareObjects(p, c);
41
+ arrayDiff.push(nestedDiff);
42
+ }
43
+ else {
44
+ arrayDiff.push({ curr_value: c, prev_value: p });
45
+ }
46
+ }
47
+ if (arrayDiff.length > 0) {
48
+ diff[key] = arrayDiff;
49
+ }
50
+ }
51
+ else if (prevVal
52
+ && currVal
53
+ && typeof prevVal === 'object'
54
+ && typeof currVal === 'object') {
55
+ const nestedDiff = compareObjects(prevVal, currVal);
56
+ if (Object.keys(nestedDiff).length > 0) {
57
+ diff[key] = nestedDiff;
58
+ }
59
+ }
60
+ else {
61
+ diff[key] = { curr_value: currVal, prev_value: prevVal };
62
+ }
63
+ });
64
+ return diff;
65
+ }
@@ -0,0 +1,21 @@
1
+ type DotPrefix<T extends string, U extends string> = '' extends T ? U : `${T}.${U}`;
2
+ type NestedKeys<T, Prefix extends string = ''> = {
3
+ [K in keyof T & string]: T[K] extends Record<string, any> ? DotPrefix<Prefix, K> | NestedKeys<T[K], DotPrefix<Prefix, K>> : DotPrefix<Prefix, K>;
4
+ }[keyof T & string];
5
+ export type FlattenObjectType<T> = Record<NestedKeys<T>, any>;
6
+ /**
7
+ * Flattens an object using dot notation for nested fields.
8
+ * Arrays are treated as leaf nodes and are not traversed.
9
+ * Includes both nested objects and their individual properties as separate keys.
10
+ *
11
+ * Example:
12
+ * flattenObject({ a: { b: 1, c: { d: 2 } } })
13
+ * -> {
14
+ * 'a': { b: 1, c: { d: 2 } },
15
+ * 'a.b': 1,
16
+ * 'a.c': { d: 2 },
17
+ * 'a.c.d': 2
18
+ * }
19
+ */
20
+ export declare function flattenObject<T extends object>(input: T): FlattenObjectType<T>;
21
+ export {};
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Flattens an object using dot notation for nested fields.
3
+ * Arrays are treated as leaf nodes and are not traversed.
4
+ * Includes both nested objects and their individual properties as separate keys.
5
+ *
6
+ * Example:
7
+ * flattenObject({ a: { b: 1, c: { d: 2 } } })
8
+ * -> {
9
+ * 'a': { b: 1, c: { d: 2 } },
10
+ * 'a.b': 1,
11
+ * 'a.c': { d: 2 },
12
+ * 'a.c.d': 2
13
+ * }
14
+ */
15
+ export function flattenObject(input) {
16
+ const result = {};
17
+ if (input === null || input === undefined)
18
+ return result;
19
+ const isObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
20
+ const stack = [{ path: '', value: input }];
21
+ while (stack.length > 0) {
22
+ const { path, value } = stack.pop();
23
+ if (!isObject(value)) {
24
+ if (path) {
25
+ result[path] = value;
26
+ }
27
+ continue;
28
+ }
29
+ const entries = Object.entries(value);
30
+ if (entries.length === 0) {
31
+ if (path)
32
+ result[path] = value;
33
+ continue;
34
+ }
35
+ // Add the current object itself to the result
36
+ if (path) {
37
+ result[path] = value;
38
+ }
39
+ for (const [key, child] of entries) {
40
+ const nextPath = path ? `${path}.${key}` : key;
41
+ if (Array.isArray(child)) {
42
+ result[nextPath] = child;
43
+ continue;
44
+ }
45
+ if (isObject(child)) {
46
+ stack.push({ path: nextPath, value: child });
47
+ continue;
48
+ }
49
+ result[nextPath] = child;
50
+ }
51
+ }
52
+ return result;
53
+ }
@@ -0,0 +1,3 @@
1
+ export * from './compare-objects.js';
2
+ export * from './convert-object.js';
3
+ export * from './flatten-object.js';
@@ -0,0 +1,3 @@
1
+ export * from './compare-objects.js';
2
+ export * from './convert-object.js';
3
+ export * from './flatten-object.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmlmobilidade/utils",
3
- "version": "20250918.1353.26",
3
+ "version": "20250918.1816.45",
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",