-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobject.util.ts
371 lines (345 loc) · 11.3 KB
/
object.util.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import { ArrayUtil } from '@app-core/util';
import { Function0, isFFunction0, TFunction0 } from '@app-core/types/function';
import { Optional } from '@app-core/types/functional';
import { NullableOrUndefined, OrUndefined } from '@app-core/types';
import { Predicate1 } from '@app-core/types/predicate';
import * as _ from 'lodash';
/**
* Helper functions to manage common operations related with class instances.
*/
export class ObjectUtil {
constructor() {
throw new SyntaxError('ObjectUtil is an utility class');
}
/**
* Returns the first not `null` and not `undefined` value of the provided ones.
*
* <pre>
* coalesce( Result:
* null, 12
* undefined,
* 12,
* 15
* )
* </pre>
*
* @param valuesToVerify
* Values to check the first one neither `undefined` nor `null`
*
* @return first not `null` and not `undefined` value if exists, `undefined` otherwise
*/
static coalesce = <T>(...valuesToVerify: NullableOrUndefined<T>[]): OrUndefined<T> => {
let result;
if (!ArrayUtil.isEmpty(valuesToVerify)) {
result = ArrayUtil.find(
valuesToVerify,
Predicate1.of(
(t: NullableOrUndefined<T>) => this.nonNullOrUndefined(t)
)
);
}
return this.isNullOrUndefined(result)
? undefined
: result;
}
/**
* Returns an {@link Optional} containing not `null` and not `undefined` value of the provided ones,
* {@link Optional#empty} otherwise.
*
* <pre>
* coalesce( Result:
* null, Optional(12)
* undefined,
* 12,
* 15
* )
* </pre>
*
* @param valuesToVerify
* Values to check the first one neither `undefined` nor `null`
*
* @return {@link Optional} containing the first `null` and not `undefined` value if exists,
* {@link Optional#empty} otherwise.
*/
static coalesceOptional = <T>(...valuesToVerify: NullableOrUndefined<T>[]): Optional<T> => {
let result;
if (!ArrayUtil.isEmpty(valuesToVerify)) {
result = ArrayUtil.find(
valuesToVerify,
Predicate1.of(
(t: NullableOrUndefined<T>) => this.nonNullOrUndefined(t)
)
);
}
return Optional.ofNullable<T>(result);
}
/**
* Using provided `sourceObject` returns a new object containing the property-value pairs that match with given
* array of properties `propertiesToCopy`.
*
* <pre>
* Example:
*
* class User {
* public id: number;
* public name: string;
* public age: number;
*
* constructor(id: number, name: string, age: number) {
* this.id = id;
* this.name = name;
* this.age = age;
* }
* }
*
* // Will return { id: 10, name: 'user1' }
* ObjectUtil.copyProperties(
* new User(10, 'user1', 31),
* ['id', 'name']
* );
* </pre>
*
* @param sourceObject
* Instance with the property values to copy
* @param propertiesToCopy
* Array of the property to copy in the returned object
*
* @return new object containing the property-value pairs that match with `propertiesToCopy`, included in `sourceObject`,
* `undefined` if `sourceObject` is `null` or `undefined` and/or `propertiesToCopy` has no elements.
*/
static copyProperties = <T, K extends keyof T>(sourceObject: NullableOrUndefined<T>,
propertiesToCopy: NullableOrUndefined<K[]>): OrUndefined<Pick<T, K>> => {
if (this.isNullOrUndefined(sourceObject) ||
ArrayUtil.isEmpty(propertiesToCopy)) {
return undefined;
}
const result = {} as Pick<T, K>;
for (let property of propertiesToCopy!) {
if (this.nonNullOrUndefined(property)) {
result[property] = _.cloneDeep(sourceObject![property]);
}
}
return result;
}
/**
* Using provided `sourceObject` returns an {@link Optional} containing a new object with the property-value pairs
* that match with given array of properties `propertiesToCopy`.
*
* <pre>
* Example:
*
* class User {
* public id: number;
* public name: string;
* public age: number;
*
* constructor(id: number, name: string, age: number) {
* this.id = id;
* this.name = name;
* this.age = age;
* }
* }
*
* // Will return Optional({ id: 10, name: 'user1' })
* ObjectUtil.copyPropertiesOptional(
* new User(10, 'user1', 31),
* ['id', 'name']
* );
* </pre>
*
* @param sourceObject
* Instance with the property values to copy
* @param propertiesToCopy
* Array of the property to copy in the returned object
*
* @return {@link Optional} new object containing the property-value pairs that match with `propertiesToCopy`, included in `sourceObject`,
* {@link Optional#empty} if `sourceObject` is `null` or `undefined` and/or `propertiesToCopy` has no elements.
*/
static copyPropertiesOptional = <T, K extends keyof T>(sourceObject: NullableOrUndefined<T>,
propertiesToCopy: NullableOrUndefined<K[]>): Optional<Pick<T, K>> => {
if (this.isNullOrUndefined(sourceObject) ||
ArrayUtil.isEmpty(propertiesToCopy)) {
return Optional.empty<Pick<T, K>>();
}
const result = {} as Pick<T, K>;
for (let property of propertiesToCopy!) {
if (this.nonNullOrUndefined(property)) {
result[property] = _.cloneDeep(sourceObject![property]);
}
}
return Optional.of<Pick<T, K>>(result);
}
/**
* Returns `true` if `a` is equals to `b`, `false` otherwise.
*
* @apiNote
* This method supports comparing arrays, array buffers, booleans, date objects, error objects, maps, numbers,
* {@link Object} objects, regexes, sets, strings, symbols, and typed arrays.
* <p>
* Comparing {@link Object} objects tries to find if the instance has defined the `equals` method, using it
* if exists. Otherwise, are compared by their own, not inherited, enumerable properties.
* <p>
* Functions and DOM nodes are compared by strict equality, i.e. ===.
*
* <pre>
* Example:
*
* class User {
* public id: number;
* public name: string;
*
* constructor(id: number, name: string) {
* this.id = id;
* this.name = name;
* }
*
* equals = (other?: User | null): boolean =>
* ObjectUtil.isNullOrUndefined(other)
* ? false
* : this.id === other.id;
* }
*
* // Will return true
* ObjectUtil.equals(
* new User(10, 'user1'),
* new User(10, 'user2')
* );
*
* // Will return false
* ObjectUtil.equals(
* new User(10, 'user1'),
* new User(11, 'user1')
* );
* </pre>
*
* @param a
* First value to compare
* @param b
* First value to compare
*
* @return `true` if `a` is equals to `b`,
* `false` otherwise.
*/
static equals = <T>(a: NullableOrUndefined<T>,
b: NullableOrUndefined<T>): boolean => {
if ((this.isNullOrUndefined(a) && this.nonNullOrUndefined(b)) ||
(this.nonNullOrUndefined(a) && this.isNullOrUndefined(b))) {
return false;
}
if (this.nonNullOrUndefined(a) &&
'object' === typeof a &&
// @ts-ignore
'function' === typeof a['equals']) {
// @ts-ignore
return a['equals'](b);
}
return _.isEqual(a, b);
}
/**
* Returns the given `valueToVerify` if it is neither `undefined` nor `null`,
* `defaultValue` otherwise.
*
* @param valueToVerify
* Value to return if it is neither `undefined` nor `null`
* @param defaultValue
* Returned value if `valueToVerify` is `undefined` or `null`
*
* @return `valueToVerify` if it is neither `undefined` nor `null`,
* `defaultValue` otherwise
*/
static getOrElse<T>(valueToVerify: NullableOrUndefined<T>,
defaultValue: T): T;
/**
* Returns the given `valueToVerify` if it is neither `undefined` nor `null`,
* the result after invoking `defaultValue` otherwise.
*
* <pre>
* getOrElse( Result:
* null, 'DEFAULT VALUE'
* () => 'DEFAULT VALUE'
* )
* </pre>
*
* @param valueToVerify
* Value to return if it is neither `undefined` nor `null`
* @param defaultValue
* {@link TFunction0} to invoke if `valueToVerify` is `undefined` or `null`
*
* @return `valueToVerify` if it is neither `undefined` nor `null`,
* the result after invoking `defaultValue` otherwise
*/
static getOrElse<T>(valueToVerify: NullableOrUndefined<T>,
defaultValue: TFunction0<T>): T;
static getOrElse<T>(valueToVerify: NullableOrUndefined<T>,
defaultValue: TFunction0<T> | T): T {
if (this.nonNullOrUndefined(valueToVerify)) {
return valueToVerify;
}
if (Function0.isFunction(defaultValue) || isFFunction0(defaultValue)) {
return Function0.of(defaultValue)
.apply();
}
return defaultValue;
}
/**
* Verifies if the provided `valueToVerify` is `null` or `undefined`.
*
* @param valueToVerify
* Value to return if it is `undefined` or `null`
*
* @return `true` if the provided `valueToVerify` is `null` or `undefined`,
* `false` otherwise.
*/
static isNullOrUndefined = (valueToVerify: any): valueToVerify is null | undefined =>
_.isNil(valueToVerify);
/**
* Verifies if the provided `valueToVerify` is `null` or `undefined`.
*
* @param valueToVerify
* Value to return if it is `undefined` or `null`
*
* @return `true` if the provided `valueToVerify` is not `null` and not `undefined`,
* `false` otherwise.
*/
static nonNullOrUndefined = <T>(valueToVerify: NullableOrUndefined<T>): valueToVerify is Exclude<typeof valueToVerify, null | undefined> =>
!this.isNullOrUndefined(valueToVerify);
/**
* Returns a copy of provided `sourceObject` sorting its properties.
*
* @apiNote
* If `sourceObject` is not an {@link Object} then it will be returned.
*
* <pre>
* sortObjectProperties( Result:
* 12 12
* )
* sortObjectProperties( Result:
* { b: 1, a: '2', h: { z: 11, a: 'ea' }} { a: '2', b: 1, h: { a: 'ea', z: 11 }}
* )
* </pre>
*
* @param sourceObject
* Instance with the properties to sort
*
* @return new object containing the same properties but sorted,
* `undefined` if `sourceObject` is `null` or `undefined`.
*/
static sortObjectProperties<T>(sourceObject: NullableOrUndefined<T>): OrUndefined<T> {
if (this.isNullOrUndefined(sourceObject)) {
return undefined;
}
if ('object' !== typeof sourceObject) {
return sourceObject;
}
return Object.keys(sourceObject!)
.sort()
.reduce(
(accumulator, currentKey) => {
// @ts-ignore
accumulator[currentKey] = ObjectUtil.sortObjectProperties(sourceObject![currentKey]);
return accumulator;
},
{} as T
);
}
}