From 3273b2723d115aae324d00da37b48ce7f6dadc25 Mon Sep 17 00:00:00 2001 From: Trevor Farlow Date: Thu, 16 Jun 2022 22:44:56 -0600 Subject: [PATCH] refactor: currency-formatter -> Intl.NumberFormat --- packages/loot-core/package.json | 1 - packages/loot-core/src/shared/util.js | 20 +++++----------- packages/loot-core/src/shared/util.test.js | 24 ++++++++++++++++++- .../components/modals/SelectLinkedAccounts.js | 1 - packages/mobile/package.json | 1 - yarn.lock | 17 ------------- 6 files changed, 29 insertions(+), 35 deletions(-) diff --git a/packages/loot-core/package.json b/packages/loot-core/package.json index 5b170a2..41d5c5b 100644 --- a/packages/loot-core/package.json +++ b/packages/loot-core/package.json @@ -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", diff --git a/packages/loot-core/src/shared/util.js b/packages/loot-core/src/shared/util.js index 7479491..4ef11de 100644 --- a/packages/loot-core/src/shared/util.js +++ b/packages/loot-core/src/shared/util.js @@ -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, '.') diff --git a/packages/loot-core/src/shared/util.test.js b/packages/loot-core/src/shared/util.test.js index 3d6a4b2..a5a2df9 100644 --- a/packages/loot-core/src/shared/util.test.js +++ b/packages/loot-core/src/shared/util.test.js @@ -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'); + }); }); diff --git a/packages/loot-design/src/components/modals/SelectLinkedAccounts.js b/packages/loot-design/src/components/modals/SelectLinkedAccounts.js index fc339d2..862f3bf 100644 --- a/packages/loot-design/src/components/modals/SelectLinkedAccounts.js +++ b/packages/loot-design/src/components/modals/SelectLinkedAccounts.js @@ -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 diff --git a/packages/mobile/package.json b/packages/mobile/package.json index 113a593..80e75ed 100644 --- a/packages/mobile/package.json +++ b/packages/mobile/package.json @@ -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", diff --git a/yarn.lock b/yarn.lock index b0591da..4c51c31 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"