Skip to content

Commit

Permalink
version(0.3.1): Improve JSDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianWendelborn committed Nov 24, 2024
1 parent 069b758 commit f1b85a5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 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.3.0"
"version": "0.3.1"
}
2 changes: 0 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ Calculates the maximum of the provided numbers. Ignores `null` and `undefined`.

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`

Converts the input to either `number | null`. General-purpose equivalent of `nm.end()`
Expand Down
42 changes: 42 additions & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export class NullishMath {
this.#value = NullishMath.unwrap(value)
}

/**
* 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.
*/
static average = (
numbers: NullishNumber[],
options: {
Expand Down Expand Up @@ -36,6 +39,9 @@ export class NullishMath {
return nm(sumValid).divide(countValid)
}

/**
* Calculates the maximum of the provided numbers. Ignores `null` and `undefined`. Returns `null` if no proper number was provided
*/
static max = (numbers: NullishNumber[]): NullishMath => {
let max: number | null = null

Expand All @@ -50,6 +56,9 @@ export class NullishMath {
return nm(max)
}

/**
* Calculates the minimum of the provided numbers. Ignores `null` and `undefined`. Returns `null` if no proper number was provided
*/
static min = (numbers: NullishNumber[]): NullishMath => {
let min: number | null = null

Expand All @@ -64,13 +73,19 @@ export class NullishMath {
return nm(min)
}

/**
* Converts the input to either `number | null`. General-purpose equivalent of `nm.end()`
*/
static unwrap(value: NullishNumber) {
if (value === null) return null
if (value === undefined) return null
if (typeof value === 'number') return value
return value.end()
}

/**
* Returns a new instance of `NullishMath` with the sum of the current value and the given number.
*/
add(number: NullishNumber): NullishMath {
const n = NullishMath.unwrap(number)

Expand All @@ -79,6 +94,9 @@ export class NullishMath {
return new NullishMath(this.#value + n)
}

/**
* Returns a new instance of `NullishMath` with the sum of the current value and the given numbers.
*/
addMany(...numbers: NullishNumber[]) {
let result = this.#value

Expand Down Expand Up @@ -151,6 +169,9 @@ export class NullishMath {
return this.#value !== n
}

/**
* Returns a new instance of `NullishMath` with the difference of the current value and the given number.
*/
subtract(number: NullishNumber): NullishMath {
const n = NullishMath.unwrap(number)

Expand All @@ -159,6 +180,9 @@ export class NullishMath {
return new NullishMath(this.#value - n)
}

/**
* Returns a new instance of `NullishMath` with the difference of the current value and the given numbers.
*/
subtractMany(...numbers: NullishNumber[]) {
let result = this.#value

Expand All @@ -175,6 +199,9 @@ export class NullishMath {
return new NullishMath(result)
}

/**
* Returns a new instance of `NullishMath` with the product of the current value and the given number.
*/
multiply(number: NullishNumber): NullishMath {
const n = NullishMath.unwrap(number)

Expand All @@ -183,6 +210,9 @@ export class NullishMath {
return new NullishMath(this.#value * n)
}

/**
* Returns a new instance of `NullishMath` with the product of the current value and the given numbers.
*/
multiplyMany(...numbers: NullishNumber[]) {
let result = this.#value

Expand All @@ -199,6 +229,9 @@ export class NullishMath {
return new NullishMath(result)
}

/**
* Returns a new instance of `NullishMath` with the quotient of the current value and the given number.
*/
divide(number: NullishNumber): NullishMath {
const n = NullishMath.unwrap(number)

Expand All @@ -207,6 +240,9 @@ export class NullishMath {
return new NullishMath(this.#value / n)
}

/**
* Returns a new instance of `NullishMath` with the quotient of the current value and the given numbers.
*/
divideMany(...numbers: NullishNumber[]) {
let result = this.#value

Expand All @@ -223,11 +259,17 @@ export class NullishMath {
return new NullishMath(result)
}

/**
* 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`.
*/
end() {
return this.#value
}
}

/**
* Creates a new `NullishMath` object with an initial value.
*/
export function nm(value: NullishNumber) {
return new NullishMath(value)
}

0 comments on commit f1b85a5

Please sign in to comment.