Skip to content

Commit

Permalink
version(0.2.0): Add NullishMath.max, NullishMath.min
Browse files Browse the repository at this point in the history
also improve tests
  • Loading branch information
FlorianWendelborn committed Oct 27, 2024
1 parent 71f3401 commit 59fb267
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@
"test": "bun test"
},
"types": "dist/index.d.ts",
"version": "0.1.0"
"version": "0.2.0"
}
10 changes: 9 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,18 @@ Returns a new instance of `NullishMath` with the quotient of the current value a

Returns the final value of the `NullishMath` instance. If any of the values passed to the math operation methods are `null` or `undefined`, the final value will be `null`.

### `NullishMath.average(Array<NullishMath | number | null | undefined>, options?: { treatNullishAsZero?: boolean }): number | null`
### `NullishMath.average(Array<NullishMath | number | null | undefined>, options?: { treatNullishAsZero?: boolean }): NullishMath`

Calculates the average of the provided numbers. By default, `null`s are excluded from the average. This can be changed by setting the `treatNullishAsZero` option. With this flag, nullish numbers get counted as a `0` and thus impact the average.

### `NullishMath.max(Array<NullishMath | number | null | undefined>): NullishMath`

Calculates the maximum of the provided numbers. Ignores `null` and `undefined`. Returns `null` if no proper number was provided

### `NullishMath.min(Array<NullishMath | number | null | undefined>): NullishMath`

Calculates the minimum of the provided numbers. Ignores `null` and `undefined`. Returns `null` if no proper number was provided

Returns `null` on division by zero unless `treatNullishAsZero` is set (in which case it returns `0`).

### `NullishMath.unwrap(NullishMath | number | null | undefined): number | null`
Expand Down
42 changes: 42 additions & 0 deletions source/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@ describe('NullishMath static methods', () => {
expect(NullishMath.average([null, 10, 20], o).end()).toBe(10)
expect(NullishMath.average([42, 1337], o).end()).toBe(689.5)
})

it('correctly implements NullishMath.max()', () => {
expect(NullishMath.max([]).end()).toBe(null)
expect(NullishMath.max([null, undefined]).end()).toBe(null)
expect(NullishMath.max([null, null]).end()).toBe(null)
expect(NullishMath.max([undefined, 42]).end()).toBe(42)
expect(NullishMath.max([null, 42]).end()).toBe(42)
expect(NullishMath.max([undefined, 42, 1337]).end()).toBe(1337)
expect(NullishMath.max([null, 42, 1337]).end()).toBe(1337)
expect(NullishMath.max([42, 1337]).end()).toBe(1337)
})

it('correctly implements NullishMath.min()', () => {
expect(NullishMath.min([]).end()).toBe(null)
expect(NullishMath.min([null, undefined]).end()).toBe(null)
expect(NullishMath.min([null, null]).end()).toBe(null)
expect(NullishMath.min([undefined, 42]).end()).toBe(42)
expect(NullishMath.min([null, 42]).end()).toBe(42)
expect(NullishMath.min([undefined, 42, 1337]).end()).toBe(42)
expect(NullishMath.min([null, 42, 1337]).end()).toBe(42)
expect(NullishMath.min([42, 1337]).end()).toBe(42)
})
})

describe('nm.add()', () => {
Expand Down Expand Up @@ -112,6 +134,10 @@ describe('comparison operators', () => {
// @ts-expect-error not allowed to pass a value that’s always nullish
expect(nm(undefined).eq(null)).toBe(true)

// @ts-expect-error not allowed to pass a value that’s always nullish
expect(nm(undefined).eq(42)).toBe(false)
expect(nm(42).eq(undefined)).toBe(false)

expect(nm(1).eq(0)).toBe(false)
expect(nm(1).eq(1)).toBe(true)
expect(nm(1).eq(2)).toBe(false)
Expand All @@ -125,6 +151,10 @@ describe('comparison operators', () => {
// @ts-expect-error not allowed to pass a value that’s always nullish
expect(nm(undefined).lt(null)).toBe(null)

// @ts-expect-error not allowed to pass a value that’s always nullish
expect(nm(undefined).lt(42)).toBe(null)
expect(nm(42).lt(undefined)).toBe(null)

expect(nm(1).lt(0)).toBe(false)
expect(nm(1).lt(1)).toBe(false)
expect(nm(1).lt(2)).toBe(true)
Expand All @@ -138,6 +168,10 @@ describe('comparison operators', () => {
// @ts-expect-error not allowed to pass a value that’s always nullish
expect(nm(undefined).lte(null)).toBe(null)

// @ts-expect-error not allowed to pass a value that’s always nullish
expect(nm(undefined).lte(42)).toBe(null)
expect(nm(42).lte(undefined)).toBe(null)

expect(nm(1).lte(0)).toBe(false)
expect(nm(1).lte(1)).toBe(true)
expect(nm(1).lte(2)).toBe(true)
Expand All @@ -151,6 +185,10 @@ describe('comparison operators', () => {
// @ts-expect-error not allowed to pass a value that’s always nullish
expect(nm(undefined).gt(null)).toBe(null)

// @ts-expect-error not allowed to pass a value that’s always nullish
expect(nm(undefined).gt(42)).toBe(null)
expect(nm(42).gt(undefined)).toBe(null)

expect(nm(1).gt(0)).toBe(true)
expect(nm(1).gt(1)).toBe(false)
expect(nm(1).gt(2)).toBe(false)
Expand All @@ -164,6 +202,10 @@ describe('comparison operators', () => {
// @ts-expect-error not allowed to pass a value that’s always nullish
expect(nm(undefined).gte(null)).toBe(null)

// @ts-expect-error not allowed to pass a value that’s always nullish
expect(nm(undefined).gte(42)).toBe(null)
expect(nm(42).gte(undefined)).toBe(null)

expect(nm(1).gte(0)).toBe(true)
expect(nm(1).gte(1)).toBe(true)
expect(nm(1).gte(2)).toBe(false)
Expand Down
34 changes: 34 additions & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,40 @@ export class NullishMath<T extends NullishNumber> {
return nm(sumValid).divide(countValid)
}

static max = (
numbers: NullishNumber[],
): NullishMath<NullishNumber> => {
let max: number | null = null

for (const rawNumber of numbers) {
const number = NullishMath.unwrap(rawNumber)

if (number === null) continue

if (max === null || number > max)
max = number
}

return nm(max)
}

static min = (
numbers: NullishNumber[],
): NullishMath<NullishNumber> => {
let min: number | null = null

for (const rawNumber of numbers) {
const number = NullishMath.unwrap(rawNumber)

if (number === null) continue

if (min === null || number < min)
min = number
}

return nm(min)
}

static unwrap(value: NullishNumber) {
if (value === null) return null
if (value === undefined) return null
Expand Down

0 comments on commit 59fb267

Please sign in to comment.