UNPKG

money-ts

Version:

TypeScript library for type-safe and lossless encoding and manipulation of world currencies and precious metals

103 lines 3.39 kB
"use strict"; var __assign = (this && this.__assign) || Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); var Discrete_1 = require("./Discrete"); var Scale_1 = require("./Scale"); var rational = require("./Rational"); var integer = require("./Integer"); var Dense = /** @class */ (function () { function Dense(dimension, value) { this.dimension = dimension; this.value = value; } Dense.prototype.isZero = function () { return rational.isZero(this.value); }; Dense.prototype.add = function (y) { return new Dense(this.dimension, rational.add(this.value, y.value)); }; Dense.prototype.mul = function (y) { return new Dense(this.dimension, rational.mul(this.value, y)); }; Dense.prototype.negate = function () { return new Dense(this.dimension, rational.negate(this.value)); }; Dense.prototype.sub = function (y) { return new Dense(this.dimension, rational.sub(this.value, y.value)); }; Dense.prototype.div = function (y) { return new Dense(this.dimension, rational.div(this.value, y)); }; Dense.prototype.inspect = function () { return this.toString(); }; Dense.prototype.toString = function () { return this.dimension + " " + rational.show(this.value); }; return Dense; }()); exports.Dense = Dense; function fromInteger(d, i) { return new Dense(d, rational.fromInteger(i)); } exports.fromInteger = fromInteger; function getZero(d) { return new Dense(d, rational.zero); } exports.getZero = getZero; function getOne(d) { return new Dense(d, rational.one); } exports.getOne = getOne; function getSetoid() { return { equals: function (x, y) { return rational.setoid.equals(x.value, y.value); } }; } exports.getSetoid = getSetoid; function getOrd() { return __assign({}, getSetoid(), { compare: function (x, y) { return rational.ord.compare(x.value, y.value); } }); } exports.getOrd = getOrd; function getScale(format) { return Scale_1.scale[format.dimension][format.unit]; } exports.getScale = getScale; function fromDiscrete(d) { var _a = getScale(d.format), ns = _a[0], ds = _a[1]; return new Dense(d.format.dimension, [integer.mul(d.value, ds), ns]); } exports.fromDiscrete = fromDiscrete; /** Internal. Used to implement `round`, `ceil`, `floor` and `trunc` */ function roundf(f, unit, d) { var format = { dimension: d.dimension, unit: unit }; var input = d.value; var scale = getScale(format); var result = f(rational.mul(input, scale)); var scaledResult = [integer.mul(result, scale[1]), scale[0]]; return [new Discrete_1.Discrete(format, result), new Dense(d.dimension, rational.sub(input, scaledResult))]; } function floor(unit, d) { return roundf(rational.floor, unit, d); } exports.floor = floor; function round(unit, d) { return roundf(rational.round, unit, d); } exports.round = round; function ceil(unit, d) { return roundf(rational.ceil, unit, d); } exports.ceil = ceil; function trunc(unit, d) { return roundf(rational.trunc, unit, d); } exports.trunc = trunc; //# sourceMappingURL=Dense.js.map