-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathtypeUtils.ts
319 lines (299 loc) · 7.11 KB
/
typeUtils.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
/**
* Check if a value is a record.
*
* @example
* ```typescript
* import { isRecord } from '@aws-lambda-powertools/commons/typeUtils';
*
* const value = { key: 'value' };
* if (isRecord(value)) {
* // value is a record
* }
* ```
*
* @param value The value to check
*/
const isRecord = (
value: unknown
): value is Record<string | number, unknown> => {
return (
Object.prototype.toString.call(value) === '[object Object]' &&
!Object.is(value, null)
);
};
/**
* Check if a value is a string.
*
* @example
* ```typescript
* import { isString } from '@aws-lambda-powertools/commons/typeUtils';
*
* const value = 'foo';
* if (isString(value)) {
* // value is a string
* }
* ```
*
* @param value The value to check
*/
const isString = (value: unknown): value is string => {
return typeof value === 'string';
};
/**
* Check if a value is a number.
*
* @example
* ```typescript
* import { isNumber } from '@aws-lambda-powertools/commons/typeUtils';
*
* const value = 42;
* if (isNumber(value)) {
* // value is a number
* }
* ```
*
* @param value The value to check
*/
const isNumber = (value: unknown): value is number => {
return typeof value === 'number';
};
/**
* Check if a value is an integer number.
*
* @example
* ```typescript
* import { isIntegerNumber } from '@aws-lambda-powertools/commons/typeUtils';
*
* const value = 42;
* if (isIntegerNumber(value)) {
* // value is an integer number
* }
* ```
*
* @param value The value to check
*/
const isIntegerNumber = (value: unknown): value is number => {
return isNumber(value) && Number.isInteger(value);
};
/**
* Check if a value is truthy.
*
* @example
* ```typescript
* import { isTruthy } from '@aws-lambda-powertools/commons/typeUtils';
*
* const value = 'yes';
* if (isTruthy(value)) {
* // value is truthy
* }
* ```
*
* @see https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/types-grammar/ch4.md#toboolean
*
* @param value The value to check
*/
const isTruthy = (value: unknown): boolean => {
if (isString(value)) {
return value !== '';
}
if (isNumber(value)) {
return value !== 0;
}
if (typeof value === 'boolean') {
return value;
}
if (Array.isArray(value)) {
return value.length > 0;
}
if (isRecord(value)) {
return Object.keys(value).length > 0;
}
return false;
};
/**
* Check if a value is `null`.
*
* @example
* ```typescript
* import { isNull } from '@aws-lambda-powertools/commons/typeUtils';
*
* const value = null;
* if (isNull(value)) {
* // value is null
* }
* ```
*
* @param value The value to check
*/
const isNull = (value: unknown): value is null => {
return Object.is(value, null);
};
/**
* Check if a value is `null` or `undefined`.
*
* @example
* ```typescript
* import { isNullOrUndefined } from '@aws-lambda-powertools/commons/typeUtils';
*
* const value = null;
* if (isNullOrUndefined(value)) {
* // value is null or undefined
* }
* ```
*
* @param value The value to check
*/
const isNullOrUndefined = (value: unknown): value is null | undefined => {
return isNull(value) || Object.is(value, undefined);
};
/**
* Get the type of a value as a string.
*
* @example
* ```typescript
* import { getType } from '@aws-lambda-powertools/commons/typeUtils';
*
* const type = getType('foo'); // 'string'
* const otherType = getType(42); // 'number'
* const anotherType = getType({ key: 'value' }); // 'object'
* const unknownType = getType(Symbol('foo')); // 'unknown'
* ```
*
* @param value The value to check
*/
const getType = (value: unknown): string => {
if (Array.isArray(value)) {
return 'array';
}
if (isRecord(value)) {
return 'object';
}
if (isString(value)) {
return 'string';
}
if (isNumber(value)) {
return 'number';
}
if (typeof value === 'boolean') {
return 'boolean';
}
if (isNull(value)) {
return 'null';
}
return 'unknown';
};
/**
* Compare two arrays for strict equality.
*
* This function compares each element in the arrays, regardless of order.
*
* @example
* ```typescript
* import { areArraysEqual } from '@aws-lambda-powertools/commons/typeUtils';
*
* const left = [1, 2, 3];
* const right = [3, 2, 1];
* const equal = areArraysEqual(left, right); // true
*
* const otherLeft = [1, 2, 3];
* const otherRight = [1, 2, 4];
* const otherEqual = areArraysEqual(otherLeft, otherRight); // false
* ```
*
* @param left The left array to compare
* @param right The right array to compare
*/
const areArraysEqual = (left: unknown[], right: unknown[]): boolean => {
if (left.length !== right.length) {
return false;
}
return left.every((value, i) => isStrictEqual(value, right[i]));
};
/**
* Compare two records for strict equality.
*
* @example
* ```typescript
* import { areRecordsEqual } from '@aws-lambda-powertools/commons/typeUtils';
*
* const left = { key: 'value' };
* const right = { key: 'value' };
* const equal = areRecordsEqual(left, right); // true
*
* const otherLeft = { key: 'value' };
* const otherRight = { key: 'other value' };
* const otherEqual = areRecordsEqual(otherLeft, otherRight); // false
* ```
*
* @param left The left record to compare
* @param right The right record to compare
*/
const areRecordsEqual = (
left: Record<string, unknown>,
right: Record<string, unknown>
): boolean => {
const leftKeys = Object.keys(left);
const rightKeys = Object.keys(right);
if (leftKeys.length !== rightKeys.length) {
return false;
}
return leftKeys.every((key) => isStrictEqual(left[key], right[key]));
};
/**
* Check if two unknown values are strictly equal.
*
* If the values are arrays, then each element is compared, regardless of
* order. If the values are objects, then each key and value from left
* is compared to the corresponding key and value from right. If the
* values are primitives, then they are compared using strict equality.
*
* @example
* ```typescript
* import { isStrictEqual } from '@aws-lambda-powertools/commons/typeUtils';
*
* const left = { key: 'value' };
* const right = { key: 'value' };
* const equal = isStrictEqual(left, right); // true
*
* const otherLeft = [1, 2, 3];
* const otherRight = [3, 2, 1];
* const otherEqual = isStrictEqual(otherLeft, otherRight); // true
*
* const anotherLeft = 'foo';
* const anotherRight = 'bar';
* const anotherEqual = isStrictEqual(anotherLeft, anotherRight); // false
*
* const yetAnotherLeft = 42;
* const yetAnotherRight = 42;
* const yetAnotherEqual = isStrictEqual(yetAnotherLeft, yetAnotherRight); // true
* ```
*
* @param left Left side of strict equality comparison
* @param right Right side of strict equality comparison
*/
const isStrictEqual = (left: unknown, right: unknown): boolean => {
if (left === right) {
return true;
}
if (typeof left !== typeof right) {
return false;
}
if (Array.isArray(left) && Array.isArray(right)) {
return areArraysEqual(left, right);
}
if (isRecord(left) && isRecord(right)) {
return areRecordsEqual(left, right);
}
return false;
};
export {
isRecord,
isString,
isNumber,
isIntegerNumber,
isTruthy,
isNull,
isNullOrUndefined,
getType,
isStrictEqual,
};