-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathutils.ts
165 lines (151 loc) · 4.51 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import difference from 'lodash/difference';
import flatMap from 'lodash/flatMap';
import isArray from 'lodash/isArray';
import isObject from 'lodash/isObject';
import reduce from 'lodash/reduce';
import set from 'lodash/set';
import values from 'lodash/values';
import { JSONSchemaDto } from '@novu/shared';
export function findMissingKeys(requiredRecord: object, actualRecord: object) {
const requiredKeys = collectKeys(requiredRecord);
const actualKeys = collectKeys(actualRecord);
return difference(requiredKeys, actualKeys);
}
export function collectKeys(obj, prefix = '') {
return reduce(
obj,
(result, value, key) => {
const newKey = prefix ? `${prefix}.${key}` : key;
if (isObject(value) && !isArray(value)) {
result.push(...collectKeys(value, newKey));
} else {
result.push(newKey);
}
return result;
},
[]
);
}
/**
* Recursively flattens an object's values into an array of strings.
* Handles nested objects, arrays, and converts primitive values to strings.
*
* @param obj - The object to flatten
* @returns An array of strings containing all primitive values found in the object
*
* @example
* ```typescript
* const input = {
* subject: "Hello {{name}}",
* body: "Welcome!",
* actions: {
* primary: {
* label: "Click {{here}}",
* url: "https://example.com"
* }
* },
* data: { count: 42 }
* };
*
* flattenObjectValues(input);
* Returns:
* [
* "Hello {{name}}",
* "Welcome!",
* "Click {{here}}",
* "https://example.com",
* "42"
* ]
* ```
*/
export function flattenObjectValues(obj: Record<string, unknown>): string[] {
return flatMap(values(obj), (value) => {
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
return String(value);
}
if (value && typeof value === 'object') {
return flattenObjectValues(value as Record<string, unknown>);
}
return [];
});
}
/**
* Recursively adds missing defaults for properties in a JSON schema object.
* For properties without defaults, adds interpolated path as the default value.
* Handles nested objects by recursively processing their properties.
*
* @param {Object} schema - The JSON schema object to process
* @param {string} parentPath - The parent path for building default values (default: 'payload')
* @returns {Object} The schema with missing defaults added
*
* @example
* const schema = {
* properties: {
* name: { type: 'string' },
* address: {
* type: 'object',
* properties: {
* street: { type: 'string' }
* }
* }
* }
* };
*
* const result = addMissingDefaults(schema);
* // Result:
* // {
* // properties: {
* // name: {
* // type: 'string',
* // default: '{{payload.name}}'
* // },
* // address: {
* // type: 'object',
* // properties: {
* // street: {
* // type: 'string',
* // default: '{{payload.address.street}}'
* // }
* // }
* // }
* // }
* // }
*/
export function mockSchemaDefaults(schema: JSONSchemaDto, parentPath = 'payload', depth = 0) {
const MAX_DEPTH = 10;
if (depth >= MAX_DEPTH) {
return schema;
}
if (schema.properties) {
Object.entries(schema.properties).forEach(([key, value]) => {
const valueDto = value as JSONSchemaDto;
if (valueDto.type === 'object') {
mockSchemaDefaults(valueDto, `${parentPath}.${key}`, depth + 1);
}
if (!valueDto.default && valueDto.type !== 'object') {
valueDto.default = `{{${parentPath}.${key}}}`;
}
});
}
return schema;
}
/**
*
* Converts an array of dot-notation paths into a nested object structure,
* setting each path's value to the path itself.
*
* @param keys - Array of dot-notation paths
* @param options - Optional configuration object
* - fn: Callback function to transform each path's value (default: identity function)
* @returns Nested object with paths as values
* @warning Entries without a namespace (no dots) will be ignored.
* @example
* keysToObject(['payload.old', 'payload.new', 'payload'])
* // Returns: { payload: { old: 'payload.old', new: 'payload.new' } }
* // Note: 'payload' entry is ignored as it has no namespace
*/
export function keysToObject(keys: string[], { fn } = { fn: (key: string) => key }) {
const result: Record<string, Record<string, unknown> | undefined> = {};
keys.filter((key) => key.includes('.')).forEach((key) => set(result, key, fn(key)));
return result;
}