Number overview
This module provides utility functions and type class instances for working with the number
type in TypeScript. It includes functions for basic arithmetic operations, as well as type class instances for Equivalence
and Order
.
Added in v2.0.0
Table of contents
constructors
parse
Tries to parse a number
from a string
using the Number()
function. The following special string values are supported: “NaN”, “Infinity”, “-Infinity”.
Signature
export declare const parse: (s: string) => Option<number>
Added in v2.0.0
guards
isNumber
Tests if a value is a number
.
Signature
export declare const isNumber: (input: unknown) => input is number
Example
import { isNumber } from "effect/Number"
assert.deepStrictEqual(isNumber(2), true)
assert.deepStrictEqual(isNumber("2"), false)
Added in v2.0.0
instances
Equivalence
Signature
export declare const Equivalence: equivalence.Equivalence<number>
Added in v2.0.0
Order
Signature
export declare const Order: order.Order<number>
Added in v2.0.0
math
decrement
Decrements a number by 1
.
Signature
export declare const decrement: (n: number) => number
Example
import { decrement } from "effect/Number"
assert.deepStrictEqual(decrement(3), 2)
Added in v2.0.0
divide
Provides a division operation on number
s.
Signature
export declare const divide: {
(that: number): (self: number) => Option<number>
(self: number, that: number): Option<number>
}
Example
import { Number, Option } from "effect"
assert.deepStrictEqual(Number.divide(6, 3), Option.some(2))
assert.deepStrictEqual(Number.divide(6, 0), Option.none())
Added in v2.0.0
increment
Returns the result of adding 1
to a given number.
Signature
export declare const increment: (n: number) => number
Example
import { increment } from "effect/Number"
assert.deepStrictEqual(increment(2), 3)
Added in v2.0.0
multiply
Provides a multiplication operation on number
s.
Signature
export declare const multiply: { (that: number): (self: number) => number; (self: number, that: number): number }
Example
import { multiply } from "effect/Number"
assert.deepStrictEqual(multiply(2, 3), 6)
Added in v2.0.0
multiplyAll
Takes an Iterable
of number
s and returns their multiplication as a single number
.
Signature
export declare const multiplyAll: (collection: Iterable<number>) => number
Example
import { multiplyAll } from "effect/Number"
assert.deepStrictEqual(multiplyAll([2, 3, 4]), 24)
Added in v2.0.0
nextPow2
Returns the next power of 2 from the given number.
Signature
export declare const nextPow2: (n: number) => number
Example
import { nextPow2 } from "effect/Number"
assert.deepStrictEqual(nextPow2(5), 8)
assert.deepStrictEqual(nextPow2(17), 32)
Added in v2.0.0
remainder
Returns the remainder left over when one operand is divided by a second operand.
It always takes the sign of the dividend.
Signature
export declare const remainder: { (divisor: number): (self: number) => number; (self: number, divisor: number): number }
Example
import { remainder } from "effect/Number"
assert.deepStrictEqual(remainder(2, 2), 0)
assert.deepStrictEqual(remainder(3, 2), 1)
assert.deepStrictEqual(remainder(-4, 2), -0)
Added in v2.0.0
round
Returns the number rounded with the given precision.
Signature
export declare const round: { (precision: number): (self: number) => number; (self: number, precision: number): number }
Example
import { round } from "effect/Number"
assert.deepStrictEqual(round(1.1234, 2), 1.12)
assert.deepStrictEqual(round(1.567, 2), 1.57)
Added in v3.8.0
sign
Determines the sign of a given number
.
Signature
export declare const sign: (n: number) => Ordering
Example
import { sign } from "effect/Number"
assert.deepStrictEqual(sign(-5), -1)
assert.deepStrictEqual(sign(0), 0)
assert.deepStrictEqual(sign(5), 1)
Added in v2.0.0
subtract
Provides a subtraction operation on number
s.
Signature
export declare const subtract: { (that: number): (self: number) => number; (self: number, that: number): number }
Example
import { subtract } from "effect/Number"
assert.deepStrictEqual(subtract(2, 3), -1)
Added in v2.0.0
sum
Provides an addition operation on number
s.
Signature
export declare const sum: { (that: number): (self: number) => number; (self: number, that: number): number }
Example
import { sum } from "effect/Number"
assert.deepStrictEqual(sum(2, 3), 5)
Added in v2.0.0
sumAll
Takes an Iterable
of number
s and returns their sum as a single number
.
Signature
export declare const sumAll: (collection: Iterable<number>) => number
Example
import { sumAll } from "effect/Number"
assert.deepStrictEqual(sumAll([2, 3, 4]), 9)
Added in v2.0.0
unsafeDivide
Provides a division operation on number
s.
Throws a RangeError
if the divisor is 0
.
Signature
export declare const unsafeDivide: { (that: number): (self: number) => number; (self: number, that: number): number }
Example
import { unsafeDivide } from "effect/Number"
assert.deepStrictEqual(unsafeDivide(6, 3), 2)
Added in v2.0.0
predicates
between
Checks if a number
is between a minimum
and maximum
value (inclusive).
Signature
export declare const between: {
(options: { minimum: number; maximum: number }): (self: number) => boolean
(self: number, options: { minimum: number; maximum: number }): boolean
}
Example
import { Number } from "effect"
const between = Number.between({ minimum: 0, maximum: 5 })
assert.deepStrictEqual(between(3), true)
assert.deepStrictEqual(between(-1), false)
assert.deepStrictEqual(between(6), false)
Added in v2.0.0
greaterThan
Returns true
if the first argument is greater than the second, otherwise false
.
Signature
export declare const greaterThan: { (that: number): (self: number) => boolean; (self: number, that: number): boolean }
Example
import { greaterThan } from "effect/Number"
assert.deepStrictEqual(greaterThan(2, 3), false)
assert.deepStrictEqual(greaterThan(3, 3), false)
assert.deepStrictEqual(greaterThan(4, 3), true)
Added in v2.0.0
greaterThanOrEqualTo
Returns a function that checks if a given number
is greater than or equal to the provided one.
Signature
export declare const greaterThanOrEqualTo: {
(that: number): (self: number) => boolean
(self: number, that: number): boolean
}
Example
import { greaterThanOrEqualTo } from "effect/Number"
assert.deepStrictEqual(greaterThanOrEqualTo(2, 3), false)
assert.deepStrictEqual(greaterThanOrEqualTo(3, 3), true)
assert.deepStrictEqual(greaterThanOrEqualTo(4, 3), true)
Added in v2.0.0
lessThan
Returns true
if the first argument is less than the second, otherwise false
.
Signature
export declare const lessThan: { (that: number): (self: number) => boolean; (self: number, that: number): boolean }
Example
import { lessThan } from "effect/Number"
assert.deepStrictEqual(lessThan(2, 3), true)
assert.deepStrictEqual(lessThan(3, 3), false)
assert.deepStrictEqual(lessThan(4, 3), false)
Added in v2.0.0
lessThanOrEqualTo
Returns a function that checks if a given number
is less than or equal to the provided one.
Signature
export declare const lessThanOrEqualTo: {
(that: number): (self: number) => boolean
(self: number, that: number): boolean
}
Example
import { lessThanOrEqualTo } from "effect/Number"
assert.deepStrictEqual(lessThanOrEqualTo(2, 3), true)
assert.deepStrictEqual(lessThanOrEqualTo(3, 3), true)
assert.deepStrictEqual(lessThanOrEqualTo(4, 3), false)
Added in v2.0.0
utils
clamp
Restricts the given number
to be within the range specified by the minimum
and maximum
values.
- If the
number
is less than theminimum
value, the function returns theminimum
value. - If the
number
is greater than themaximum
value, the function returns themaximum
value. - Otherwise, it returns the original
number
.
Signature
export declare const clamp: {
(options: { minimum: number; maximum: number }): (self: number) => number
(self: number, options: { minimum: number; maximum: number }): number
}
Example
import { Number } from "effect"
const clamp = Number.clamp({ minimum: 1, maximum: 5 })
assert.equal(clamp(3), 3)
assert.equal(clamp(0), 1)
assert.equal(clamp(6), 5)
Added in v2.0.0
max
Returns the maximum between two number
s.
Signature
export declare const max: { (that: number): (self: number) => number; (self: number, that: number): number }
Example
import { max } from "effect/Number"
assert.deepStrictEqual(max(2, 3), 3)
Added in v2.0.0
min
Returns the minimum between two number
s.
Signature
export declare const min: { (that: number): (self: number) => number; (self: number, that: number): number }
Example
import { min } from "effect/Number"
assert.deepStrictEqual(min(2, 3), 2)
Added in v2.0.0