@ledgerhq/currencies
Advanced tools
Comparing version 4.8.0-rc1.1b178f67 to 4.8.0
@@ -27,7 +27,19 @@ "use strict"; | ||
locale: "en-EN", | ||
// show the currency code | ||
showCode: false, | ||
// always show the sign, even if it's a positive value | ||
alwaysShowSign: false, | ||
// override showAllDigits of the unit | ||
showAllDigits: false, | ||
disableRounding: false | ||
// disable the feature that only show significant digits | ||
// and removes the negligible extra digits. | ||
// (rounding is dynamically applied based on the value. higher value means more rounding) | ||
disableRounding: false, | ||
// enable or not the thousands grouping (e.g; 1,234.00) | ||
useGrouping: true, | ||
// this allow to increase the number of digits displayed | ||
// even if the currency don't allow more than this (sub-cent) | ||
// a value of 1 can display USD 0.006 for instance. 2 can display USD 0.0065 | ||
// NB even if you set 3, USD 4.50 will be display as USD 4.50 , not 4.5000 (extra zeros are not displayed) | ||
subMagnitude: 0 | ||
}; | ||
@@ -70,3 +82,5 @@ | ||
locale = _defaultFormatOptions.locale, | ||
disableRounding = _defaultFormatOptions.disableRounding; | ||
disableRounding = _defaultFormatOptions.disableRounding, | ||
useGrouping = _defaultFormatOptions.useGrouping, | ||
subMagnitude = _defaultFormatOptions.subMagnitude; | ||
@@ -79,5 +93,5 @@ var magnitude = unit.magnitude, | ||
var minimumFractionDigits = showAllDigits ? magnitude : 0; | ||
var maximumFractionDigits = disableRounding ? magnitude : Math.max(minimumFractionDigits, Math.max(0, | ||
var maximumFractionDigits = disableRounding ? magnitude + subMagnitude : Math.max(minimumFractionDigits, Math.max(0, | ||
// dynamic max number of digits based on the value itself. to only show significant part | ||
Math.min(4 - Math.round((0, _log2.default)(floatValueAbs)), magnitude))); | ||
Math.min(4 - Math.round((0, _log2.default)(floatValueAbs)), magnitude + subMagnitude))); | ||
@@ -89,3 +103,4 @@ var fragValueByKind = { | ||
maximumFractionDigits: maximumFractionDigits, | ||
minimumFractionDigits: minimumFractionDigits | ||
minimumFractionDigits: minimumFractionDigits, | ||
useGrouping: useGrouping | ||
}), | ||
@@ -92,0 +107,0 @@ separator: nonBreakableSpace |
@@ -6,3 +6,3 @@ "use strict"; | ||
}); | ||
exports.decodeURIScheme = exports.encodeURIScheme = exports.countervalueForRate = exports.hasCurrencyByCoinType = exports.getDefaultUnitByCoinType = exports.getCurrencyByCoinType = exports.formatShort = exports.formatCurrencyUnitFragment = exports.formatCurrencyUnit = exports.parseCurrencyUnit = exports.hasFiatUnit = exports.getFiatUnit = exports.listCurrencies = undefined; | ||
exports.decodeURIScheme = exports.encodeURIScheme = exports.countervalueForRate = exports.hasCurrencyByCoinType = exports.getDefaultUnitByCoinType = exports.getCurrencyByCoinType = exports.formatShort = exports.formatCurrencyUnitFragment = exports.formatCurrencyUnit = exports.chopCurrencyUnitDecimals = exports.parseCurrencyUnit = exports.hasFiatUnit = exports.getFiatUnit = exports.listCurrencies = undefined; | ||
@@ -17,2 +17,4 @@ var _CurrencyURIScheme = require("./CurrencyURIScheme"); | ||
var _chopCurrencyUnitDecimals = require("./chopCurrencyUnitDecimals"); | ||
var _formatCurrencyUnit = require("./formatCurrencyUnit"); | ||
@@ -32,2 +34,3 @@ | ||
exports.parseCurrencyUnit = _parseCurrencyUnit.parseCurrencyUnit; | ||
exports.chopCurrencyUnitDecimals = _chopCurrencyUnitDecimals.chopCurrencyUnitDecimals; | ||
exports.formatCurrencyUnit = _formatCurrencyUnit.formatCurrencyUnit; | ||
@@ -34,0 +37,0 @@ exports.formatCurrencyUnitFragment = _formatCurrencyUnit.formatCurrencyUnitFragment; |
@@ -6,13 +6,51 @@ "use strict"; | ||
}); | ||
exports.parseCurrencyUnit = undefined; | ||
exports.parseCurrencyUnit = exports.getSeparators = undefined; | ||
var _numeral = require("numeral"); | ||
var _extends2 = require("babel-runtime/helpers/extends"); | ||
var _numeral2 = _interopRequireDefault(_numeral); | ||
var _extends3 = _interopRequireDefault(_extends2); | ||
var _memoize = require("lodash/memoize"); | ||
var _memoize2 = _interopRequireDefault(_memoize); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var parseCurrencyUnit = exports.parseCurrencyUnit = function parseCurrencyUnit(unit, valueString) { | ||
return Math.round((0, _numeral2.default)(valueString).value() * Math.pow(10, unit.magnitude)); | ||
var defaultFormatOptions = { | ||
locale: "en-EN" | ||
}; | ||
// returns decimal and thousands separator | ||
var getSeparators = exports.getSeparators = (0, _memoize2.default)(function (locale) { | ||
var res = 10000.2.toLocaleString(locale); | ||
var decimal = void 0, | ||
thousands = void 0; | ||
for (var i = 0; i < res.length; i++) { | ||
var c = res[i]; | ||
if (/[0-9]/.test(c)) continue; | ||
if (!thousands) { | ||
thousands = c; | ||
} else { | ||
decimal = c; | ||
} | ||
} | ||
return { decimal: decimal, thousands: thousands }; | ||
}); | ||
var parseCurrencyUnit = exports.parseCurrencyUnit = function parseCurrencyUnit(unit, valueString, options) { | ||
var _defaultFormatOptions = (0, _extends3.default)({}, defaultFormatOptions, options), | ||
locale = _defaultFormatOptions.locale; | ||
var sep = getSeparators(locale); | ||
var str = ""; | ||
for (var i = 0; i < valueString.length; i++) { | ||
var c = valueString[i]; | ||
if (c !== sep.thousands) { | ||
str += c === sep.decimal ? "." : c; | ||
} | ||
} | ||
var value = parseFloat(str); | ||
if (isNaN(value)) return 0; | ||
return Math.round(value * Math.pow(10, unit.magnitude)); | ||
}; | ||
//# sourceMappingURL=parseCurrencyUnit.js.map |
{ | ||
"name": "@ledgerhq/currencies", | ||
"version": "4.8.0-rc1.1b178f67", | ||
"version": "4.8.0", | ||
"description": "Ledger Hardware Wallet currencies dataset", | ||
@@ -5,0 +5,0 @@ "keywords": [], |
@@ -8,7 +8,19 @@ //@flow | ||
locale: "en-EN", | ||
// show the currency code | ||
showCode: false, | ||
// always show the sign, even if it's a positive value | ||
alwaysShowSign: false, | ||
// override showAllDigits of the unit | ||
showAllDigits: false, | ||
disableRounding: false | ||
// disable the feature that only show significant digits | ||
// and removes the negligible extra digits. | ||
// (rounding is dynamically applied based on the value. higher value means more rounding) | ||
disableRounding: false, | ||
// enable or not the thousands grouping (e.g; 1,234.00) | ||
useGrouping: true, | ||
// this allow to increase the number of digits displayed | ||
// even if the currency don't allow more than this (sub-cent) | ||
// a value of 1 can display USD 0.006 for instance. 2 can display USD 0.0065 | ||
// NB even if you set 3, USD 4.50 will be display as USD 4.50 , not 4.5000 (extra zeros are not displayed) | ||
subMagnitude: 0 | ||
}; | ||
@@ -56,3 +68,11 @@ | ||
): FormatFragment[] { | ||
const { showCode, alwaysShowSign, showAllDigits, locale, disableRounding } = { | ||
const { | ||
showCode, | ||
alwaysShowSign, | ||
showAllDigits, | ||
locale, | ||
disableRounding, | ||
useGrouping, | ||
subMagnitude | ||
} = { | ||
...defaultFormatOptions, | ||
@@ -67,3 +87,3 @@ ...unit, | ||
const maximumFractionDigits = disableRounding | ||
? magnitude | ||
? magnitude + subMagnitude | ||
: Math.max( | ||
@@ -74,3 +94,6 @@ minimumFractionDigits, | ||
// dynamic max number of digits based on the value itself. to only show significant part | ||
Math.min(4 - Math.round(Math.log10(floatValueAbs)), magnitude) | ||
Math.min( | ||
4 - Math.round(Math.log10(floatValueAbs)), | ||
magnitude + subMagnitude | ||
) | ||
) | ||
@@ -85,3 +108,4 @@ ); | ||
maximumFractionDigits, | ||
minimumFractionDigits | ||
minimumFractionDigits, | ||
useGrouping | ||
}), | ||
@@ -88,0 +112,0 @@ separator: nonBreakableSpace |
@@ -16,2 +16,4 @@ //@flow | ||
import { chopCurrencyUnitDecimals } from "./chopCurrencyUnitDecimals"; | ||
import { | ||
@@ -31,2 +33,3 @@ formatCurrencyUnit, | ||
parseCurrencyUnit, | ||
chopCurrencyUnitDecimals, | ||
formatCurrencyUnit, | ||
@@ -33,0 +36,0 @@ formatCurrencyUnitFragment, |
//@flow | ||
import type { Unit } from "./types"; | ||
import numeral from "numeral"; | ||
import memoize from "lodash/memoize"; | ||
export const parseCurrencyUnit = (unit: Unit, valueString: string): number => | ||
Math.round(numeral(valueString).value() * 10 ** unit.magnitude); | ||
const defaultFormatOptions = { | ||
locale: "en-EN" | ||
}; | ||
// returns decimal and thousands separator | ||
export const getSeparators = memoize((locale: string): { | ||
decimal: ?string, | ||
thousands: ?string | ||
} => { | ||
const res = (10000.2).toLocaleString(locale); | ||
let decimal, thousands; | ||
for (let i = 0; i < res.length; i++) { | ||
const c = res[i]; | ||
if (/[0-9]/.test(c)) continue; | ||
if (!thousands) { | ||
thousands = c; | ||
} else { | ||
decimal = c; | ||
} | ||
} | ||
return { decimal, thousands }; | ||
}); | ||
export const parseCurrencyUnit = ( | ||
unit: Unit, | ||
valueString: string, | ||
options?: $Shape<typeof defaultFormatOptions> | ||
): number => { | ||
const { locale } = { | ||
...defaultFormatOptions, | ||
...options | ||
}; | ||
const sep = getSeparators(locale); | ||
let str = ""; | ||
for (let i = 0; i < valueString.length; i++) { | ||
let c = valueString[i]; | ||
if (c !== sep.thousands) { | ||
str += c === sep.decimal ? "." : c; | ||
} | ||
} | ||
const value = parseFloat(str); | ||
if (isNaN(value)) return 0; | ||
return Math.round(value * 10 ** unit.magnitude); | ||
}; |
@@ -13,2 +13,3 @@ //@flow | ||
parseCurrencyUnit, | ||
chopCurrencyUnitDecimals, | ||
formatShort, | ||
@@ -119,2 +120,57 @@ decodeURIScheme, | ||
test("sub magnitude", () => { | ||
expect( | ||
formatCurrencyUnit(getFiatUnit("USD"), 0.04, { | ||
subMagnitude: 2 | ||
}) | ||
).toBe("0.0004"); | ||
// digits will be round after subMagnitude | ||
expect( | ||
formatCurrencyUnit(getFiatUnit("USD"), 0.03987654, { | ||
subMagnitude: 2, | ||
showCode: true | ||
}) | ||
).toBe("USD 0.0004"); | ||
expect( | ||
formatCurrencyUnit(getFiatUnit("USD"), 0.03987654, { | ||
subMagnitude: 2, | ||
disableRounding: true | ||
}) | ||
).toBe("0.0004"); | ||
expect( | ||
formatCurrencyUnit(getFiatUnit("USD"), 0.03987654, { | ||
subMagnitude: 5, | ||
disableRounding: true | ||
}) | ||
).toBe("0.0003988"); | ||
// even tho the USD unit showAllDigits, it does not force the sub magnitude digits to show | ||
expect( | ||
formatCurrencyUnit(getFiatUnit("USD"), 0.03, { | ||
subMagnitude: 5, | ||
disableRounding: true | ||
}) | ||
).toBe("0.0003"); | ||
expect( | ||
formatCurrencyUnit(getCurrencyByCoinType(0).units[0], 9.123456, { | ||
subMagnitude: 2 | ||
}) | ||
).toBe("0.0000000912"); | ||
expect( | ||
formatCurrencyUnit(getCurrencyByCoinType(0).units[0], 999999999999.123456, { | ||
disableRounding: true, | ||
subMagnitude: 2 | ||
}) | ||
).toBe("9,999.9999999912"); | ||
expect( | ||
formatCurrencyUnit(getCurrencyByCoinType(0).units[0], 999999999999.123456, { | ||
subMagnitude: 2 | ||
}) | ||
).toBe("10,000"); | ||
}); | ||
test("parseCurrencyUnit", () => { | ||
@@ -124,2 +180,13 @@ expect( | ||
).toBe(999999999999); | ||
expect(parseCurrencyUnit(getCurrencyByCoinType(0).units[0], ".987654")).toBe( | ||
98765400 | ||
); | ||
expect(parseCurrencyUnit(getCurrencyByCoinType(0).units[0], "9,999")).toBe( | ||
999900000000 | ||
); | ||
expect(parseCurrencyUnit(getCurrencyByCoinType(0).units[0], "1")).toBe( | ||
100000000 | ||
); | ||
expect(parseCurrencyUnit(getCurrencyByCoinType(0).units[0], "0x1")).toBe(0); | ||
expect(parseCurrencyUnit(getCurrencyByCoinType(0).units[0], "NOPE")).toBe(0); | ||
}); | ||
@@ -135,2 +202,11 @@ | ||
test("formatter useGrouping", () => { | ||
expect( | ||
formatCurrencyUnit(getFiatUnit("EUR"), 1234500, { useGrouping: true }) | ||
).toBe("12,345.00"); | ||
expect( | ||
formatCurrencyUnit(getFiatUnit("EUR"), 1234500, { useGrouping: false }) | ||
).toBe("12345.00"); | ||
}); | ||
test("formatter can change locale", () => { | ||
@@ -156,2 +232,16 @@ expect( | ||
test("chopCurrencyUnitDecimals", () => { | ||
expect(chopCurrencyUnitDecimals(getFiatUnit("EUR"), "1")).toBe("1"); | ||
expect(chopCurrencyUnitDecimals(getFiatUnit("EUR"), "1,234")).toBe("1,234"); | ||
expect(chopCurrencyUnitDecimals(getFiatUnit("EUR"), "1,234.56")).toBe( | ||
"1,234.56" | ||
); | ||
expect(chopCurrencyUnitDecimals(getFiatUnit("EUR"), "1,234.5678")).toBe( | ||
"1,234.56" | ||
); | ||
expect(chopCurrencyUnitDecimals(getFiatUnit("EUR"), "1,234.5678 EUR")).toBe( | ||
"1,234.56 EUR" | ||
); | ||
}); | ||
test("encodeURIScheme", () => { | ||
@@ -158,0 +248,0 @@ expect( |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
599510
272
10278
0