@exodus/currency
Advanced tools
Comparing version 2.3.2 to 2.3.3
@@ -27,3 +27,4 @@ "use strict"; | ||
return newUnit.to(unit2); | ||
} else if (ut.equals(ut2)) { | ||
} | ||
if (ut.equals(ut2)) { | ||
let newUnit = ut1.baseUnit(someNumberUnit._number).mul(ut1[unit1].multiplier); | ||
@@ -38,4 +39,5 @@ try { | ||
return newUnit.to(unit1); | ||
} else throw new Error('Conversion error'); | ||
} | ||
throw new Error('Conversion error'); | ||
}; | ||
} |
@@ -18,8 +18,10 @@ "use strict"; | ||
return ut2[num2.unitName](newNumber).to(num2.unitName); | ||
} else if (ut.equals(ut2)) { | ||
} | ||
if (ut.equals(ut2)) { | ||
const normalizeNum = someNumberUnit.to(num2.unitName); | ||
const newNumber = num2.isZero ? num2.unitType.ZERO._number : normalizeNum._number.mul(num1._number).div(num2._number); | ||
return ut1[num1.unitName](newNumber).to(num1.unitName); | ||
} else throw new Error('Conversion error'); | ||
} | ||
throw new Error('Conversion error'); | ||
}; | ||
} |
@@ -57,3 +57,5 @@ declare module '@exodus/currency' { | ||
} | ||
// the implementation allows calling toNumber/toNumberString without a param, but we want to deprecate that usage | ||
toNumber: (unit: ToNumberUnitType) => number | ||
toNumberString: (unit: ToNumberUnitType) => string | ||
toDefaultNumber: () => number | ||
@@ -63,3 +65,2 @@ toBaseNumber: () => number | ||
toBaseBufferBE: () => Buffer | ||
toNumberString: (unit: ToNumberUnitType) => string | ||
toString: ({ | ||
@@ -74,4 +75,4 @@ unit, | ||
}) => string | ||
toDefaultString: ({ unit }: { unit?: boolean }) => string | ||
toBaseString: ({ unit }: { unit?: boolean }) => string | ||
toDefaultString: (options?: { unit?: boolean }) => string | ||
toBaseString: (options?: { unit?: boolean }) => string | ||
valueOf: () => number | ||
@@ -104,3 +105,3 @@ } | ||
toJSON: () => Definitions | ||
ZERO: () => NumberUnit | ||
ZERO: NumberUnit | ||
@@ -107,0 +108,0 @@ units: UnitMap |
@@ -139,3 +139,4 @@ "use strict"; | ||
(0, _deprecationWarning.maybeReportDifferentTypesDeprecated)(this, num, 'add'); | ||
if (num.isZero) return this;else if (this.isZero) return new NumberUnit(num._number, this.unit); | ||
if (num.isZero) return this; | ||
if (this.isZero) return new NumberUnit(num._number, this.unit); | ||
return new NumberUnit(this._number.add(num._number), this.unit); | ||
@@ -145,3 +146,4 @@ } | ||
const zero = new NumberUnit(0, this.unit); | ||
if (this.gte(zero)) return this;else return zero; | ||
if (this.gte(zero)) return this; | ||
return zero; | ||
} | ||
@@ -210,7 +212,6 @@ clone() { | ||
return this; | ||
} else { | ||
const num = this._toNumberString(); | ||
const [whole] = num.split('.'); | ||
return new NumberUnit(whole, this.unit); | ||
} | ||
const num = this._toNumberString(); | ||
const [whole] = num.split('.'); | ||
return new NumberUnit(whole, this.unit); | ||
} | ||
@@ -220,11 +221,10 @@ round() { | ||
return this; | ||
} else { | ||
const num = this._toNumberString(); | ||
const [whole, fraction] = num.split('.'); | ||
let newNumber = new NumberUnit(whole, this.unit); | ||
if (fraction && Math.round(Number.parseFloat(`0.${fraction}`)) > 0) { | ||
newNumber = newNumber.add(new NumberUnit(1, this.unit)); | ||
} | ||
return newNumber; | ||
} | ||
const num = this._toNumberString(); | ||
const [whole, fraction] = num.split('.'); | ||
let newNumber = new NumberUnit(whole, this.unit); | ||
if (fraction && Math.round(Number.parseFloat(`0.${fraction}`)) > 0) { | ||
newNumber = newNumber.add(new NumberUnit(1, this.unit)); | ||
} | ||
return newNumber; | ||
} | ||
@@ -234,11 +234,10 @@ ceil() { | ||
return this; | ||
} else { | ||
const num = this._toNumberString(); | ||
const [whole, fraction] = num.split('.'); | ||
let newNumber = new NumberUnit(whole, this.unit); | ||
if (fraction && Math.ceil(Number.parseFloat(`0.${fraction}`)) > 0) { | ||
newNumber = newNumber.add(new NumberUnit(1, this.unit)); | ||
} | ||
return newNumber; | ||
} | ||
const num = this._toNumberString(); | ||
const [whole, fraction] = num.split('.'); | ||
let newNumber = new NumberUnit(whole, this.unit); | ||
if (fraction && Math.ceil(Number.parseFloat(`0.${fraction}`)) > 0) { | ||
newNumber = newNumber.add(new NumberUnit(1, this.unit)); | ||
} | ||
return newNumber; | ||
} | ||
@@ -267,3 +266,4 @@ toFixed(x, rm) { | ||
(0, _deprecationWarning.maybeReportDifferentTypesDeprecated)(this, num, 'sub'); | ||
if (num.isZero) return this;else if (this.isZero) return new NumberUnit(num._number.neg(), this.unit); | ||
if (num.isZero) return this; | ||
if (this.isZero) return new NumberUnit(num._number.neg(), this.unit); | ||
return new NumberUnit(this._number.sub(num._number), this.unit); | ||
@@ -304,18 +304,16 @@ } | ||
return new NumberUnit(this._number, unit); | ||
} else { | ||
if (!conversionUnit) throw new Error('Incompatible unit types. Must specify a conversion.'); | ||
if (typeof conversionUnit === 'function') { | ||
return conversionUnit(this); | ||
} else { | ||
if (!this.unitType.equals(conversionUnit.from.unitType)) { | ||
throw new Error('Conversion unit from is of different type.'); | ||
} | ||
const rate = Number.parseFloat(conversionUnit.toValue) / Number.parseFloat(conversionUnit.fromValue); | ||
const convert = (0, _conversionByRate.default)(conversionUnit.from.unitType, conversionUnit.to.unitType, rate, { | ||
unit1: conversionUnit.fromUnit, | ||
unit2: conversionUnit.toUnit | ||
}); | ||
return convert(this); | ||
} | ||
} | ||
if (!conversionUnit) throw new Error('Incompatible unit types. Must specify a conversion.'); | ||
if (typeof conversionUnit === 'function') { | ||
return conversionUnit(this); | ||
} | ||
if (!this.unitType.equals(conversionUnit.from.unitType)) { | ||
throw new Error('Conversion unit from is of different type.'); | ||
} | ||
const rate = Number.parseFloat(conversionUnit.toValue) / Number.parseFloat(conversionUnit.fromValue); | ||
const convert = (0, _conversionByRate.default)(conversionUnit.from.unitType, conversionUnit.to.unitType, rate, { | ||
unit1: conversionUnit.fromUnit, | ||
unit2: conversionUnit.toUnit | ||
}); | ||
return convert(this); | ||
} | ||
@@ -354,3 +352,4 @@ toJSON() { | ||
return this._numberStringMap.get(unitString); | ||
} else if (!this._numberStringMap) { | ||
} | ||
if (!this._numberStringMap) { | ||
(0, _deprecationWarning.default)(noInstanceWarning); | ||
@@ -430,6 +429,5 @@ } | ||
_coerceToNumberUnit(number) { | ||
if (NumberUnit.isNumberUnit(number)) return number;else { | ||
(0, _deprecationWarning.default)('DEPRECATION WARNING: number-unit.add/sub usage with int or string param will be removed soon'); | ||
return new NumberUnit(number, this.unit); | ||
} | ||
if (NumberUnit.isNumberUnit(number)) return number; | ||
(0, _deprecationWarning.default)('DEPRECATION WARNING: number-unit.add/sub usage with int or string param will be removed soon'); | ||
return new NumberUnit(number, this.unit); | ||
} | ||
@@ -436,0 +434,0 @@ } |
@@ -7,4 +7,6 @@ "use strict"; | ||
exports.default = void 0; | ||
var _memoize2 = _interopRequireDefault(require("lodash/memoize")); | ||
var _mapValues2 = _interopRequireDefault(require("lodash/mapValues")); | ||
var _isEmpty2 = _interopRequireDefault(require("lodash/isEmpty")); | ||
var _minimalisticAssert = _interopRequireDefault(require("minimalistic-assert")); | ||
var _lodash = require("lodash"); | ||
var _unit = require("./unit"); | ||
@@ -68,3 +70,3 @@ var _isUnitType = _interopRequireDefault(require("./is-unit-type")); | ||
toJSON() { | ||
return (0, _lodash.mapValues)(this.units, u => u.power); | ||
return (0, _mapValues2.default)(this.units, u => u.power); | ||
} | ||
@@ -76,5 +78,5 @@ get ZERO() { | ||
exports.default = UnitType; | ||
_defineProperty(UnitType, "create", (0, _lodash.memoize)(definitions => { | ||
(0, _minimalisticAssert.default)(!(0, _lodash.isEmpty)(definitions), 'definitions must have at least one item'); | ||
_defineProperty(UnitType, "create", (0, _memoize2.default)(definitions => { | ||
(0, _minimalisticAssert.default)(!(0, _isEmpty2.default)(definitions), 'definitions must have at least one item'); | ||
return new UnitType(normalizeDefinitions(definitions), FACTORY_SYMBOL); | ||
}, definitions => JSON.stringify(normalizeDefinitions(definitions)))); |
@@ -19,5 +19,4 @@ "use strict"; | ||
return val.to(numberUnitCreator); | ||
} else { | ||
return _numberUnit.default.create(val, numberUnitCreator); | ||
} | ||
return _numberUnit.default.create(val, numberUnitCreator); | ||
}; | ||
@@ -24,0 +23,0 @@ numberUnitCreator.unitName = name; |
{ | ||
"name": "@exodus/currency", | ||
"version": "2.3.2", | ||
"version": "2.3.3", | ||
"description": "Currency support.", | ||
@@ -13,9 +13,6 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"bn.js": "^4.11.0", | ||
"bn.js": "^5.2.1", | ||
"lodash": "^4.17.11", | ||
"minimalistic-assert": "^1.0.1" | ||
}, | ||
"devDependencies": { | ||
"jest": "^29.3.1" | ||
}, | ||
"scripts": { | ||
@@ -25,4 +22,4 @@ "build": "yarn build:clean && babel src --out-dir lib --config-file ../../babel.config.js && yarn build:flow && cp src/*.d.ts lib/", | ||
"build:clean": "rm -rf lib", | ||
"test": "jest", | ||
"lint": "eslint . --ignore-path ../../.gitignore", | ||
"test": "run -T jest", | ||
"lint": "run -T eslint . --ignore-path ../../.gitignore", | ||
"lint:fix": "yarn lint --fix" | ||
@@ -37,3 +34,3 @@ }, | ||
}, | ||
"gitHead": "4b17b7fb0cd62618d63e8dac4fe5cb26d2824278" | ||
"gitHead": "6dd83359a8a5fb374ab7d443edfafb26f7759eb3" | ||
} |
51005
0
828
+ Addedbn.js@5.2.1(transitive)
- Removedbn.js@4.12.1(transitive)
Updatedbn.js@^5.2.1