import { expect } from '@std/expect'
import { describe, it } from 'node:test'
-import { max, min, nthPercentile, stdDeviation } from '../../src/utils/StatisticUtils.js'
+import {
+ max,
+ mean,
+ median,
+ min,
+ nthPercentile,
+ stdDeviation,
+} from '../../src/utils/StatisticUtils.js'
await describe('StatisticUtils test suite', async () => {
+ await it('Verify mean()', () => {
+ expect(mean([])).toBe(0)
+ expect(mean([0.08])).toBe(0.08)
+ expect(mean([0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03])).toBe(3.1642857142857146)
+ expect(mean([0.25, 4.75, 3.05, 6.04, 1.01, 2.02])).toBe(2.8533333333333335)
+ })
+
+ await it('Verify median()', () => {
+ expect(median([])).toBe(0)
+ expect(median([0.08])).toBe(0.08)
+ expect(median([0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03])).toBe(3.05)
+ expect(median([0.25, 4.75, 3.05, 6.04, 1.01, 2.02])).toBe(2.535)
+ })
+
await it('Verify min()', () => {
expect(min()).toBe(Number.POSITIVE_INFINITY)
expect(min(0, 1)).toBe(0)
formatDurationSeconds,
generateUUID,
getRandomFloat,
+ has,
insertAt,
isArraySorted,
isAsyncFunction,
+ isEmpty,
isNotEmptyArray,
isNotEmptyString,
isValidDate,
+ once,
roundTo,
secureRandom,
sleep,
expect(() => clone(weakSet)).toThrow(new Error('#<WeakSet> could not be cloned.'))
})
+ await it('Verify once()', () => {
+ let called = 0
+ const fn = (): number => ++called
+ const onceFn = once(fn)
+ const result1 = onceFn()
+ expect(called).toBe(1)
+ expect(result1).toBe(1)
+ const result2 = onceFn()
+ expect(called).toBe(1)
+ expect(result2).toBe(1)
+ const result3 = onceFn()
+ expect(called).toBe(1)
+ expect(result3).toBe(1)
+ })
+
+ await it('Verify has()', () => {
+ expect(has('', 'test')).toBe(false)
+ expect(has('test', '')).toBe(false)
+ expect(has('test', 'test')).toBe(false)
+ expect(has('', undefined)).toBe(false)
+ expect(has('', null)).toBe(false)
+ expect(has('', [])).toBe(false)
+ expect(has('', {})).toBe(false)
+ expect(has(1, { 1: 1 })).toBe(true)
+ expect(has('1', { 1: 1 })).toBe(true)
+ expect(has(2, { 1: 1 })).toBe(false)
+ expect(has('2', { 1: 1 })).toBe(false)
+ expect(has('1', { 1: '1' })).toBe(true)
+ expect(has(1, { 1: '1' })).toBe(true)
+ expect(has('2', { 1: '1' })).toBe(false)
+ expect(has(2, { 1: '1' })).toBe(false)
+ })
+
+ await it('Verify isEmpty()', () => {
+ expect(isEmpty('')).toBe(true)
+ expect(isEmpty(' ')).toBe(false)
+ expect(isEmpty(' ')).toBe(false)
+ expect(isEmpty('test')).toBe(false)
+ expect(isEmpty(' test')).toBe(false)
+ expect(isEmpty('test ')).toBe(false)
+ expect(isEmpty(undefined)).toBe(false)
+ expect(isEmpty(null)).toBe(false)
+ expect(isEmpty(0)).toBe(false)
+ expect(isEmpty({})).toBe(true)
+ expect(isEmpty([])).toBe(true)
+ expect(isEmpty(new Map())).toBe(true)
+ expect(isEmpty(new Set())).toBe(true)
+ expect(isEmpty(new WeakMap())).toBe(false)
+ expect(isEmpty(new WeakSet())).toBe(false)
+ })
+
await it('Verify isNotEmptyString()', () => {
expect(isNotEmptyString('')).toBe(false)
expect(isNotEmptyString(' ')).toBe(false)