@exodus/currency
Advanced tools
Comparing version 1.1.2 to 1.1.3
@@ -0,1 +1,6 @@ | ||
## 1.1.3 / 2021-09-28 | ||
- `toString`, `toNumberString`, and `toNumber` now accept an optional unit argument (unitInstance for toString, unit was already taken) | ||
- deprecate direct `toNumberString` usage | ||
## 1.1.2 / 2021-06-28 | ||
@@ -2,0 +7,0 @@ |
@@ -22,6 +22,6 @@ "use strict"; | ||
const reportDifferentTypesDeprecated = (num, otherNum) => { | ||
const deprecationWarning = message => { | ||
try { | ||
console.warn('***************************************************************'); | ||
console.warn(`DEPRECATION WARNING: number-unit.add/sub on different NumberUnit types (${num}, ${otherNum}) is not correct and will be removed soon`); | ||
console.warn(message); | ||
console.warn('***************************************************************'); | ||
@@ -32,2 +32,6 @@ console.trace(); | ||
const reportDifferentTypesDeprecated = (num, otherNum) => { | ||
deprecationWarning(`DEPRECATION WARNING: number-unit.add/sub on different NumberUnit types (${num}, ${otherNum}) is not correct and will be removed soon`); | ||
}; | ||
class NumberUnit { | ||
@@ -57,5 +61,5 @@ static create(num, unit) { | ||
this._number = this._createNumber(num); | ||
Object.defineProperty(this, '_numberString', { | ||
Object.defineProperty(this, '_numberStringMap', { | ||
enumerable: false, | ||
value: null, | ||
value: new Map(), | ||
writable: true | ||
@@ -260,3 +264,4 @@ }); | ||
} else { | ||
const num = this.toNumberString(); | ||
const num = this._toNumberString(); | ||
const [whole] = num.split('.'); | ||
@@ -271,3 +276,4 @@ return new NumberUnit(whole, this.unit); | ||
} else { | ||
const num = this.toNumberString(); | ||
const num = this._toNumberString(); | ||
const [whole, fraction] = num.split('.'); | ||
@@ -288,3 +294,4 @@ let newNumber = new NumberUnit(whole, this.unit); | ||
} else { | ||
const num = this.toNumberString(); | ||
const num = this._toNumberString(); | ||
const [whole, fraction] = num.split('.'); | ||
@@ -374,3 +381,3 @@ let newNumber = new NumberUnit(whole, this.unit); | ||
return { | ||
value: this.toNumberString(), | ||
value: this._toNumberString(), | ||
unit: this.unitName, | ||
@@ -382,4 +389,4 @@ unitType: this.unitType.toString(), | ||
toNumber() { | ||
return parseFloat(this.toNumberString()); | ||
toNumber(unit) { | ||
return parseFloat(this._toNumberString(unit)); | ||
} | ||
@@ -395,8 +402,18 @@ | ||
toNumberString() { | ||
if (this._numberString) return this._numberString; | ||
toNumberString(unit) { | ||
deprecationWarning('DEPRECATION WARNING: number-unit.toNumberString usage will be removed soon. Use number-unit.toString({unit: false}) instead'); | ||
return this._toNumberString(unit); | ||
} | ||
let number = this._number.toString(10); | ||
_toNumberString(unit = this.unit) { | ||
const unitString = unit.toString(); | ||
if (this._numberStringMap.get(unitString)) return this._numberStringMap.get(unitString); | ||
let numberUnit; | ||
if (unit === this.unit) numberUnit = this;else numberUnit = this.to(unit); | ||
if (this.unit.power === 0) { | ||
let number = numberUnit._number.toString(10); | ||
if (numberUnit.unit.power === 0) { | ||
this._numberStringMap.set(unitString, number); | ||
return number; | ||
@@ -411,7 +428,7 @@ } | ||
if (number.length < this.unit.power + 1) { | ||
number = number.padStart(this.unit.power + 1, '0'); | ||
if (number.length < numberUnit.unit.power + 1) { | ||
number = number.padStart(numberUnit.unit.power + 1, '0'); | ||
} | ||
number = number.slice(0, -this.unit.power) + '.' + number.slice(-this.unit.power); | ||
number = number.slice(0, -numberUnit.unit.power) + '.' + number.slice(-numberUnit.unit.power); | ||
const pp = number.indexOf('.'); | ||
@@ -434,3 +451,4 @@ | ||
this._numberString = number; | ||
this._numberStringMap.set(unitString, number); | ||
return number; | ||
@@ -441,8 +459,9 @@ } | ||
unit = true, | ||
format | ||
format, | ||
unitInstance = this.unit | ||
} = {}) { | ||
if (!format) { | ||
return this.toNumberString() + (unit ? ' ' + this.unitName : ''); | ||
return this._toNumberString(unitInstance) + (unit ? ' ' + unitInstance.unitName : ''); | ||
} else { | ||
return format(this.toNumberString(), this.unit); | ||
return format(this._toNumberString(unitInstance), unitInstance); | ||
} | ||
@@ -473,9 +492,3 @@ } | ||
if (NumberUnit.isNumberUnit(number)) return number;else { | ||
try { | ||
console.warn('***************************************************************'); | ||
console.warn('DEPRECATION WARNING: number-unit.add/sub usage with int or string param will be removed soon'); | ||
console.warn('***************************************************************'); | ||
console.trace(); | ||
} catch (err) {} | ||
deprecationWarning('DEPRECATION WARNING: number-unit.add/sub usage with int or string param will be removed soon'); | ||
return new NumberUnit(number, this.unit); | ||
@@ -482,0 +495,0 @@ } |
{ | ||
"name": "@exodus/currency", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "Currency support.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22263
539
1