(n: number): Option.Option<BigDecimal>Creates a BigDecimal safely from a finite number.
When to use
Use to convert a finite JavaScript number to a BigDecimal without throwing
on invalid input.
Details
Returns Option.none() for NaN, +Infinity or -Infinity.
Gotchas
It is not recommended to convert a floating point number to a decimal directly, as the floating point representation may be unexpected.
Example (Creating decimals from numbers safely)
import { BigDecimal, Option } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(BigDecimal.fromNumber(123), Option.some(BigDecimal.make(123n, 0)))
assert.deepStrictEqual(
BigDecimal.fromNumber(123.456),
Option.some(BigDecimal.make(123456n, 3))
)
assert.deepStrictEqual(BigDecimal.fromNumber(Infinity), Option.none())export const const fromNumber: (
n: number
) => Option.Option<BigDecimal>
Creates a BigDecimal safely from a finite number.
When to use
Use to convert a finite JavaScript number to a BigDecimal without throwing
on invalid input.
Details
Returns Option.none() for NaN, +Infinity or -Infinity.
Gotchas
It is not recommended to convert a floating point number to a decimal
directly, as the floating point representation may be unexpected.
Example (Creating decimals from numbers safely)
import { BigDecimal, Option } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(BigDecimal.fromNumber(123), Option.some(BigDecimal.make(123n, 0)))
assert.deepStrictEqual(
BigDecimal.fromNumber(123.456),
Option.some(BigDecimal.make(123456n, 3))
)
assert.deepStrictEqual(BigDecimal.fromNumber(Infinity), Option.none())
fromNumber = (n: numbern: number): import OptionOption.type Option<A> = Option.None<A> | Option.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<BigDecimal> => {
if (!var Number: NumberConstructorAn object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.
Number.NumberConstructor.isFinite(number: unknown): booleanReturns true if passed value is finite.
Unlike the global isFinite, Number.isFinite doesn't forcibly convert the parameter to a
number. Only finite values of the type number, result in true.
isFinite(n: numbern)) {
return import OptionOption.const none: <A = never>() => Option<A>Creates an Option representing the absence of a value.
When to use
Use to represent a missing or uninitialized value, such as returning "no
result" from a function.
Details
- Returns
Option<never>, which is a subtype of Option<A> for any A
- Always returns the same singleton instance
Example (Creating an empty Option)
import { Option } from "effect"
// ┌─── Option<never>
// ▼
const noValue = Option.none()
console.log(noValue)
// Output: { _id: 'Option', _tag: 'None' }
none()
}
const const string: stringstring = `${n: numbern}`
if (const string: stringstring.String.includes(searchString: string, position?: number): booleanReturns true if searchString appears as a substring of the result of converting this
object to a String, at one or more positions that are
greater than or equal to position; otherwise, returns false.
includes("e")) {
return const fromString: (
s: string
) => Option.Option<BigDecimal>
Parses a decimal string into a BigDecimal safely.
When to use
Use to parse external decimal text without throwing on invalid input.
Details
Returns Option.some for valid decimal or exponent notation and
Option.none when the string cannot be parsed or would produce an unsafe
scale. The empty string parses as zero.
Example (Parsing decimal strings safely)
import { BigDecimal, Option } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(BigDecimal.fromString("123"), Option.some(BigDecimal.make(123n, 0)))
assert.deepStrictEqual(
BigDecimal.fromString("123.456"),
Option.some(BigDecimal.make(123456n, 3))
)
assert.deepStrictEqual(BigDecimal.fromString("123.abc"), Option.none())
fromString(const string: stringstring)
}
const [const lead: stringlead, const trail: stringtrail = ""] = const string: stringstring.String.split(separator: string | RegExp, limit?: number): string[] (+1 overload)Split a string into substrings using the specified separator and return them as an array.
split(".")
return import OptionOption.const some: <A>(value: A) => Option<A>Wraps the given value into an Option to represent its presence.
When to use
Use to wrap a known present value as Option
- Returning a successful result from a partial function
Details
- Always returns
Some<A>
- Does not filter
null or undefined; use
fromNullishOr
for that
Example (Wrapping a value)
import { Option } from "effect"
// ┌─── Option<number>
// ▼
const value = Option.some(1)
console.log(value)
// Output: { _id: 'Option', _tag: 'Some', value: 1 }
some(const make: (
value: bigint,
scale: number
) => BigDecimal
Creates a BigDecimal from a bigint value and a scale.
When to use
Use to construct a decimal directly from its unscaled integer value and
decimal scale.
Example (Creating decimals from bigint and scale)
import { BigDecimal } from "effect"
// Create 123.45 (12345 with scale 2)
const decimal = BigDecimal.make(12345n, 2)
console.log(BigDecimal.format(decimal)) // "123.45"
// Create 42 (42 with scale 0)
const integer = BigDecimal.make(42n, 0)
console.log(BigDecimal.format(integer)) // "42"
make(var BigInt: BigIntConstructor
;(value: bigint | boolean | number | string) =>
bigint
BigInt(`${const lead: stringlead}${const trail: stringtrail}`), const trail: stringtrail.String.length: numberReturns the length of a String object.
length))
}