refactor: currency-formatter -> Intl.NumberFormat

This commit is contained in:
Trevor Farlow 2022-06-16 22:44:56 -06:00 committed by James Long
parent 2c9ecee464
commit 3273b2723d
6 changed files with 29 additions and 35 deletions

View file

@ -43,7 +43,6 @@
"babel-jest": "25.2.6",
"babel-loader": "^8.0.6",
"buffer": "^5.5.0",
"currency-formatter": "jlongster/currency-formatter",
"damerau-levenshtein": "^1.0.4",
"date-fns": "2.0.0-alpha.27",
"esm": "^3.0.82",

View file

@ -1,5 +1,3 @@
let currencyFormatter = require('currency-formatter');
export function cleanUUID(uuid) {
return uuid.replace(/-/g, '');
}
@ -267,7 +265,7 @@ export function setNumberFormat(format) {
switch (format) {
case 'space-comma':
locale = 'za-ZA';
locale = 'en-ZA';
regex = /[^-0-9,]/g;
separator = ',';
break;
@ -286,12 +284,10 @@ export function setNumberFormat(format) {
numberFormat = {
value: format,
separator,
// This is the keep in line with the Intl API which we might
// switch to when it's available on all mobile platforms
formatter: {
format: number =>
currencyFormatter.format(number, { locale, format: '%v' })
},
formatter: new Intl.NumberFormat(locale, {
minimumFractionDigits: 2,
maximumFractionDigits: 2
}),
regex
};
}
@ -300,7 +296,7 @@ export function getNumberFormat() {
return numberFormat;
}
setNumberFormat('1,000.33');
setNumberFormat('comma-dot');
export function toRelaxedNumber(value) {
return integerToAmount(currencyToInteger(value) || 0);
@ -319,10 +315,6 @@ export function amountToCurrency(n) {
return numberFormat.formatter.format(n);
}
export function amountToPrettyCurrency(n, code) {
return currencyFormatter.format(n, { code });
}
export function currencyToAmount(str) {
let amount = parseFloat(
str.replace(numberFormat.regex, '').replace(numberFormat.separator, '.')

View file

@ -1,4 +1,4 @@
import { looselyParseAmount } from './util';
import { looselyParseAmount, getNumberFormat, setNumberFormat } from './util';
describe('utility functions', () => {
test('looseParseAmount works with basic numbers', () => {
@ -28,4 +28,26 @@ describe('utility functions', () => {
// thought through more.
expect(looselyParseAmount('3_45_23.10')).toBe(34523.1);
});
test('number formatting works with comma-dot format', () => {
setNumberFormat('comma-dot');
const formatter = getNumberFormat().formatter;
expect(formatter.format('1234.56')).toBe('1,234.56');
});
test('number formatting works with dot-comma format', () => {
setNumberFormat('dot-comma');
const formatter = getNumberFormat().formatter;
expect(formatter.format('1234.56')).toBe('1.234,56');
});
test('number formatting works with space-comma format', () => {
setNumberFormat('space-comma');
const formatter = getNumberFormat().formatter;
// grouping separator space char is a non-breaking space, or UTF-16 \xa0
expect(formatter.format('1234.56')).toBe('1\xa0234,56');
});
});

View file

@ -1,7 +1,6 @@
import React, { useState } from 'react';
import { styles, colors } from '../../style';
import { View, Text, Modal, P, Button } from '../common';
import { amountToPrettyCurrency } from 'loot-core/src/shared/util';
import {
fromPlaidAccountType,
prettyAccountType

View file

@ -25,7 +25,6 @@
"@react-navigation/native-stack": "^6.1.0",
"@reactions/component": "^2.0.2",
"@sentry/react-native": "3.0.2",
"currency-formatter": "jlongster/currency-formatter",
"jsc-android": "^241213.1.0",
"loot-core": "*",
"memoize-one": "^4.0.0",

View file

@ -2979,10 +2979,6 @@ accepts@^1.3.7, accepts@~1.3.4, accepts@~1.3.5, accepts@~1.3.7, accepts@~1.3.8:
mime-types "~2.1.34"
negotiator "0.6.3"
accounting@jlongster/accounting.js:
version "0.4.2"
resolved "https://codeload.github.com/jlongster/accounting.js/tar.gz/cfccd812a0eb9f38853f81e0a00ac5a170afc75a"
acorn-dynamic-import@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/acorn-dynamic-import/-/acorn-dynamic-import-3.0.0.tgz#901ceee4c7faaef7e07ad2a47e890675da50a278"
@ -5428,14 +5424,6 @@ csv-stringify@^5.3.6:
resolved "https://registry.yarnpkg.com/csv-stringify/-/csv-stringify-5.6.5.tgz#c6d74badda4b49a79bf4e72f91cce1e33b94de00"
integrity sha512-PjiQ659aQ+fUTQqSrd1XEDnOr52jh30RBurfzkscaE2tPaFsDH5wOAHJiw8XAHphRknCwMUE9KRayc4K/NbO8A==
currency-formatter@jlongster/currency-formatter:
version "1.5.5"
resolved "https://codeload.github.com/jlongster/currency-formatter/tar.gz/64ded5bde37e7c1add2636b5c5e3de5722300fe0"
dependencies:
accounting jlongster/accounting.js
locale-currency "0.0.2"
object-assign "^4.1.1"
cyclist@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/cyclist/-/cyclist-1.0.1.tgz#596e9698fd0c80e12038c2b82d6eb1b35b6224d9"
@ -10519,11 +10507,6 @@ loader-utils@^2.0.0:
emojis-list "^3.0.0"
json5 "^2.1.2"
locale-currency@0.0.2:
version "0.0.2"
resolved "https://registry.yarnpkg.com/locale-currency/-/locale-currency-0.0.2.tgz#e2c90607563ce47a59f9559e45a70e24e4db4b6d"
integrity sha1-4skGB1Y85HpZ+VWeRacOJOTbS20=
localforage@^1.8.1:
version "1.10.0"
resolved "https://registry.yarnpkg.com/localforage/-/localforage-1.10.0.tgz#5c465dc5f62b2807c3a84c0c6a1b1b3212781dd4"