@hichchi/utils 0.0.1-alpha.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.
@@ -0,0 +1,414 @@
1
+ "use strict";
2
+ /* eslint-disable @typescript-eslint/no-explicit-any */
3
+ // noinspection JSUnusedGlobalSymbols
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.prune = exports.omit = exports.getValueByPath = exports.searchMapValues = exports.groupBy = exports.getMapKeys = void 0;
6
+ exports.deepCopy = deepCopy;
7
+ exports.getMapKey = getMapKey;
8
+ exports.objectToPathValueSet = objectToPathValueSet;
9
+ exports.pathValueSetToObject = pathValueSetToObject;
10
+ /**
11
+ * Deep copy an object.
12
+ * @template T Type of the object.
13
+ * @param {T} obj Object to copy.
14
+ * @returns {T} Copied object.
15
+ *
16
+ * @example
17
+ * ```TypeScript
18
+ * // Example usage
19
+ * const object = {
20
+ * name: "John Doe"
21
+ * }
22
+ *
23
+ * const copiedObject = deepCopy(object);
24
+ * ```
25
+ */
26
+ function deepCopy(obj) {
27
+ if (Array.isArray(obj)) {
28
+ return obj.map(deepCopy);
29
+ }
30
+ else if (typeof obj === "object" && obj !== null) {
31
+ return Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, deepCopy(value)]));
32
+ }
33
+ return obj;
34
+ }
35
+ /**
36
+ * Get the key of a map by value.
37
+ * @param {Map<string, unknown>} map Map to get key from.
38
+ * @param {unknown} value Value to get key for.
39
+ * @returns {string | undefined} Key of the map.
40
+ *
41
+ * @example
42
+ * ```TypeScript
43
+ * // Example usage
44
+ * const user = new Map<string, string>([
45
+ * ["firstName", "John"],
46
+ * ["lastName", "Doe"],
47
+ * ["preferredName", "John"],
48
+ * ["age", 30],
49
+ * ]);
50
+ *
51
+ * const key = getMapKey(user, "value2");
52
+ *
53
+ * // Example output: "firstName"
54
+ * ```
55
+ */
56
+ function getMapKey(map, value) {
57
+ var _a;
58
+ return (_a = [...Array.from(map.entries())].find(([, v]) => v === value)) === null || _a === void 0 ? void 0 : _a[0];
59
+ }
60
+ /**
61
+ * Get the keys of a map by partial value.
62
+ * @param {Map<string, string>} map Map to get keys from.
63
+ * @param {string} partialValue Partial value to get keys for.
64
+ * @returns - Keys of the map.
65
+ *
66
+ * @example
67
+ * ```TypeScript
68
+ * // Example usage
69
+ * const user = new Map<string, string>([
70
+ * ["firstName", "John"],
71
+ * ["lastName", "Doe"],
72
+ * ["preferredName", "John"],
73
+ * ["age", 30],
74
+ * ]);
75
+ *
76
+ * const keys = getMapKeys(user, "Jo");
77
+ *
78
+ * // Example output
79
+ * ["firstName", "preferredName"]
80
+ * ```
81
+ */
82
+ const getMapKeys = (map, partialValue) => {
83
+ const keys = [];
84
+ for (const [key, value] of Array.from(map.entries())) {
85
+ if (value.includes(partialValue)) {
86
+ keys.push(key);
87
+ }
88
+ }
89
+ return keys;
90
+ };
91
+ exports.getMapKeys = getMapKeys;
92
+ /**
93
+ * Groups an array of objects by a key.
94
+ * @template K Type of the key.
95
+ * @template V Type of the object.
96
+ * @param {Array<V>} list Array of objects to group.
97
+ * @param {(input: V) => K} keyGetter Function to get the key from the object.
98
+ * @returns {Map<K | null, Array<V>>} Grouped objects.
99
+ *
100
+ * @example
101
+ * ```TypeScript
102
+ * // Example usage
103
+ * // group by age, all have unique names
104
+ * const users = [
105
+ * { name: "John", age: 30 },
106
+ * { name: "Jane", age: 25 },
107
+ * { name: "Doe", age: 30 },
108
+ * { name: "Smith", age: 25 },
109
+ * { name: "Denis", age: 30 },
110
+ * ];
111
+ *
112
+ * const groupedUsers = groupBy(users, user => user.age);
113
+ *
114
+ * // Example output
115
+ * Map {
116
+ * 30 => [
117
+ * { name: "John", age: 30 },
118
+ * { name: "Doe", age: 30 },
119
+ * { name: "Denis", age: 30 },
120
+ * ],
121
+ * 25 => [
122
+ * { name: "Jane", age: 25 },
123
+ * { name: "Smith", age: 25 },
124
+ * ],
125
+ * }
126
+ * ```
127
+ */
128
+ const groupBy = (list, keyGetter) => {
129
+ const map = new Map();
130
+ list.forEach(item => {
131
+ const key = keyGetter(item);
132
+ const collection = map.get(key);
133
+ if (!collection) {
134
+ map.set(key, [item]);
135
+ }
136
+ else {
137
+ collection.push(item);
138
+ }
139
+ });
140
+ return map;
141
+ };
142
+ exports.groupBy = groupBy;
143
+ /**
144
+ * Get the values of a map by partial value.
145
+ * @param {Map<string, string>} map Map to get values from.
146
+ * @param {string} partialValue Partial value to get values for.
147
+ * @returns {string[]} Values of the map.
148
+ *
149
+ * @example
150
+ * ```TypeScript
151
+ * // Example usage
152
+ * const user = new Map<string, string>([
153
+ * ["name", "John Doe"],
154
+ * ["preferredName", "John"],
155
+ * ["age", 30],
156
+ * ]);
157
+ *
158
+ * const values = getMapValues(user, "Jo");
159
+ *
160
+ * // Example output
161
+ * ["John Doe", "John"]
162
+ * ```
163
+ */
164
+ const searchMapValues = (map, partialValue) => {
165
+ const values = [];
166
+ for (const [, value] of Array.from(map.entries())) {
167
+ if (value.includes(partialValue)) {
168
+ values.push(value);
169
+ }
170
+ }
171
+ return values;
172
+ };
173
+ exports.searchMapValues = searchMapValues;
174
+ /**
175
+ * Get value from an object by path.
176
+ * @template T - Type of the value.
177
+ * @param {InfiniteObject} obj Object to get value from.
178
+ * @param {string} path Path to get value from.
179
+ * @returns {T | undefined} Value from the object.
180
+ *
181
+ * @example
182
+ * ```TypeScript
183
+ * // Example usage
184
+ * const object = {
185
+ * role: "user",
186
+ * profile: {
187
+ * name: "John Doe",
188
+ * age: 30,
189
+ * address: {
190
+ * city: "New York",
191
+ * },
192
+ * },
193
+ * };
194
+ *
195
+ * const value = getValueByPath<string>(object, "profile.address.city");
196
+ *
197
+ * // Example output: "New York"
198
+ * ```
199
+ */
200
+ const getValueByPath = (obj, path) => {
201
+ const keys = path.split("."); // Split the path into an array of keys
202
+ let value = obj;
203
+ for (const key of keys) {
204
+ // noinspection RegExpRedundantEscape
205
+ const regExp = /^(\w+)\[(\d+)\]$/;
206
+ const isArrayIndex = regExp.exec(key); // Check if the key is an array index
207
+ if (isArrayIndex) {
208
+ const arrayKey = isArrayIndex[1];
209
+ // eslint-disable-next-line @typescript-eslint/no-magic-numbers
210
+ const index = Number(isArrayIndex[2]);
211
+ if (arrayKey && Array.isArray(value[arrayKey]) && index >= 0 && index < value[arrayKey].length) {
212
+ value = value[arrayKey][index]; // Update the value to the array element
213
+ }
214
+ else {
215
+ return undefined; // Return undefined if the array index is invalid
216
+ }
217
+ }
218
+ else if (value && typeof value === "object" && key in value) {
219
+ value = value[key]; // Update the value to the nested property
220
+ }
221
+ else {
222
+ return undefined; // Return undefined if any key is not found
223
+ }
224
+ }
225
+ return value;
226
+ };
227
+ exports.getValueByPath = getValueByPath;
228
+ /**
229
+ * Convert an object to a path value set
230
+ * @template T The type of the value
231
+ * @param {LiteralObject} obj The object
232
+ * @returns {PathValueSet<T>} The path value set
233
+ *
234
+ * @example
235
+ * ```TypeScript
236
+ * // Example usage
237
+ * const object = {
238
+ * role: "user",
239
+ * profile: {
240
+ * name: "John Doe",
241
+ * age: 30,
242
+ * address: {
243
+ * city: "New York",
244
+ * },
245
+ * },
246
+ * };
247
+ *
248
+ * const pathValueSet = objectToPathValueSet(object);
249
+ *
250
+ * // Example output
251
+ * {
252
+ * "role": "user",
253
+ * "profile.name": "John Doe",
254
+ * "profile.age": 30,
255
+ * "profile.address.city": "New York",
256
+ * }
257
+ * ```
258
+ */
259
+ function objectToPathValueSet(obj) {
260
+ const result = {};
261
+ function traverse(obj, path = []) {
262
+ for (const key in obj) {
263
+ if (Object.prototype.hasOwnProperty.call(obj, key)) {
264
+ const value = obj[key];
265
+ if (typeof value === "object" && !Array.isArray(value)) {
266
+ traverse(value, [...path, key]);
267
+ }
268
+ else {
269
+ result[[...path, key].join(".")] = value;
270
+ }
271
+ }
272
+ }
273
+ }
274
+ traverse(obj);
275
+ return result;
276
+ }
277
+ /**
278
+ * Convert the path value set to an object
279
+ * @template R The return type
280
+ * @param {PathValueSet} pathValueSet The path value set
281
+ * @returns {R} The object with the path value set converted
282
+ *
283
+ * @example
284
+ * ```TypeScript
285
+ * // Example usage
286
+ * const pathValueSet = {
287
+ * "role": "user",
288
+ * "profile.name": "John Doe",
289
+ * "profile.age": 30,
290
+ * "profile.address.city": "New York",
291
+ * }
292
+ *
293
+ * const object = pathValueSetToObject(pathValueSet);
294
+ *
295
+ * // Example output
296
+ * {
297
+ * role: "user",
298
+ * profile: {
299
+ * name: "John Doe",
300
+ * age: 30,
301
+ * address: {
302
+ * city: "New York",
303
+ * },
304
+ * },
305
+ * }
306
+ * ```
307
+ *
308
+ */
309
+ function pathValueSetToObject(pathValueSet) {
310
+ const object = {};
311
+ // Helper function to validate paths
312
+ const isValidPath = (path) => {
313
+ const regex = /^[a-zA-Z0-9_.-]+$/;
314
+ return path.split(".").every(part => regex.test(part));
315
+ };
316
+ // Helper function to set nested properties
317
+ const setObjectValue = (obj, keys, value) => {
318
+ const [firstKey, ...remainingKeys] = keys;
319
+ if (remainingKeys.length === 0) {
320
+ obj[firstKey] = value; // Set value at the final key
321
+ return;
322
+ }
323
+ // Initialize the key if it doesn't exist
324
+ obj[firstKey] = obj[firstKey] || {};
325
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
326
+ setObjectValue(obj[firstKey], remainingKeys, value); // Recurse for the rest of the keys
327
+ // TODO: Fix type for above
328
+ };
329
+ for (const path in pathValueSet) {
330
+ if (Object.prototype.hasOwnProperty.call(pathValueSet, path)) {
331
+ if (!isValidPath(path)) {
332
+ continue; // Skip invalid paths
333
+ }
334
+ const value = pathValueSet[path];
335
+ const keys = path.split("."); // Split path into keys
336
+ setObjectValue(object, keys, value); // Use helper to populate the object
337
+ }
338
+ }
339
+ return object;
340
+ }
341
+ /**
342
+ * Omits undefined properties and properties in the keys array from an object.
343
+ * @template T Type of the object.
344
+ * @param {Partial<T>} obj - Object to omit properties from.
345
+ * @param {(keyof T)[]} keys - Array of keys to omit.
346
+ * @returns {Partial<T>} - Object with omitted properties.
347
+ *
348
+ * @example
349
+ * ```TypeScript
350
+ * // Example usage
351
+ * const object = {
352
+ * role: "user",
353
+ * name: "John Doe",
354
+ * age: 30,
355
+ * address: undefined,
356
+ * };
357
+ *
358
+ * omit(object, ["role"]);
359
+ *
360
+ * // Example output
361
+ * {
362
+ * name: "John Doe",
363
+ * age: 30,
364
+ * }
365
+ * ```
366
+ */
367
+ const omit = (obj, keys) => {
368
+ if (obj) {
369
+ Object.keys(obj).forEach(key => {
370
+ return (obj[key] === undefined || (keys === null || keys === void 0 ? void 0 : keys.includes(key))) && delete obj[key];
371
+ });
372
+ }
373
+ return obj;
374
+ };
375
+ exports.omit = omit;
376
+ /**
377
+ * Prune an object by removing all empty, null, undefined, and prototype properties.
378
+ * @template T Type of the object.
379
+ * @param {T} obj Object to prune.
380
+ * @param {boolean} [omitPrototype] Omit prototype properties.
381
+ * @returns {T} Pruned object.
382
+ *
383
+ * @example
384
+ * ```TypeScript
385
+ * // Example usage
386
+ * const object = {
387
+ * role: "user",
388
+ * profile: {
389
+ * name: "John Doe",
390
+ * age: 30,
391
+ * address: undefined,
392
+ * city: "New York",
393
+ * },
394
+ * };
395
+ */
396
+ const prune = (obj, omitPrototype) => {
397
+ const objClone = {};
398
+ if (typeof obj !== "object") {
399
+ return objClone;
400
+ }
401
+ for (const key in obj) {
402
+ if (!omitPrototype || Object.prototype.hasOwnProperty.call(obj, key)) {
403
+ if (obj[key] !== null && typeof obj[key] === "object") {
404
+ objClone[key] = (0, exports.prune)(obj[key], omitPrototype);
405
+ }
406
+ else if (obj[key] !== null && obj[key] !== undefined && obj[key] !== "") {
407
+ objClone[key] = obj[key];
408
+ }
409
+ }
410
+ }
411
+ return objClone;
412
+ };
413
+ exports.prune = prune;
414
+ //# sourceMappingURL=object.utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"object.utils.js","sourceRoot":"","sources":["../../../../../libs/utils/src/lib/object.utils.ts"],"names":[],"mappings":";AAAA,uDAAuD;AACvD,qCAAqC;;;AAqBrC,4BA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* - `#{upperSnakeCase}`
8
+ * - `#{lowerCase}`
9
+ * - `#{sentenceCase}`
10
+ * - `#{firstCase}`
11
+ *
12
+ * @example
13
+ * ```TypeScript
14
+ * applyTemplate(
15
+ * 'Cannot create a #{lowerCase} with this email. #{sentenceCase} already exists.',
16
+ * 'User'
17
+ * );
18
+ * // Output: Cannot create a user with this email. User exists.
19
+ * ```
20
+ *
21
+ * @param {string} str Template string to apply prefix
22
+ * @param {string} prefix Prefix to apply
23
+ * @returns {string} Prefix applied string
24
+ */
25
+ export declare function applyTemplate(str: string, prefix: string): string;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ // noinspection JSUnusedGlobalSymbols
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.applyTemplate = applyTemplate;
5
+ const string_utils_1 = require("./string.utils");
6
+ /**
7
+ * Apply error message prefix to a valid template string
8
+ *
9
+ * Acceptable template tags:
10
+ * - `#{upperCase}`
11
+ * - `#{snakeCase}`
12
+ * - `#{upperSnakeCase}`
13
+ * - `#{lowerCase}`
14
+ * - `#{sentenceCase}`
15
+ * - `#{firstCase}`
16
+ *
17
+ * @example
18
+ * ```TypeScript
19
+ * applyTemplate(
20
+ * 'Cannot create a #{lowerCase} with this email. #{sentenceCase} already exists.',
21
+ * 'User'
22
+ * );
23
+ * // Output: Cannot create a user with this email. User exists.
24
+ * ```
25
+ *
26
+ * @param {string} str Template string to apply prefix
27
+ * @param {string} prefix Prefix to apply
28
+ * @returns {string} Prefix applied string
29
+ */
30
+ function applyTemplate(str, prefix) {
31
+ // TODO: Upgrade to export #{} as constants and include all string utils
32
+ return str
33
+ .replace("#{upperCase}", prefix.toUpperCase())
34
+ .replace("#{snakeCase}", (0, string_utils_1.toSnakeCase)(prefix))
35
+ .replace("#{upperSnakeCase}", (0, string_utils_1.toSnakeCase)(prefix, true))
36
+ .replace("#{lowerCase}", prefix.toLowerCase())
37
+ .replace("#{sentenceCase}", (0, string_utils_1.toSentenceCase)(prefix))
38
+ .replace("#{firstCase}", (0, string_utils_1.toFirstCase)(prefix));
39
+ }
40
+ //# sourceMappingURL=string-template.utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"string-template.utils.js","sourceRoot":"","sources":["../../../../../libs/utils/src/lib/string-template.utils.ts"],"names":[],"mappings":";AAAA,qCAAqC;;AA4BrC,sCASC;AAnCD,iDAA0E;AAE1E;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,SAAgB,aAAa,CAAC,GAAW,EAAE,MAAc;IACrD,wEAAwE;IACxE,OAAO,GAAG;SACL,OAAO,CAAC,cAAc,EAAE,MAAM,CAAC,WAAW,EAAE,CAAC;SAC7C,OAAO,CAAC,cAAc,EAAE,IAAA,0BAAW,EAAC,MAAM,CAAC,CAAC;SAC5C,OAAO,CAAC,mBAAmB,EAAE,IAAA,0BAAW,EAAC,MAAM,EAAE,IAAI,CAAC,CAAC;SACvD,OAAO,CAAC,cAAc,EAAE,MAAM,CAAC,WAAW,EAAE,CAAC;SAC7C,OAAO,CAAC,iBAAiB,EAAE,IAAA,6BAAc,EAAC,MAAM,CAAC,CAAC;SAClD,OAAO,CAAC,cAAc,EAAE,IAAA,0BAAW,EAAC,MAAM,CAAC,CAAC,CAAC;AACtD,CAAC"}