Skip to main content

Number

A collection of functions to manipulate number

@example
import { Number } from '@w5s/core';

const total = [1, 1.5, 2].reduce(Number['+'], 0);// 4.5
Number['=='](total, 4.5);// true

Index

Bound

readonlymaxValue

maxValue: number

Maximum value for this type

@example
const anyNumber: number
anyNumber < Number.maxValue // true

readonlyminValue

minValue: number

Minimum value for this type

@example
const anyNumber: number
anyNumber > Number.minValue // true

Codec

codecDecode

  • Returns the decoded input, Result.Ok or Result.Error()

    @example
    interface SomeObject {
    foo: string
    }
    const someCodec: Codec<SomeObject> = ...;
    const input: unknown = ...;
    const decoded = Codec.decode(someCodec, input);

codecEncode

  • codecEncode(this: void, input: number): unknown
  • Returns the encoded input

    @example
    interface SomeObject {
    foo: string
    }
    const someCodec: Codec<SomeObject> = ...;
    const someObject: SomeObject = { foo: "bar" }
    const encoded = Codec.decode(someCodec, someObject);

codecSchema

  • Returns the JSONSchema corresponding to the decoded type

    @example
    const someCodec: Codec<unknown> = ...;
    const jsonSchema = Codec.schema(someCodec);

Comparator

!=

  • !=(this: void, left: number, right: number): boolean
  • "Not equal to" operator

    @example
    const TEqual: Equal<T>;
    TEqual['!='](value, otherValue); // true
    TEqual['!='](value, value); // false

  • (this: void, left: number, right: number): boolean
  • "Less than" operator

    @example
    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare['<'](smallerT, smallerT); // false
    TCompare['<'](smallerT, greaterT); // true
    TCompare['<'](greaterT, smallerT); // false

<=

  • <=(this: void, left: number, right: number): boolean
  • "Less than or equal to" operator

    @example
    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare['<='](smallerT, smallerT); // true
    TCompare['<='](smallerT, greaterT); // true
    TCompare['<='](greaterT, smallerT); // false

==

  • ==(this: void, left: number, right: number): boolean
  • "Equal to" operator

    @example
    type T = // ...
    const TEqual: Equal<T>;
    TEqual['=='](value, value); // true
    TEqual['=='](value, otherValue); // false

  • (this: void, left: number, right: number): boolean
  • "Greater than" operator

    @example
    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare['>'](smallerT, smallerT); // false
    TCompare['>'](smallerT, greaterT); // false
    TCompare['>'](greaterT, smallerT); // true

>=

  • >=(this: void, left: number, right: number): boolean
  • "Greater than or equal to" operator

    @example
    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare['>='](smallerT, smallerT); // true
    TCompare['>='](smallerT, greaterT); // false
    TCompare['>='](greaterT, smallerT); // true

compare

  • compare(this: void, left: number, right: number): number
  • Return a number that represents comparison

    @example
    type T;
    const TCompare: Comparable<T>;
    const sorted = [3, 1, 1].sort(TCompare.compare);

equals

  • equals(this: void, left: number, right: number): boolean
  • Alias to '=='

    @example
    type T = // ...
    const TEqual: Equal<T>;
    TEqual.equals(value, value); // true
    TEqual.equals(value, otherValue); // false

max

  • max(this: void, left: number, right: number): number
  • "maximum" operator

    @example
    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare.max(smallerT, greaterT); // greaterT

min

  • min(this: void, left: number, right: number): number
  • "minimum" operator

    @example
    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare.min(smallerT, greaterT); // smallerT

Numeric

*

  • *(left: number, right: number): number
  • Multiplication operator

    @example
    type T = ...;
    const TNumeric: Numeric<T> = ...;
    const result = Numeric['*'](left, right);// represents (left * right)

+

  • +(left: number, right: number): number
  • Addition operator

    @example
    type T = ...;
    const TNumeric: Numeric<T> = ...;
    const result = TNumeric['+'](left, right);// represents (left + right)

-

  • -(left: number, right: number): number
  • Subtraction operator

    @example
    type T = ...;
    const TNumeric: Numeric<T> = ...;
    const result = Numeric['-'](left, right);// represents (left - right)

abs

  • abs(value: number): number
  • Absolute value. It should satisfy Numeric['*'](Numeric.abs(x), Numeric.sign(x)) == x

    @example
    type T = ...;
    const TNumeric: Numeric<T> = ...;
    const result = Numeric.abs(value);// absolute value of (value)

sign

  • sign(value: number): number
  • Sign of a number. It should satisfy Numeric['*'](Numeric.abs(x), Numeric.sign(x)) == x

Type

hasInstance

  • hasInstance(anyValue: unknown): anyValue is number
  • Return true if the given value is an instance of the class.

    @example
    const StringType: Type<string>;
    StringType.hasInstance('foo'); // true
    StringType.hasInstance(42); // false

Other

typeName

Re-exports typeName

format

  • format(numberValue: number, radix?: Radix36): string
  • Return string representation of number

    @example
    Number.format(1.1);// '1.1'

parse

  • parse(expression: string): Option<number>
  • Parses a string argument and returns an number If the expression is not valid, it returns Option.None

    @example
    Number.parse('1');// Option.Some(1)
    Number.parse('invalid');// Option.None