Skip to content

TypeSafe generic builder pattern to enforce required props and only allow declaring a specific property once

License

Notifications You must be signed in to change notification settings

HitchPin/rots-builder

Repository files navigation

Doubloons

Statements Branches Functions Lines
Statements Branches Functions Lines

How to use?

npm install doubloons

Import types:

import { Doubloon } from 'doubloons';
import { USD } from 'doubloons/currencies';

Create a Doubloon with a string or a Decimal (from Decimal.js) with the intended currency:

const x = new Doubloon<USD>(USD, '10.00');
const y = new Doubloon<USD>(USD, new Decimal('10.50'));
console.log(x.add(y));

Adding and subtracting between Doubloons is only supported with two Doubloons of the same currency:

const x = new Doubloon<USD>(USD, '10.00');
const y = new Doubloon<USD>(USD, '5.05');
console.log(x.add(y));
console.log(x.subtract(y));

Multiplying and dividing between Doubloons is forbidden, you can only multiply or divide by a scalar int (number with no decimals) or an instance of Decimal from Decimal.js.

const x = new Doubloon<USD>(USD, '10.00');
const y = new Doubloon<USD>(USD, '5.05');
console.log(x.mul(y)); // not allowed
console.log(x.mul(5)); // OK
console.log(x.mul(5.15)); // Not OK
console.log(x.mul(new Decimal(5.15)); // OK

Similarly, boolean comparisons require two Doubloons of the same kind.

To get a value out of a Doubloon, you can use .str() to get a string. This will be a properly-quantized decimal number for the given currency, without the currency symbol.

API Docs

About

TypeSafe generic builder pattern to enforce required props and only allow declaring a specific property once

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published