UNPKG

@aeternity/aepp-sdk

Version:
152 lines (132 loc) 6.46 kB
import _defineProperty from "@babel/runtime-corejs3/helpers/defineProperty"; var _DENOMINATION_MAGNITU; import _Object$values from "@babel/runtime-corejs3/core-js-stable/object/values"; import _includesInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/includes"; import _reduceInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/reduce"; import _findInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/find"; import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat"; /* * ISC License (ISC) * Copyright (c) 2018 aeternity developers * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. */ /** * Amount Formatter * @module @aeternity/aepp-sdk/es/utils/amount-formatter * @example import { AmountFormatter } from '@aeternity/aepp-sdk' */ import BigNumber from 'bignumber.js'; import { isBigNumber } from './bignumber'; /** * AE amount formats */ export var AE_AMOUNT_FORMATS = { AE: 'ae', MILI_AE: 'miliAE', MICRO_AE: 'microAE', NANO_AE: 'nanoAE', PICO_AE: 'picoAE', FEMTO_AE: 'femtoAE', AETTOS: 'aettos' }; /** * DENOMINATION_MAGNITUDE */ export var DENOMINATION_MAGNITUDE = (_DENOMINATION_MAGNITU = {}, _defineProperty(_DENOMINATION_MAGNITU, AE_AMOUNT_FORMATS.AE, 0), _defineProperty(_DENOMINATION_MAGNITU, AE_AMOUNT_FORMATS.MILI_AE, -3), _defineProperty(_DENOMINATION_MAGNITU, AE_AMOUNT_FORMATS.MICRO_AE, -6), _defineProperty(_DENOMINATION_MAGNITU, AE_AMOUNT_FORMATS.NANO_AE, -9), _defineProperty(_DENOMINATION_MAGNITU, AE_AMOUNT_FORMATS.PICO_AE, -12), _defineProperty(_DENOMINATION_MAGNITU, AE_AMOUNT_FORMATS.FEMTO_AE, -15), _defineProperty(_DENOMINATION_MAGNITU, AE_AMOUNT_FORMATS.AETTOS, -18), _DENOMINATION_MAGNITU); /** * Convert amount to AE * @param {String|Number|BigNumber} value amount to convert * @param {Object} [options={}] options * @param {String} [options.denomination='aettos'] denomination of amount, can be ['ae', 'aettos'] * @return {String} */ export var toAe = function toAe(value) { var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, _ref$denomination = _ref.denomination, denomination = _ref$denomination === void 0 ? AE_AMOUNT_FORMATS.AETTOS : _ref$denomination; return formatAmount(value, { denomination: denomination, targetDenomination: AE_AMOUNT_FORMATS.AE }); }; /** * Convert amount to aettos * @param {String|Number|BigNumber} value amount to convert * @param {Object} [options={}] options * @param {String} [options.denomination='ae'] denomination of amount, can be ['ae', 'aettos'] * @return {String} */ export var toAettos = function toAettos(value) { var _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, _ref2$denomination = _ref2.denomination, denomination = _ref2$denomination === void 0 ? AE_AMOUNT_FORMATS.AE : _ref2$denomination; return formatAmount(value, { denomination: denomination }); }; /** * Convert amount from one to other denomination * @param {String|Number|BigNumber} value amount to convert * @param {Object} [options={}] options * @param {String} [options.denomination='aettos'] denomination of amount, can be ['ae', 'aettos'] * @param {String} [options.targetDenomination='aettos'] target denomination, can be ['ae', 'aettos'] * @return {String} */ export var formatAmount = function formatAmount(value) { var _ref3 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, _ref3$denomination = _ref3.denomination, denomination = _ref3$denomination === void 0 ? AE_AMOUNT_FORMATS.AETTOS : _ref3$denomination, _ref3$targetDenominat = _ref3.targetDenomination, targetDenomination = _ref3$targetDenominat === void 0 ? AE_AMOUNT_FORMATS.AETTOS : _ref3$targetDenominat; var denominations = _Object$values(AE_AMOUNT_FORMATS); if (!_includesInstanceProperty(denominations).call(denominations, denomination)) throw new Error("Invalid denomination: ".concat(denomination)); if (!_includesInstanceProperty(denominations).call(denominations, targetDenomination)) throw new Error("Invalid target denomination: ".concat(targetDenomination)); if (!isBigNumber(value)) throw new Error("Value ".concat(value.toString(), " is not type of number")); return new BigNumber(value).shiftedBy(DENOMINATION_MAGNITUDE[denomination] - DENOMINATION_MAGNITUDE[targetDenomination]).toFixed(); }; var prefixes = [{ name: 'exa', magnitude: 18 }, { name: 'giga', magnitude: 9 }, { name: '', magnitude: 0 }, { name: 'pico', magnitude: -12 }]; var getNearestPrefix = function getNearestPrefix(exponent) { return _reduceInstanceProperty(prefixes).call(prefixes, function (p, n) { return Math.abs(n.magnitude - exponent) < Math.abs(p.magnitude - exponent) ? n : p; }); }; var getLowerBoundPrefix = function getLowerBoundPrefix(exponent) { var _prefixes$find; return (_prefixes$find = _findInstanceProperty(prefixes).call(prefixes, function (p) { return p.magnitude <= exponent; })) !== null && _prefixes$find !== void 0 ? _prefixes$find : prefixes[prefixes.length - 1]; }; export default (function (rawValue) { var _value$e, _context, _context2; var value = new BigNumber(rawValue); var exp = (_value$e = value.e) !== null && _value$e !== void 0 ? _value$e : 0; var _ref4 = (exp < 0 ? getNearestPrefix : getLowerBoundPrefix)(exp), name = _ref4.name, magnitude = _ref4.magnitude; var v = value.shiftedBy(-magnitude).precision(9 + Math.min(exp - magnitude, 0)).toFixed(); return _concatInstanceProperty(_context = _concatInstanceProperty(_context2 = "".concat(v)).call(_context2, name !== '' ? ' ' : '')).call(_context, name); }); //# sourceMappingURL=amount-formatter.js.map