UNPKG

@epicallan/money-ts

Version:

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

107 lines 3.56 kB
"use strict"; var __assign = (this && this.__assign) || function () { __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; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.trunc = exports.ceil = exports.round = exports.floor = exports.fromDiscrete = exports.getScale = exports.getOrd = exports.getEq = exports.getOne = exports.getZero = exports.fromInteger = exports.Dense = void 0; var Discrete_1 = require("./Discrete"); var Scale_1 = require("./Scale"); var R = require("./Rational"); var I = require("./Integer"); var Dense = /** @class */ (function () { function Dense(dimension, value) { this.dimension = dimension; this.value = value; } Dense.prototype.isZero = function () { return R.isZero(this.value); }; Dense.prototype.add = function (y) { return new Dense(this.dimension, R.add(this.value, y.value)); }; Dense.prototype.mul = function (y) { return new Dense(this.dimension, R.mul(this.value, y)); }; Dense.prototype.negate = function () { return new Dense(this.dimension, R.negate(this.value)); }; Dense.prototype.sub = function (y) { return new Dense(this.dimension, R.sub(this.value, y.value)); }; Dense.prototype.div = function (y) { return new Dense(this.dimension, R.div(this.value, y)); }; Dense.prototype.inspect = function () { return this.toString(); }; Dense.prototype.toString = function () { return this.dimension + " " + R.show(this.value); }; return Dense; }()); exports.Dense = Dense; function fromInteger(d, i) { return new Dense(d, R.fromInteger(i)); } exports.fromInteger = fromInteger; function getZero(d) { return new Dense(d, R.zero); } exports.getZero = getZero; function getOne(d) { return new Dense(d, R.one); } exports.getOne = getOne; function getEq() { return { equals: function (x, y) { return R.Eq.equals(x.value, y.value); } }; } exports.getEq = getEq; function getOrd() { return __assign(__assign({}, getEq()), { compare: function (x, y) { return R.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, [I.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(R.mul(input, scale)); var scaledResult = [I.mul(result, scale[1]), scale[0]]; return [new Discrete_1.Discrete(format, result), new Dense(d.dimension, R.sub(input, scaledResult))]; } function floor(unit, d) { return roundf(R.floor, unit, d); } exports.floor = floor; function round(unit, d) { return roundf(R.round, unit, d); } exports.round = round; function ceil(unit, d) { return roundf(R.ceil, unit, d); } exports.ceil = ceil; function trunc(unit, d) { return roundf(R.trunc, unit, d); } exports.trunc = trunc; //# sourceMappingURL=Dense.js.map