(options: { minimum: BigDecimal; maximum: BigDecimal }): (
self: BigDecimal
) => BigDecimal
(
self: BigDecimal,
options: { minimum: BigDecimal; maximum: BigDecimal }
): BigDecimalRestricts the given BigDecimal to be within the range specified by the minimum and maximum values.
When to use
Use to force a BigDecimal into an inclusive range.
Details
If the BigDecimal is less than the minimum value, the function returns
the minimum value. If it is greater than the maximum value, the function
returns the maximum value. Otherwise, it returns the original BigDecimal.
Example (Clamping decimals to a range)
import { BigDecimal } from "effect"
import * as assert from "node:assert"
const clamp = BigDecimal.clamp({
minimum: BigDecimal.fromStringUnsafe("1"),
maximum: BigDecimal.fromStringUnsafe("5")
})
assert.deepStrictEqual(
clamp(BigDecimal.fromStringUnsafe("3")),
BigDecimal.fromStringUnsafe("3")
)
assert.deepStrictEqual(
clamp(BigDecimal.fromStringUnsafe("0")),
BigDecimal.fromStringUnsafe("1")
)
assert.deepStrictEqual(
clamp(BigDecimal.fromStringUnsafe("6")),
BigDecimal.fromStringUnsafe("5")
)export const const clamp: {
(options: {
minimum: BigDecimal
maximum: BigDecimal
}): (self: BigDecimal) => BigDecimal
(
self: BigDecimal,
options: {
minimum: BigDecimal
maximum: BigDecimal
}
): BigDecimal
}
Restricts the given BigDecimal to be within the range specified by the minimum and maximum values.
When to use
Use to force a BigDecimal into an inclusive range.
Details
If the BigDecimal is less than the minimum value, the function returns
the minimum value. If it is greater than the maximum value, the function
returns the maximum value. Otherwise, it returns the original BigDecimal.
Example (Clamping decimals to a range)
import { BigDecimal } from "effect"
import * as assert from "node:assert"
const clamp = BigDecimal.clamp({
minimum: BigDecimal.fromStringUnsafe("1"),
maximum: BigDecimal.fromStringUnsafe("5")
})
assert.deepStrictEqual(
clamp(BigDecimal.fromStringUnsafe("3")),
BigDecimal.fromStringUnsafe("3")
)
assert.deepStrictEqual(
clamp(BigDecimal.fromStringUnsafe("0")),
BigDecimal.fromStringUnsafe("1")
)
assert.deepStrictEqual(
clamp(BigDecimal.fromStringUnsafe("6")),
BigDecimal.fromStringUnsafe("5")
)
clamp: {
(options: {
minimum: BigDecimal
maximum: BigDecimal
}
options: {
minimum: BigDecimal(property) minimum: {
value: bigint;
scale: number;
normalized: BigDecimal;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
minimum: BigDecimal
maximum: BigDecimal(property) maximum: {
value: bigint;
scale: number;
normalized: BigDecimal;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
maximum: BigDecimal
}): (self: BigDecimal(parameter) self: {
value: bigint;
scale: number;
normalized: BigDecimal;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
self: BigDecimal) => BigDecimal
(self: BigDecimal(parameter) self: {
value: bigint;
scale: number;
normalized: BigDecimal;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
self: BigDecimal, options: {
minimum: BigDecimal
maximum: BigDecimal
}
options: {
minimum: BigDecimal(property) minimum: {
value: bigint;
scale: number;
normalized: BigDecimal;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
minimum: BigDecimal
maximum: BigDecimal(property) maximum: {
value: bigint;
scale: number;
normalized: BigDecimal;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
maximum: BigDecimal
}): BigDecimal
} = import orderorder.const clamp: <A>(O: Order<A>) => {
(options: { minimum: A; maximum: A }): (
self: A
) => A
(
self: A,
options: { minimum: A; maximum: A }
): A
}
Restricts a value between a minimum and a maximum according to the given order.
When to use
Use when you need to clamp a value to an inclusive range according to an
Order.
Details
Returns the value itself when it is between minimum and maximum, inclusive.
Values below the range return minimum, and values above the range return
maximum. The minimum must be less than or equal to the maximum according to
the order.
Example (Clamping values)
import { Order } from "effect"
const clamp = Order.clamp(Order.Number)({ minimum: 1, maximum: 5 })
console.log(clamp(3)) // 3
console.log(clamp(0)) // 1
console.log(clamp(6)) // 5
clamp(const Order: order.Order<BigDecimal>Provides an Order instance for BigDecimal that allows comparing and sorting BigDecimal values.
When to use
Use when you need to sort or compare decimal values through APIs that accept
an ordering instance.
Example (Comparing decimals)
import { BigDecimal } from "effect"
const a = BigDecimal.fromNumberUnsafe(1.5)
const b = BigDecimal.fromNumberUnsafe(2.3)
const c = BigDecimal.fromNumberUnsafe(1.5)
console.log(BigDecimal.Order(a, b)) // -1 (a < b)
console.log(BigDecimal.Order(b, a)) // 1 (b > a)
console.log(BigDecimal.Order(a, c)) // 0 (a === c)
Order)