| 1 | import { expect } from 'expect'; |
| 2 | |
| 3 | import { Constants } from '../../src/utils/Constants'; |
| 4 | import { |
| 5 | cloneObject, |
| 6 | convertToBoolean, |
| 7 | convertToDate, |
| 8 | convertToFloat, |
| 9 | convertToInt, |
| 10 | generateUUID, |
| 11 | getRandomFloat, |
| 12 | getRandomInteger, |
| 13 | hasOwnProp, |
| 14 | isArraySorted, |
| 15 | isEmptyArray, |
| 16 | isEmptyObject, |
| 17 | isEmptyString, |
| 18 | isIterable, |
| 19 | isNotEmptyArray, |
| 20 | isNotEmptyString, |
| 21 | isNullOrUndefined, |
| 22 | isObject, |
| 23 | isUndefined, |
| 24 | isValidDate, |
| 25 | roundTo, |
| 26 | secureRandom, |
| 27 | sleep, |
| 28 | validateUUID, |
| 29 | } from '../../src/utils/Utils'; |
| 30 | |
| 31 | describe('Utils test suite', () => { |
| 32 | it('Verify generateUUID()/validateUUID()', () => { |
| 33 | const uuid = generateUUID(); |
| 34 | expect(uuid).toBeDefined(); |
| 35 | expect(uuid.length).toEqual(36); |
| 36 | expect(validateUUID(uuid)).toBe(true); |
| 37 | expect(validateUUID('abcdef00-0000-4000-0000-000000000000')).toBe(true); |
| 38 | expect(validateUUID('')).toBe(false); |
| 39 | // Shall invalidate Nil UUID |
| 40 | expect(validateUUID('00000000-0000-0000-0000-000000000000')).toBe(false); |
| 41 | expect(validateUUID('987FBC9-4BED-3078-CF07A-9141BA07C9F3')).toBe(false); |
| 42 | }); |
| 43 | |
| 44 | it('Verify sleep()', async () => { |
| 45 | const start = performance.now(); |
| 46 | await sleep(1000); |
| 47 | const end = performance.now(); |
| 48 | expect(end - start).toBeGreaterThanOrEqual(1000); |
| 49 | }); |
| 50 | |
| 51 | it('Verify isValidDate()', () => { |
| 52 | expect(isValidDate(undefined)).toBe(false); |
| 53 | expect(isValidDate(null)).toBe(false); |
| 54 | expect(isValidDate('')).toBe(false); |
| 55 | expect(isValidDate({})).toBe(false); |
| 56 | expect(isValidDate([])).toBe(false); |
| 57 | expect(isValidDate(new Map())).toBe(false); |
| 58 | expect(isValidDate(new Set())).toBe(false); |
| 59 | expect(isValidDate(new WeakMap())).toBe(false); |
| 60 | expect(isValidDate(new WeakSet())).toBe(false); |
| 61 | expect(isValidDate(-1)).toBe(true); |
| 62 | expect(isValidDate(0)).toBe(true); |
| 63 | expect(isValidDate(1)).toBe(true); |
| 64 | expect(isValidDate(-0.5)).toBe(true); |
| 65 | expect(isValidDate(0.5)).toBe(true); |
| 66 | expect(isValidDate(new Date())).toBe(true); |
| 67 | }); |
| 68 | |
| 69 | it('Verify convertToDate()', () => { |
| 70 | expect(convertToDate(undefined)).toBe(undefined); |
| 71 | expect(convertToDate(null)).toBe(null); |
| 72 | const invalidDate = convertToDate(''); |
| 73 | expect(invalidDate instanceof Date && !isNaN(invalidDate.getTime())).toBe(false); |
| 74 | expect(convertToDate(0)).toStrictEqual(new Date('1970-01-01T00:00:00.000Z')); |
| 75 | const dateStr = '2020-01-01T00:00:00.000Z'; |
| 76 | let date = convertToDate(dateStr); |
| 77 | expect(date).toBeInstanceOf(Date); |
| 78 | expect(date).toStrictEqual(new Date(dateStr)); |
| 79 | date = convertToDate(new Date(dateStr)); |
| 80 | expect(date).toBeInstanceOf(Date); |
| 81 | expect(date).toStrictEqual(new Date(dateStr)); |
| 82 | }); |
| 83 | |
| 84 | it('Verify convertToInt()', () => { |
| 85 | expect(convertToInt(undefined)).toBe(0); |
| 86 | expect(convertToInt(null)).toBe(0); |
| 87 | expect(convertToInt(0)).toBe(0); |
| 88 | const randomInteger = getRandomInteger(); |
| 89 | expect(convertToInt(randomInteger)).toEqual(randomInteger); |
| 90 | expect(convertToInt('-1')).toBe(-1); |
| 91 | expect(convertToInt('1')).toBe(1); |
| 92 | expect(convertToInt('1.1')).toBe(1); |
| 93 | expect(convertToInt('1.9')).toBe(1); |
| 94 | expect(convertToInt('1.999')).toBe(1); |
| 95 | expect(convertToInt(-1)).toBe(-1); |
| 96 | expect(convertToInt(1)).toBe(1); |
| 97 | expect(convertToInt(1.1)).toBe(1); |
| 98 | expect(convertToInt(1.9)).toBe(1); |
| 99 | expect(convertToInt(1.999)).toBe(1); |
| 100 | expect(() => { |
| 101 | convertToInt('NaN'); |
| 102 | }).toThrow('Cannot convert to integer: NaN'); |
| 103 | }); |
| 104 | |
| 105 | it('Verify convertToFloat()', () => { |
| 106 | expect(convertToFloat(undefined)).toBe(0); |
| 107 | expect(convertToFloat(null)).toBe(0); |
| 108 | expect(convertToFloat(0)).toBe(0); |
| 109 | const randomFloat = getRandomFloat(); |
| 110 | expect(convertToFloat(randomFloat)).toEqual(randomFloat); |
| 111 | expect(convertToFloat('-1')).toBe(-1); |
| 112 | expect(convertToFloat('1')).toBe(1); |
| 113 | expect(convertToFloat('1.1')).toBe(1.1); |
| 114 | expect(convertToFloat('1.9')).toBe(1.9); |
| 115 | expect(convertToFloat('1.999')).toBe(1.999); |
| 116 | expect(convertToFloat(-1)).toBe(-1); |
| 117 | expect(convertToFloat(1)).toBe(1); |
| 118 | expect(convertToFloat(1.1)).toBe(1.1); |
| 119 | expect(convertToFloat(1.9)).toBe(1.9); |
| 120 | expect(convertToFloat(1.999)).toBe(1.999); |
| 121 | expect(() => { |
| 122 | convertToFloat('NaN'); |
| 123 | }).toThrow('Cannot convert to float: NaN'); |
| 124 | }); |
| 125 | |
| 126 | it('Verify convertToBoolean()', () => { |
| 127 | expect(convertToBoolean(undefined)).toBe(false); |
| 128 | expect(convertToBoolean(null)).toBe(false); |
| 129 | expect(convertToBoolean('true')).toBe(true); |
| 130 | expect(convertToBoolean('false')).toBe(false); |
| 131 | expect(convertToBoolean('TRUE')).toBe(true); |
| 132 | expect(convertToBoolean('FALSE')).toBe(false); |
| 133 | expect(convertToBoolean('1')).toBe(true); |
| 134 | expect(convertToBoolean('0')).toBe(false); |
| 135 | expect(convertToBoolean(1)).toBe(true); |
| 136 | expect(convertToBoolean(0)).toBe(false); |
| 137 | expect(convertToBoolean(true)).toBe(true); |
| 138 | expect(convertToBoolean(false)).toBe(false); |
| 139 | expect(convertToBoolean('')).toBe(false); |
| 140 | expect(convertToBoolean('NoNBoolean')).toBe(false); |
| 141 | }); |
| 142 | |
| 143 | it('Verify secureRandom()', () => { |
| 144 | const random = secureRandom(); |
| 145 | expect(typeof random === 'number').toBe(true); |
| 146 | expect(random).toBeGreaterThanOrEqual(0); |
| 147 | expect(random).toBeLessThan(1); |
| 148 | }); |
| 149 | |
| 150 | it('Verify getRandomInteger()', () => { |
| 151 | let randomInteger = getRandomInteger(); |
| 152 | expect(Number.isSafeInteger(randomInteger)).toBe(true); |
| 153 | expect(randomInteger).toBeGreaterThanOrEqual(0); |
| 154 | expect(randomInteger).toBeLessThanOrEqual(Constants.MAX_RANDOM_INTEGER); |
| 155 | expect(randomInteger).not.toEqual(getRandomInteger()); |
| 156 | randomInteger = getRandomInteger(0, -Constants.MAX_RANDOM_INTEGER); |
| 157 | expect(randomInteger).toBeGreaterThanOrEqual(-Constants.MAX_RANDOM_INTEGER); |
| 158 | expect(randomInteger).toBeLessThanOrEqual(0); |
| 159 | expect(() => getRandomInteger(0, 1)).toThrowError( |
| 160 | 'The value of "max" is out of range. It must be greater than the value of "min" (1). Received 1', |
| 161 | ); |
| 162 | expect(() => getRandomInteger(-1)).toThrowError( |
| 163 | 'The value of "max" is out of range. It must be greater than the value of "min" (0). Received 0', |
| 164 | ); |
| 165 | expect(() => getRandomInteger(Constants.MAX_RANDOM_INTEGER + 1)).toThrowError( |
| 166 | `The value of "max" is out of range. It must be <= ${ |
| 167 | Constants.MAX_RANDOM_INTEGER + 1 |
| 168 | }. Received 281_474_976_710_656`, |
| 169 | ); |
| 170 | randomInteger = getRandomInteger(2, 1); |
| 171 | expect(randomInteger).toBeGreaterThanOrEqual(1); |
| 172 | expect(randomInteger).toBeLessThanOrEqual(2); |
| 173 | const max = 2.2, |
| 174 | min = 1.1; |
| 175 | randomInteger = getRandomInteger(max, min); |
| 176 | expect(randomInteger).toBeGreaterThanOrEqual(Math.ceil(min)); |
| 177 | expect(randomInteger).toBeLessThanOrEqual(Math.floor(max)); |
| 178 | }); |
| 179 | |
| 180 | it('Verify roundTo()', () => { |
| 181 | expect(roundTo(0, 2)).toBe(0); |
| 182 | expect(roundTo(0.5, 0)).toBe(1); |
| 183 | expect(roundTo(0.5, 2)).toBe(0.5); |
| 184 | expect(roundTo(-0.5, 0)).toBe(-1); |
| 185 | expect(roundTo(-0.5, 2)).toBe(-0.5); |
| 186 | expect(roundTo(1.005, 0)).toBe(1); |
| 187 | expect(roundTo(1.005, 2)).toBe(1.01); |
| 188 | expect(roundTo(2.175, 2)).toBe(2.18); |
| 189 | expect(roundTo(5.015, 2)).toBe(5.02); |
| 190 | expect(roundTo(-1.005, 2)).toBe(-1.01); |
| 191 | expect(roundTo(-2.175, 2)).toBe(-2.18); |
| 192 | expect(roundTo(-5.015, 2)).toBe(-5.02); |
| 193 | }); |
| 194 | |
| 195 | it('Verify getRandomFloat()', () => { |
| 196 | let randomFloat = getRandomFloat(); |
| 197 | expect(typeof randomFloat === 'number').toBe(true); |
| 198 | expect(randomFloat).toBeGreaterThanOrEqual(0); |
| 199 | expect(randomFloat).toBeLessThanOrEqual(Number.MAX_VALUE); |
| 200 | expect(randomFloat).not.toEqual(getRandomFloat()); |
| 201 | expect(() => getRandomFloat(0, 1)).toThrowError(new RangeError('Invalid interval')); |
| 202 | expect(() => getRandomFloat(Number.MAX_VALUE, -Number.MAX_VALUE)).toThrowError( |
| 203 | new RangeError('Invalid interval'), |
| 204 | ); |
| 205 | randomFloat = getRandomFloat(0, -Number.MAX_VALUE); |
| 206 | expect(randomFloat).toBeGreaterThanOrEqual(-Number.MAX_VALUE); |
| 207 | expect(randomFloat).toBeLessThanOrEqual(0); |
| 208 | }); |
| 209 | |
| 210 | it('Verify isObject()', () => { |
| 211 | expect(isObject('test')).toBe(false); |
| 212 | expect(isObject(undefined)).toBe(false); |
| 213 | expect(isObject(null)).toBe(false); |
| 214 | expect(isObject(0)).toBe(false); |
| 215 | expect(isObject([])).toBe(false); |
| 216 | expect(isObject([0, 1])).toBe(false); |
| 217 | expect(isObject(['0', '1'])).toBe(false); |
| 218 | expect(isObject({})).toBe(true); |
| 219 | expect(isObject({ 1: 1 })).toBe(true); |
| 220 | expect(isObject({ '1': '1' })).toBe(true); |
| 221 | expect(isObject(new Map())).toBe(true); |
| 222 | expect(isObject(new Set())).toBe(true); |
| 223 | expect(isObject(new WeakMap())).toBe(true); |
| 224 | expect(isObject(new WeakSet())).toBe(true); |
| 225 | }); |
| 226 | |
| 227 | it('Verify cloneObject()', () => { |
| 228 | const obj = { 1: 1 }; |
| 229 | expect(cloneObject(obj)).toStrictEqual(obj); |
| 230 | expect(cloneObject(obj) === obj).toBe(false); |
| 231 | const array = [1, 2]; |
| 232 | expect(cloneObject(array)).toStrictEqual(array); |
| 233 | expect(cloneObject(array) === array).toBe(false); |
| 234 | const date = new Date(); |
| 235 | expect(cloneObject(date)).toStrictEqual(date); |
| 236 | expect(cloneObject(date) === date).toBe(false); |
| 237 | const map = new Map([['1', '2']]); |
| 238 | expect(cloneObject(map)).toStrictEqual(map); |
| 239 | expect(cloneObject(map) === map).toBe(false); |
| 240 | const set = new Set(['1']); |
| 241 | expect(cloneObject(set)).toStrictEqual(set); |
| 242 | expect(cloneObject(set) === set).toBe(false); |
| 243 | // The URL object seems to have not enumerable properties |
| 244 | const url = new URL('https://domain.tld'); |
| 245 | expect(cloneObject(url)).toStrictEqual(url); |
| 246 | expect(cloneObject(url) === url).toBe(true); |
| 247 | const weakMap = new WeakMap([[{ 1: 1 }, { 2: 2 }]]); |
| 248 | expect(cloneObject(weakMap)).toStrictEqual(weakMap); |
| 249 | expect(cloneObject(weakMap) === weakMap).toBe(true); |
| 250 | const weakSet = new WeakSet([{ 1: 1 }, { 2: 2 }]); |
| 251 | expect(cloneObject(weakSet)).toStrictEqual(weakSet); |
| 252 | expect(cloneObject(weakSet) === weakSet).toBe(true); |
| 253 | }); |
| 254 | |
| 255 | it('Verify hasOwnProp()', () => { |
| 256 | expect(hasOwnProp('test', '')).toBe(false); |
| 257 | expect(hasOwnProp(undefined, '')).toBe(false); |
| 258 | expect(hasOwnProp(null, '')).toBe(false); |
| 259 | expect(hasOwnProp([], '')).toBe(false); |
| 260 | expect(hasOwnProp({}, '')).toBe(false); |
| 261 | expect(hasOwnProp({ 1: 1 }, 1)).toBe(true); |
| 262 | expect(hasOwnProp({ 1: 1 }, '1')).toBe(true); |
| 263 | expect(hasOwnProp({ 1: 1 }, 2)).toBe(false); |
| 264 | expect(hasOwnProp({ 1: 1 }, '2')).toBe(false); |
| 265 | expect(hasOwnProp({ '1': '1' }, '1')).toBe(true); |
| 266 | expect(hasOwnProp({ '1': '1' }, 1)).toBe(true); |
| 267 | expect(hasOwnProp({ '1': '1' }, '2')).toBe(false); |
| 268 | expect(hasOwnProp({ '1': '1' }, 2)).toBe(false); |
| 269 | }); |
| 270 | |
| 271 | it('Verify isIterable()', () => { |
| 272 | expect(isIterable('')).toBe(true); |
| 273 | expect(isIterable(' ')).toBe(true); |
| 274 | expect(isIterable('test')).toBe(true); |
| 275 | expect(isIterable(undefined)).toBe(false); |
| 276 | expect(isIterable(null)).toBe(false); |
| 277 | expect(isIterable(0)).toBe(false); |
| 278 | expect(isIterable([0, 1])).toBe(true); |
| 279 | expect(isIterable({ 1: 1 })).toBe(false); |
| 280 | expect(isIterable(new Map())).toBe(true); |
| 281 | expect(isIterable(new Set())).toBe(true); |
| 282 | expect(isIterable(new WeakMap())).toBe(false); |
| 283 | expect(isIterable(new WeakSet())).toBe(false); |
| 284 | }); |
| 285 | |
| 286 | it('Verify isEmptyString()', () => { |
| 287 | expect(isEmptyString('')).toBe(true); |
| 288 | expect(isEmptyString(' ')).toBe(true); |
| 289 | expect(isEmptyString(' ')).toBe(true); |
| 290 | expect(isEmptyString('test')).toBe(false); |
| 291 | expect(isEmptyString(' test')).toBe(false); |
| 292 | expect(isEmptyString('test ')).toBe(false); |
| 293 | expect(isEmptyString(undefined)).toBe(true); |
| 294 | expect(isEmptyString(null)).toBe(true); |
| 295 | expect(isEmptyString(0)).toBe(false); |
| 296 | expect(isEmptyString({})).toBe(false); |
| 297 | expect(isEmptyString([])).toBe(false); |
| 298 | expect(isEmptyString(new Map())).toBe(false); |
| 299 | expect(isEmptyString(new Set())).toBe(false); |
| 300 | expect(isEmptyString(new WeakMap())).toBe(false); |
| 301 | expect(isEmptyString(new WeakSet())).toBe(false); |
| 302 | }); |
| 303 | |
| 304 | it('Verify isNotEmptyString()', () => { |
| 305 | expect(isNotEmptyString('')).toBe(false); |
| 306 | expect(isNotEmptyString(' ')).toBe(false); |
| 307 | expect(isNotEmptyString(' ')).toBe(false); |
| 308 | expect(isNotEmptyString('test')).toBe(true); |
| 309 | expect(isNotEmptyString(' test')).toBe(true); |
| 310 | expect(isNotEmptyString('test ')).toBe(true); |
| 311 | expect(isNotEmptyString(undefined)).toBe(false); |
| 312 | expect(isNotEmptyString(null)).toBe(false); |
| 313 | expect(isNotEmptyString(0)).toBe(false); |
| 314 | expect(isNotEmptyString({})).toBe(false); |
| 315 | expect(isNotEmptyString([])).toBe(false); |
| 316 | expect(isNotEmptyString(new Map())).toBe(false); |
| 317 | expect(isNotEmptyString(new Set())).toBe(false); |
| 318 | expect(isNotEmptyString(new WeakMap())).toBe(false); |
| 319 | expect(isNotEmptyString(new WeakSet())).toBe(false); |
| 320 | }); |
| 321 | |
| 322 | it('Verify isUndefined()', () => { |
| 323 | expect(isUndefined(undefined)).toBe(true); |
| 324 | expect(isUndefined(null)).toBe(false); |
| 325 | expect(isUndefined('')).toBe(false); |
| 326 | expect(isUndefined(0)).toBe(false); |
| 327 | expect(isUndefined({})).toBe(false); |
| 328 | expect(isUndefined([])).toBe(false); |
| 329 | expect(isUndefined(new Map())).toBe(false); |
| 330 | expect(isUndefined(new Set())).toBe(false); |
| 331 | expect(isUndefined(new WeakMap())).toBe(false); |
| 332 | expect(isUndefined(new WeakSet())).toBe(false); |
| 333 | }); |
| 334 | |
| 335 | it('Verify isNullOrUndefined()', () => { |
| 336 | expect(isNullOrUndefined(undefined)).toBe(true); |
| 337 | expect(isNullOrUndefined(null)).toBe(true); |
| 338 | expect(isNullOrUndefined('')).toBe(false); |
| 339 | expect(isNullOrUndefined(0)).toBe(false); |
| 340 | expect(isNullOrUndefined({})).toBe(false); |
| 341 | expect(isNullOrUndefined([])).toBe(false); |
| 342 | expect(isNullOrUndefined(new Map())).toBe(false); |
| 343 | expect(isNullOrUndefined(new Set())).toBe(false); |
| 344 | expect(isNullOrUndefined(new WeakMap())).toBe(false); |
| 345 | expect(isNullOrUndefined(new WeakSet())).toBe(false); |
| 346 | }); |
| 347 | |
| 348 | it('Verify isEmptyArray()', () => { |
| 349 | expect(isEmptyArray([])).toBe(true); |
| 350 | expect(isEmptyArray([1, 2])).toBe(false); |
| 351 | expect(isEmptyArray(['1', '2'])).toBe(false); |
| 352 | expect(isEmptyArray(undefined)).toBe(false); |
| 353 | expect(isEmptyArray(null)).toBe(false); |
| 354 | expect(isEmptyArray('')).toBe(false); |
| 355 | expect(isEmptyArray('test')).toBe(false); |
| 356 | expect(isEmptyArray(0)).toBe(false); |
| 357 | expect(isEmptyArray({})).toBe(false); |
| 358 | expect(isEmptyArray(new Map())).toBe(false); |
| 359 | expect(isEmptyArray(new Set())).toBe(false); |
| 360 | expect(isEmptyArray(new WeakMap())).toBe(false); |
| 361 | expect(isEmptyArray(new WeakSet())).toBe(false); |
| 362 | }); |
| 363 | |
| 364 | it('Verify isNotEmptyArray()', () => { |
| 365 | expect(isNotEmptyArray([])).toBe(false); |
| 366 | expect(isNotEmptyArray([1, 2])).toBe(true); |
| 367 | expect(isNotEmptyArray(['1', '2'])).toBe(true); |
| 368 | expect(isNotEmptyArray(undefined)).toBe(false); |
| 369 | expect(isNotEmptyArray(null)).toBe(false); |
| 370 | expect(isNotEmptyArray('')).toBe(false); |
| 371 | expect(isNotEmptyArray('test')).toBe(false); |
| 372 | expect(isNotEmptyArray(0)).toBe(false); |
| 373 | expect(isNotEmptyArray({})).toBe(false); |
| 374 | expect(isNotEmptyArray(new Map())).toBe(false); |
| 375 | expect(isNotEmptyArray(new Set())).toBe(false); |
| 376 | expect(isNotEmptyArray(new WeakMap())).toBe(false); |
| 377 | expect(isNotEmptyArray(new WeakSet())).toBe(false); |
| 378 | }); |
| 379 | |
| 380 | it('Verify isEmptyObject()', () => { |
| 381 | expect(isEmptyObject({})).toBe(true); |
| 382 | expect(isEmptyObject({ 1: 1, 2: 2 })).toBe(false); |
| 383 | expect(isEmptyObject(new Map())).toBe(false); |
| 384 | expect(isEmptyObject(new Set())).toBe(false); |
| 385 | expect(isEmptyObject(new WeakMap())).toBe(false); |
| 386 | expect(isEmptyObject(new WeakSet())).toBe(false); |
| 387 | }); |
| 388 | |
| 389 | it('Verify isArraySorted()', () => { |
| 390 | expect( |
| 391 | isArraySorted([], (a, b) => { |
| 392 | return a - b; |
| 393 | }), |
| 394 | ).toBe(true); |
| 395 | expect( |
| 396 | isArraySorted([1], (a, b) => { |
| 397 | return a - b; |
| 398 | }), |
| 399 | ).toBe(true); |
| 400 | expect(isArraySorted<number>([1, 2, 3, 4, 5], (a, b) => a - b)).toBe(true); |
| 401 | expect(isArraySorted<number>([1, 2, 3, 5, 4], (a, b) => a - b)).toBe(false); |
| 402 | }); |
| 403 | }); |