Commit | Line | Data |
---|---|---|
6c43b441 JB |
1 | import { describe, it } from 'node:test'; |
2 | ||
85cce27f | 3 | import { hoursToMilliseconds, hoursToSeconds } from 'date-fns'; |
f64ca10c | 4 | import { expect } from 'expect'; |
a2111e8d | 5 | |
878e026c | 6 | import { Constants } from '../../src/utils/Constants'; |
9bf0ef23 JB |
7 | import { |
8 | cloneObject, | |
9 | convertToBoolean, | |
10 | convertToDate, | |
11 | convertToFloat, | |
12 | convertToInt, | |
1553f6df | 13 | extractTimeSeriesValues, |
be4c6702 | 14 | formatDurationMilliSeconds, |
8053c842 | 15 | formatDurationSeconds, |
9bf0ef23 JB |
16 | generateUUID, |
17 | getRandomFloat, | |
18 | getRandomInteger, | |
19 | hasOwnProp, | |
80c58041 | 20 | isArraySorted, |
9bf0ef23 JB |
21 | isEmptyArray, |
22 | isEmptyObject, | |
23 | isEmptyString, | |
24 | isIterable, | |
25 | isNotEmptyArray, | |
26 | isNotEmptyString, | |
27 | isNullOrUndefined, | |
28 | isObject, | |
29 | isUndefined, | |
0bd926c1 | 30 | isValidTime, |
5adf6ca4 JB |
31 | max, |
32 | min, | |
5f742aac | 33 | once, |
9bf0ef23 JB |
34 | roundTo, |
35 | secureRandom, | |
36 | sleep, | |
37 | validateUUID, | |
38 | } from '../../src/utils/Utils'; | |
a2111e8d | 39 | |
6c43b441 JB |
40 | await describe('Utils test suite', async () => { |
41 | await it('Verify generateUUID()/validateUUID()', () => { | |
9bf0ef23 | 42 | const uuid = generateUUID(); |
878e026c JB |
43 | expect(uuid).toBeDefined(); |
44 | expect(uuid.length).toEqual(36); | |
9bf0ef23 JB |
45 | expect(validateUUID(uuid)).toBe(true); |
46 | expect(validateUUID('abcdef00-0000-4000-0000-000000000000')).toBe(true); | |
47 | expect(validateUUID('')).toBe(false); | |
878e026c | 48 | // Shall invalidate Nil UUID |
9bf0ef23 JB |
49 | expect(validateUUID('00000000-0000-0000-0000-000000000000')).toBe(false); |
50 | expect(validateUUID('987FBC9-4BED-3078-CF07A-9141BA07C9F3')).toBe(false); | |
878e026c | 51 | }); |
8bd02502 | 52 | |
6c43b441 | 53 | await it('Verify sleep()', async () => { |
f204e9b4 | 54 | const start = performance.now(); |
9bf0ef23 | 55 | await sleep(1000); |
0f6058f3 JB |
56 | const stop = performance.now(); |
57 | expect(stop - start).toBeGreaterThanOrEqual(1000); | |
878e026c | 58 | }); |
45999aab | 59 | |
6c43b441 | 60 | await it('Verify formatDurationMilliSeconds()', () => { |
d7ceb0f0 JB |
61 | expect(formatDurationMilliSeconds(0)).toBe('0 seconds'); |
62 | expect(formatDurationMilliSeconds(900)).toBe('0 seconds'); | |
be4c6702 JB |
63 | expect(formatDurationMilliSeconds(1000)).toBe('1 second'); |
64 | expect(formatDurationMilliSeconds(hoursToMilliseconds(4380))).toBe('182 days 12 hours'); | |
65 | }); | |
66 | ||
6c43b441 | 67 | await it('Verify formatDurationSeconds()', () => { |
d7ceb0f0 JB |
68 | expect(formatDurationSeconds(0)).toBe('0 seconds'); |
69 | expect(formatDurationSeconds(0.9)).toBe('0 seconds'); | |
8053c842 JB |
70 | expect(formatDurationSeconds(1)).toBe('1 second'); |
71 | expect(formatDurationSeconds(hoursToSeconds(4380))).toBe('182 days 12 hours'); | |
72 | }); | |
73 | ||
6c43b441 | 74 | await it('Verify isValidTime()', () => { |
0bd926c1 JB |
75 | expect(isValidTime(undefined)).toBe(false); |
76 | expect(isValidTime(null)).toBe(false); | |
77 | expect(isValidTime('')).toBe(false); | |
78 | expect(isValidTime({})).toBe(false); | |
79 | expect(isValidTime([])).toBe(false); | |
80 | expect(isValidTime(new Map())).toBe(false); | |
81 | expect(isValidTime(new Set())).toBe(false); | |
82 | expect(isValidTime(new WeakMap())).toBe(false); | |
83 | expect(isValidTime(new WeakSet())).toBe(false); | |
84 | expect(isValidTime(-1)).toBe(true); | |
85 | expect(isValidTime(0)).toBe(true); | |
86 | expect(isValidTime(1)).toBe(true); | |
87 | expect(isValidTime(-0.5)).toBe(true); | |
88 | expect(isValidTime(0.5)).toBe(true); | |
89 | expect(isValidTime(new Date())).toBe(true); | |
ac8178a4 JB |
90 | }); |
91 | ||
6c43b441 | 92 | await it('Verify convertToDate()', () => { |
9bf0ef23 | 93 | expect(convertToDate(undefined)).toBe(undefined); |
68f56fdf JB |
94 | expect(() => convertToDate('')).toThrow(new Error("Cannot convert to date: ''")); |
95 | expect(() => convertToDate('00:70:61')).toThrow( | |
85cce27f JB |
96 | new Error("Cannot convert to date: '00:70:61'"), |
97 | ); | |
9bf0ef23 | 98 | expect(convertToDate(0)).toStrictEqual(new Date('1970-01-01T00:00:00.000Z')); |
85cce27f | 99 | expect(convertToDate(-1)).toStrictEqual(new Date('1969-12-31T23:59:59.999Z')); |
878e026c | 100 | const dateStr = '2020-01-01T00:00:00.000Z'; |
9bf0ef23 | 101 | let date = convertToDate(dateStr); |
878e026c JB |
102 | expect(date).toBeInstanceOf(Date); |
103 | expect(date).toStrictEqual(new Date(dateStr)); | |
9bf0ef23 | 104 | date = convertToDate(new Date(dateStr)); |
878e026c JB |
105 | expect(date).toBeInstanceOf(Date); |
106 | expect(date).toStrictEqual(new Date(dateStr)); | |
107 | }); | |
df645d8f | 108 | |
6c43b441 | 109 | await it('Verify convertToInt()', () => { |
9bf0ef23 JB |
110 | expect(convertToInt(undefined)).toBe(0); |
111 | expect(convertToInt(null)).toBe(0); | |
112 | expect(convertToInt(0)).toBe(0); | |
113 | const randomInteger = getRandomInteger(); | |
114 | expect(convertToInt(randomInteger)).toEqual(randomInteger); | |
115 | expect(convertToInt('-1')).toBe(-1); | |
116 | expect(convertToInt('1')).toBe(1); | |
117 | expect(convertToInt('1.1')).toBe(1); | |
118 | expect(convertToInt('1.9')).toBe(1); | |
119 | expect(convertToInt('1.999')).toBe(1); | |
120 | expect(convertToInt(-1)).toBe(-1); | |
121 | expect(convertToInt(1)).toBe(1); | |
122 | expect(convertToInt(1.1)).toBe(1); | |
123 | expect(convertToInt(1.9)).toBe(1); | |
124 | expect(convertToInt(1.999)).toBe(1); | |
878e026c | 125 | expect(() => { |
9bf0ef23 | 126 | convertToInt('NaN'); |
85cce27f | 127 | }).toThrow("Cannot convert to integer: 'NaN'"); |
878e026c | 128 | }); |
df645d8f | 129 | |
6c43b441 | 130 | await it('Verify convertToFloat()', () => { |
9bf0ef23 JB |
131 | expect(convertToFloat(undefined)).toBe(0); |
132 | expect(convertToFloat(null)).toBe(0); | |
133 | expect(convertToFloat(0)).toBe(0); | |
134 | const randomFloat = getRandomFloat(); | |
135 | expect(convertToFloat(randomFloat)).toEqual(randomFloat); | |
136 | expect(convertToFloat('-1')).toBe(-1); | |
137 | expect(convertToFloat('1')).toBe(1); | |
138 | expect(convertToFloat('1.1')).toBe(1.1); | |
139 | expect(convertToFloat('1.9')).toBe(1.9); | |
140 | expect(convertToFloat('1.999')).toBe(1.999); | |
141 | expect(convertToFloat(-1)).toBe(-1); | |
142 | expect(convertToFloat(1)).toBe(1); | |
143 | expect(convertToFloat(1.1)).toBe(1.1); | |
144 | expect(convertToFloat(1.9)).toBe(1.9); | |
145 | expect(convertToFloat(1.999)).toBe(1.999); | |
878e026c | 146 | expect(() => { |
9bf0ef23 | 147 | convertToFloat('NaN'); |
85cce27f | 148 | }).toThrow("Cannot convert to float: 'NaN'"); |
878e026c | 149 | }); |
df645d8f | 150 | |
6c43b441 | 151 | await it('Verify convertToBoolean()', () => { |
9bf0ef23 JB |
152 | expect(convertToBoolean(undefined)).toBe(false); |
153 | expect(convertToBoolean(null)).toBe(false); | |
154 | expect(convertToBoolean('true')).toBe(true); | |
155 | expect(convertToBoolean('false')).toBe(false); | |
156 | expect(convertToBoolean('TRUE')).toBe(true); | |
157 | expect(convertToBoolean('FALSE')).toBe(false); | |
158 | expect(convertToBoolean('1')).toBe(true); | |
159 | expect(convertToBoolean('0')).toBe(false); | |
160 | expect(convertToBoolean(1)).toBe(true); | |
161 | expect(convertToBoolean(0)).toBe(false); | |
162 | expect(convertToBoolean(true)).toBe(true); | |
163 | expect(convertToBoolean(false)).toBe(false); | |
164 | expect(convertToBoolean('')).toBe(false); | |
165 | expect(convertToBoolean('NoNBoolean')).toBe(false); | |
878e026c | 166 | }); |
df645d8f | 167 | |
6c43b441 | 168 | await it('Verify secureRandom()', () => { |
9bf0ef23 | 169 | const random = secureRandom(); |
878e026c JB |
170 | expect(typeof random === 'number').toBe(true); |
171 | expect(random).toBeGreaterThanOrEqual(0); | |
172 | expect(random).toBeLessThan(1); | |
173 | }); | |
a2111e8d | 174 | |
6c43b441 | 175 | await it('Verify getRandomInteger()', () => { |
9bf0ef23 | 176 | let randomInteger = getRandomInteger(); |
878e026c JB |
177 | expect(Number.isSafeInteger(randomInteger)).toBe(true); |
178 | expect(randomInteger).toBeGreaterThanOrEqual(0); | |
179 | expect(randomInteger).toBeLessThanOrEqual(Constants.MAX_RANDOM_INTEGER); | |
9bf0ef23 JB |
180 | expect(randomInteger).not.toEqual(getRandomInteger()); |
181 | randomInteger = getRandomInteger(0, -Constants.MAX_RANDOM_INTEGER); | |
878e026c JB |
182 | expect(randomInteger).toBeGreaterThanOrEqual(-Constants.MAX_RANDOM_INTEGER); |
183 | expect(randomInteger).toBeLessThanOrEqual(0); | |
68f56fdf | 184 | expect(() => getRandomInteger(0, 1)).toThrow( |
5edd8ba0 | 185 | 'The value of "max" is out of range. It must be greater than the value of "min" (1). Received 1', |
878e026c | 186 | ); |
68f56fdf | 187 | expect(() => getRandomInteger(-1)).toThrow( |
5edd8ba0 | 188 | 'The value of "max" is out of range. It must be greater than the value of "min" (0). Received 0', |
878e026c | 189 | ); |
68f56fdf | 190 | expect(() => getRandomInteger(Constants.MAX_RANDOM_INTEGER + 1)).toThrow( |
878e026c JB |
191 | `The value of "max" is out of range. It must be <= ${ |
192 | Constants.MAX_RANDOM_INTEGER + 1 | |
5edd8ba0 | 193 | }. Received 281_474_976_710_656`, |
878e026c | 194 | ); |
9bf0ef23 | 195 | randomInteger = getRandomInteger(2, 1); |
878e026c JB |
196 | expect(randomInteger).toBeGreaterThanOrEqual(1); |
197 | expect(randomInteger).toBeLessThanOrEqual(2); | |
5adf6ca4 JB |
198 | const maximum = 2.2, |
199 | minimum = 1.1; | |
200 | randomInteger = getRandomInteger(maximum, minimum); | |
201 | expect(randomInteger).toBeLessThanOrEqual(Math.floor(maximum)); | |
202 | expect(randomInteger).toBeGreaterThanOrEqual(Math.ceil(minimum)); | |
878e026c | 203 | }); |
a2111e8d | 204 | |
6c43b441 | 205 | await it('Verify roundTo()', () => { |
9bf0ef23 JB |
206 | expect(roundTo(0, 2)).toBe(0); |
207 | expect(roundTo(0.5, 0)).toBe(1); | |
208 | expect(roundTo(0.5, 2)).toBe(0.5); | |
209 | expect(roundTo(-0.5, 0)).toBe(-1); | |
210 | expect(roundTo(-0.5, 2)).toBe(-0.5); | |
211 | expect(roundTo(1.005, 0)).toBe(1); | |
212 | expect(roundTo(1.005, 2)).toBe(1.01); | |
213 | expect(roundTo(2.175, 2)).toBe(2.18); | |
214 | expect(roundTo(5.015, 2)).toBe(5.02); | |
215 | expect(roundTo(-1.005, 2)).toBe(-1.01); | |
216 | expect(roundTo(-2.175, 2)).toBe(-2.18); | |
217 | expect(roundTo(-5.015, 2)).toBe(-5.02); | |
316d1564 JB |
218 | }); |
219 | ||
6c43b441 | 220 | await it('Verify getRandomFloat()', () => { |
9bf0ef23 | 221 | let randomFloat = getRandomFloat(); |
878e026c JB |
222 | expect(typeof randomFloat === 'number').toBe(true); |
223 | expect(randomFloat).toBeGreaterThanOrEqual(0); | |
224 | expect(randomFloat).toBeLessThanOrEqual(Number.MAX_VALUE); | |
9bf0ef23 | 225 | expect(randomFloat).not.toEqual(getRandomFloat()); |
68f56fdf JB |
226 | expect(() => getRandomFloat(0, 1)).toThrow(new RangeError('Invalid interval')); |
227 | expect(() => getRandomFloat(Number.MAX_VALUE, -Number.MAX_VALUE)).toThrow( | |
5edd8ba0 | 228 | new RangeError('Invalid interval'), |
878e026c | 229 | ); |
9bf0ef23 | 230 | randomFloat = getRandomFloat(0, -Number.MAX_VALUE); |
878e026c JB |
231 | expect(randomFloat).toBeGreaterThanOrEqual(-Number.MAX_VALUE); |
232 | expect(randomFloat).toBeLessThanOrEqual(0); | |
233 | }); | |
a2111e8d | 234 | |
6c43b441 | 235 | await it('Verify extractTimeSeriesValues()', () => { |
1553f6df JB |
236 | expect(extractTimeSeriesValues([])).toEqual([]); |
237 | expect(extractTimeSeriesValues([{ timestamp: Date.now(), value: 1.1 }])).toEqual([1.1]); | |
238 | expect( | |
239 | extractTimeSeriesValues([ | |
240 | { timestamp: Date.now(), value: 1.1 }, | |
241 | { timestamp: Date.now(), value: 2.2 }, | |
242 | ]), | |
243 | ).toEqual([1.1, 2.2]); | |
244 | }); | |
245 | ||
6c43b441 | 246 | await it('Verify isObject()', () => { |
9bf0ef23 JB |
247 | expect(isObject('test')).toBe(false); |
248 | expect(isObject(undefined)).toBe(false); | |
249 | expect(isObject(null)).toBe(false); | |
250 | expect(isObject(0)).toBe(false); | |
251 | expect(isObject([])).toBe(false); | |
252 | expect(isObject([0, 1])).toBe(false); | |
253 | expect(isObject(['0', '1'])).toBe(false); | |
254 | expect(isObject({})).toBe(true); | |
255 | expect(isObject({ 1: 1 })).toBe(true); | |
256 | expect(isObject({ '1': '1' })).toBe(true); | |
257 | expect(isObject(new Map())).toBe(true); | |
258 | expect(isObject(new Set())).toBe(true); | |
259 | expect(isObject(new WeakMap())).toBe(true); | |
260 | expect(isObject(new WeakSet())).toBe(true); | |
878e026c | 261 | }); |
a2111e8d | 262 | |
6c43b441 | 263 | await it('Verify cloneObject()', () => { |
dd5a1d6c | 264 | const obj = { 1: 1 }; |
9bf0ef23 JB |
265 | expect(cloneObject(obj)).toStrictEqual(obj); |
266 | expect(cloneObject(obj) === obj).toBe(false); | |
32f5e42d JB |
267 | const nestedObj = { 1: obj, 2: obj }; |
268 | expect(cloneObject(nestedObj)).toStrictEqual(nestedObj); | |
269 | expect(cloneObject(nestedObj) === nestedObj).toBe(false); | |
dd5a1d6c | 270 | const array = [1, 2]; |
9bf0ef23 JB |
271 | expect(cloneObject(array)).toStrictEqual(array); |
272 | expect(cloneObject(array) === array).toBe(false); | |
32f5e42d JB |
273 | const objArray = [obj, obj]; |
274 | expect(cloneObject(objArray)).toStrictEqual(objArray); | |
275 | expect(cloneObject(objArray) === objArray).toBe(false); | |
dd5a1d6c | 276 | const date = new Date(); |
9bf0ef23 JB |
277 | expect(cloneObject(date)).toStrictEqual(date); |
278 | expect(cloneObject(date) === date).toBe(false); | |
dd5a1d6c | 279 | const map = new Map([['1', '2']]); |
32f5e42d | 280 | expect(cloneObject(map)).toStrictEqual({}); |
dd5a1d6c | 281 | const set = new Set(['1']); |
32f5e42d | 282 | expect(cloneObject(set)).toStrictEqual({}); |
dd5a1d6c JB |
283 | // The URL object seems to have not enumerable properties |
284 | const url = new URL('https://domain.tld'); | |
32f5e42d | 285 | expect(cloneObject(url)).toStrictEqual({}); |
dd5a1d6c | 286 | const weakMap = new WeakMap([[{ 1: 1 }, { 2: 2 }]]); |
32f5e42d | 287 | expect(cloneObject(weakMap)).toStrictEqual({}); |
dd5a1d6c | 288 | const weakSet = new WeakSet([{ 1: 1 }, { 2: 2 }]); |
32f5e42d | 289 | expect(cloneObject(weakSet)).toStrictEqual({}); |
dd5a1d6c JB |
290 | }); |
291 | ||
6c43b441 | 292 | await it('Verify hasOwnProp()', () => { |
9bf0ef23 JB |
293 | expect(hasOwnProp('test', '')).toBe(false); |
294 | expect(hasOwnProp(undefined, '')).toBe(false); | |
295 | expect(hasOwnProp(null, '')).toBe(false); | |
296 | expect(hasOwnProp([], '')).toBe(false); | |
297 | expect(hasOwnProp({}, '')).toBe(false); | |
298 | expect(hasOwnProp({ 1: 1 }, 1)).toBe(true); | |
299 | expect(hasOwnProp({ 1: 1 }, '1')).toBe(true); | |
300 | expect(hasOwnProp({ 1: 1 }, 2)).toBe(false); | |
301 | expect(hasOwnProp({ 1: 1 }, '2')).toBe(false); | |
302 | expect(hasOwnProp({ '1': '1' }, '1')).toBe(true); | |
303 | expect(hasOwnProp({ '1': '1' }, 1)).toBe(true); | |
304 | expect(hasOwnProp({ '1': '1' }, '2')).toBe(false); | |
305 | expect(hasOwnProp({ '1': '1' }, 2)).toBe(false); | |
af29b598 JB |
306 | }); |
307 | ||
6c43b441 | 308 | await it('Verify isIterable()', () => { |
9bf0ef23 JB |
309 | expect(isIterable('')).toBe(true); |
310 | expect(isIterable(' ')).toBe(true); | |
311 | expect(isIterable('test')).toBe(true); | |
312 | expect(isIterable(undefined)).toBe(false); | |
313 | expect(isIterable(null)).toBe(false); | |
314 | expect(isIterable(0)).toBe(false); | |
315 | expect(isIterable([0, 1])).toBe(true); | |
316 | expect(isIterable({ 1: 1 })).toBe(false); | |
317 | expect(isIterable(new Map())).toBe(true); | |
318 | expect(isIterable(new Set())).toBe(true); | |
319 | expect(isIterable(new WeakMap())).toBe(false); | |
320 | expect(isIterable(new WeakSet())).toBe(false); | |
878e026c | 321 | }); |
a2111e8d | 322 | |
6c43b441 | 323 | await it('Verify isEmptyString()', () => { |
9bf0ef23 JB |
324 | expect(isEmptyString('')).toBe(true); |
325 | expect(isEmptyString(' ')).toBe(true); | |
326 | expect(isEmptyString(' ')).toBe(true); | |
327 | expect(isEmptyString('test')).toBe(false); | |
328 | expect(isEmptyString(' test')).toBe(false); | |
329 | expect(isEmptyString('test ')).toBe(false); | |
330 | expect(isEmptyString(undefined)).toBe(true); | |
331 | expect(isEmptyString(null)).toBe(true); | |
332 | expect(isEmptyString(0)).toBe(false); | |
333 | expect(isEmptyString({})).toBe(false); | |
334 | expect(isEmptyString([])).toBe(false); | |
335 | expect(isEmptyString(new Map())).toBe(false); | |
336 | expect(isEmptyString(new Set())).toBe(false); | |
337 | expect(isEmptyString(new WeakMap())).toBe(false); | |
338 | expect(isEmptyString(new WeakSet())).toBe(false); | |
878e026c | 339 | }); |
5a2a53cf | 340 | |
6c43b441 | 341 | await it('Verify isNotEmptyString()', () => { |
9bf0ef23 JB |
342 | expect(isNotEmptyString('')).toBe(false); |
343 | expect(isNotEmptyString(' ')).toBe(false); | |
344 | expect(isNotEmptyString(' ')).toBe(false); | |
345 | expect(isNotEmptyString('test')).toBe(true); | |
346 | expect(isNotEmptyString(' test')).toBe(true); | |
347 | expect(isNotEmptyString('test ')).toBe(true); | |
348 | expect(isNotEmptyString(undefined)).toBe(false); | |
349 | expect(isNotEmptyString(null)).toBe(false); | |
350 | expect(isNotEmptyString(0)).toBe(false); | |
351 | expect(isNotEmptyString({})).toBe(false); | |
352 | expect(isNotEmptyString([])).toBe(false); | |
353 | expect(isNotEmptyString(new Map())).toBe(false); | |
354 | expect(isNotEmptyString(new Set())).toBe(false); | |
355 | expect(isNotEmptyString(new WeakMap())).toBe(false); | |
356 | expect(isNotEmptyString(new WeakSet())).toBe(false); | |
878e026c | 357 | }); |
429f8c9d | 358 | |
6c43b441 | 359 | await it('Verify isUndefined()', () => { |
9bf0ef23 JB |
360 | expect(isUndefined(undefined)).toBe(true); |
361 | expect(isUndefined(null)).toBe(false); | |
362 | expect(isUndefined('')).toBe(false); | |
363 | expect(isUndefined(0)).toBe(false); | |
364 | expect(isUndefined({})).toBe(false); | |
365 | expect(isUndefined([])).toBe(false); | |
366 | expect(isUndefined(new Map())).toBe(false); | |
367 | expect(isUndefined(new Set())).toBe(false); | |
368 | expect(isUndefined(new WeakMap())).toBe(false); | |
369 | expect(isUndefined(new WeakSet())).toBe(false); | |
878e026c | 370 | }); |
a2111e8d | 371 | |
6c43b441 | 372 | await it('Verify isNullOrUndefined()', () => { |
9bf0ef23 JB |
373 | expect(isNullOrUndefined(undefined)).toBe(true); |
374 | expect(isNullOrUndefined(null)).toBe(true); | |
375 | expect(isNullOrUndefined('')).toBe(false); | |
376 | expect(isNullOrUndefined(0)).toBe(false); | |
377 | expect(isNullOrUndefined({})).toBe(false); | |
378 | expect(isNullOrUndefined([])).toBe(false); | |
379 | expect(isNullOrUndefined(new Map())).toBe(false); | |
380 | expect(isNullOrUndefined(new Set())).toBe(false); | |
381 | expect(isNullOrUndefined(new WeakMap())).toBe(false); | |
382 | expect(isNullOrUndefined(new WeakSet())).toBe(false); | |
878e026c | 383 | }); |
53ac516c | 384 | |
6c43b441 | 385 | await it('Verify isEmptyArray()', () => { |
9bf0ef23 JB |
386 | expect(isEmptyArray([])).toBe(true); |
387 | expect(isEmptyArray([1, 2])).toBe(false); | |
388 | expect(isEmptyArray(['1', '2'])).toBe(false); | |
389 | expect(isEmptyArray(undefined)).toBe(false); | |
390 | expect(isEmptyArray(null)).toBe(false); | |
391 | expect(isEmptyArray('')).toBe(false); | |
392 | expect(isEmptyArray('test')).toBe(false); | |
393 | expect(isEmptyArray(0)).toBe(false); | |
394 | expect(isEmptyArray({})).toBe(false); | |
395 | expect(isEmptyArray(new Map())).toBe(false); | |
396 | expect(isEmptyArray(new Set())).toBe(false); | |
397 | expect(isEmptyArray(new WeakMap())).toBe(false); | |
398 | expect(isEmptyArray(new WeakSet())).toBe(false); | |
878e026c | 399 | }); |
a2111e8d | 400 | |
6c43b441 | 401 | await it('Verify isNotEmptyArray()', () => { |
9bf0ef23 JB |
402 | expect(isNotEmptyArray([])).toBe(false); |
403 | expect(isNotEmptyArray([1, 2])).toBe(true); | |
404 | expect(isNotEmptyArray(['1', '2'])).toBe(true); | |
405 | expect(isNotEmptyArray(undefined)).toBe(false); | |
406 | expect(isNotEmptyArray(null)).toBe(false); | |
407 | expect(isNotEmptyArray('')).toBe(false); | |
408 | expect(isNotEmptyArray('test')).toBe(false); | |
409 | expect(isNotEmptyArray(0)).toBe(false); | |
410 | expect(isNotEmptyArray({})).toBe(false); | |
411 | expect(isNotEmptyArray(new Map())).toBe(false); | |
412 | expect(isNotEmptyArray(new Set())).toBe(false); | |
413 | expect(isNotEmptyArray(new WeakMap())).toBe(false); | |
414 | expect(isNotEmptyArray(new WeakSet())).toBe(false); | |
878e026c JB |
415 | }); |
416 | ||
6c43b441 | 417 | await it('Verify isEmptyObject()', () => { |
9bf0ef23 JB |
418 | expect(isEmptyObject({})).toBe(true); |
419 | expect(isEmptyObject({ 1: 1, 2: 2 })).toBe(false); | |
9bf0ef23 JB |
420 | expect(isEmptyObject(new Map())).toBe(false); |
421 | expect(isEmptyObject(new Set())).toBe(false); | |
422 | expect(isEmptyObject(new WeakMap())).toBe(false); | |
423 | expect(isEmptyObject(new WeakSet())).toBe(false); | |
878e026c | 424 | }); |
80c58041 | 425 | |
6c43b441 | 426 | await it('Verify isArraySorted()', () => { |
80c58041 JB |
427 | expect( |
428 | isArraySorted([], (a, b) => { | |
429 | return a - b; | |
430 | }), | |
431 | ).toBe(true); | |
432 | expect( | |
433 | isArraySorted([1], (a, b) => { | |
434 | return a - b; | |
435 | }), | |
436 | ).toBe(true); | |
437 | expect(isArraySorted<number>([1, 2, 3, 4, 5], (a, b) => a - b)).toBe(true); | |
438 | expect(isArraySorted<number>([1, 2, 3, 5, 4], (a, b) => a - b)).toBe(false); | |
479200ff | 439 | expect(isArraySorted<number>([2, 1, 3, 4, 5], (a, b) => a - b)).toBe(false); |
80c58041 | 440 | }); |
5f742aac | 441 | |
6c43b441 | 442 | await it('Verify once()', () => { |
5f742aac JB |
443 | let called = 0; |
444 | const fn = () => ++called; | |
445 | const onceFn = once(fn, this); | |
446 | const result1 = onceFn(); | |
447 | expect(called).toBe(1); | |
448 | expect(result1).toBe(1); | |
449 | const result2 = onceFn(); | |
450 | expect(called).toBe(1); | |
451 | expect(result2).toBe(1); | |
452 | const result3 = onceFn(); | |
453 | expect(called).toBe(1); | |
454 | expect(result3).toBe(1); | |
455 | }); | |
5adf6ca4 | 456 | |
6c43b441 | 457 | await it('Verify min()', () => { |
a51a4ead | 458 | expect(min()).toBe(Infinity); |
5adf6ca4 JB |
459 | expect(min(0, 1)).toBe(0); |
460 | expect(min(1, 0)).toBe(0); | |
461 | expect(min(0, -1)).toBe(-1); | |
462 | expect(min(-1, 0)).toBe(-1); | |
463 | }); | |
464 | ||
6c43b441 | 465 | await it('Verify max()', () => { |
a51a4ead | 466 | expect(max()).toBe(-Infinity); |
5adf6ca4 JB |
467 | expect(max(0, 1)).toBe(1); |
468 | expect(max(1, 0)).toBe(1); | |
469 | expect(max(0, -1)).toBe(0); | |
470 | expect(max(-1, 0)).toBe(0); | |
471 | }); | |
878e026c | 472 | }); |