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