Aller au contenu principal

Money

Index

Codec

__decode__

  • __decode__(this: void, input: unknown, context: Context<Money>): Result<Money, CodecError>
  • 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);

__encode__

  • __encode__(this: void, input: Money): 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);

__schema__

  • __schema__(this: void): JSONValue
  • Returns the JSONSchema corresponding to the decoded type

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

Comparator

!=

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

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

  • (this: void, left: Money, right: Money): 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: Money, right: Money): 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: Money, right: Money): boolean
  • "Equal to" operator

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

  • (this: void, left: Money, right: Money): 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: Money, right: Money): 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

clamp

  • clamp(this: void, value: Money, minValue: Money, maxValue: Money): Money
  • Clamp value between minValue and maxValue

    @example
    type T;
    const TCompare: Comparable<T>;
    TCompare.clamp(value, min, max); // min if value < min, max if value > max, otherwise value itself

compare

  • compare(this: void, left: Money, right: Money): Ordering
  • Return an Ordering that represents comparison result

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

equals

  • equals(this: void, left: Money, right: Money): 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: Money, right: Money): Money
  • "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: Money, right: Money): Money
  • "minimum" operator

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

Constructor

create

  • create(this: void, properties: Parameters<Money>): Money
  • Construct a new model

Formatting

asString

  • asString(this: void, self: Money): string
  • Converts the given value to a String.

    @example
    Number.asString(123); // '123'

Numeric

*

  • *(money: Money, multiplier: BigDecimal): Money
  • Multiply operator

    @example
    Money['*'](EUR(2), BigDecimal('2'));// EUR("4")

+

  • +(this: void, left: Money, right: Money): Result<Money, ArgumentError>
  • Addition operator

    @example
    Money['+'](EUR(1), EUR(2));// Result.Ok(EUR(1))
    Money['+'](EUR(1), USD(2));// Result.Error(new ArgumentError({ message: 'Incompatible currencies EUR and USD' }))

-

  • -(this: void, left: Money, right: Money): Result<Money, ArgumentError>
  • Subtract operator

    @example
    Money['-'](EUR(2), EUR(1));// Result.Ok(EUR(1))
    Money['-'](EUR(1), USD(2));// Result.Error(new ArgumentError({ message: 'Incompatible currencies EUR and USD' }))

isNegative

  • isNegative(this: void, self: Money): boolean
  • Returns true if the number is negative and false if the number is zero or positive.

isPositive

  • isPositive(this: void, self: Money): boolean
  • Returns true if the number is positive and false if the number is zero or negative.

isZero

  • isZero(this: void, self: Money): boolean
  • Returns true if self is equal to the additive identity.

    @example
    Money.isZero(EUR('0')); // true
    Money.isZero(EUR('1')); // false

negate

  • negate(self: Money): Money
  • Negates the given value.

    @example
    Number.negate(5); // -5
    Number.negate(Number.negate(5)); // 5

Type

__inspect__

__inspect__: Option<(anyValue: Money, depth: number, options: InspectOptions, inspect: InspectFunction) => string>

When defined, returns a custom string representation. To be useful, it should be bound to a prototype (ex: Struct)

@example
import { inspect } from 'node:util';

interface Foo {
foo: boolean;
}
const Foo = Struct.define<Foo>({
typeName: 'Foo',
__inspect__: (self) => `Foo { ${String(self.foo)} }`,
});
const myStruct = Struct.create(Foo, { foo: true });// 'Foo { true }'
inspect(myStruct);// 'Foo { true }'
@param

readonlytypeName

typeName: Money

The factory type constant

asInstance

  • asInstance(anyValue: unknown): Option<Money>
  • Try to convert anyValue to enum value or else returns Option.None

    @example
    const StringType: Type<string>;
    StringType.asInstance('foo'); // Option.Some('foo')
    StringType.asInstance(12); // Option.None

hasInstance

  • hasInstance(anyValue: unknown): anyValue is Money
  • 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

__call__

  • __call__(properties: Parameters<Money>): Money

format

  • format(self: Money): string
  • Returns a formatted representation of money

    @example
    Money.format(EUR('1.10'));// '1.10EUR';

normalize

  • normalize(self: Money): Money
  • Normalizes a BigDecimal object to its simplest form. This means that the decimal part is reduced to its lowest terms.

    @example
    normalize(USD('1.020'));// USD('1.02')
    normalize(USD('1.02'));// USD('1.02')

parse

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

    @example
    Money.parse('1.1EUR');// Option.Some(EUR('1.1'))
    Money.parse('invalid');// Option.None