Skip to content

Commit

Permalink
Merge pull request #21 from windingtree/fix-currency-symbols
Browse files Browse the repository at this point in the history
fix: 🐛 incorrect currency symbols
  • Loading branch information
kostysh authored Nov 14, 2022
2 parents 35999c4 + 2d8abb9 commit a132972
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 20 deletions.
33 changes: 18 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"prettier": "npx prettier -w deploy src test README.md .eslintrc hardhat.config.ts",
"clean": "rm -rf dist",
"build": "yarn clean && yarn tsc -p tsconfig-build.json",
"test": "TESTING=true hardhat test"
"test": "TESTING=true hardhat test",
"test:mocha": "mocha"
},
"repository": {
"type": "git",
Expand All @@ -31,30 +32,32 @@
},
"homepage": "https://github.com/windingtree/win-commons#readme",
"devDependencies": {
"@types/chai": "^4.3.3",
"@types/mocha": "^9.1.1",
"@types/chai-as-promised": "^7.1.4",
"@commitlint/cli": "^17.0.3",
"@commitlint/config-conventional": "^17.0.3",
"@semantic-release/commit-analyzer": "^9.0.2",
"@semantic-release/release-notes-generator": "^10.0.3",
"@nomiclabs/hardhat-ethers": "npm:hardhat-deploy-ethers@^0.3.0-beta.13",
"@semantic-release/changelog": "^6.0.1",
"@semantic-release/npm": "^9.0.1",
"@semantic-release/commit-analyzer": "^9.0.2",
"@semantic-release/github": "^8.0.5",
"semantic-release": "^19.0.3",
"prettier": "^2.7.1",
"lint-staged": "^13.0.3",
"husky": "^8.0.1",
"eslint": "^8.22.0",
"@semantic-release/npm": "^9.0.1",
"@semantic-release/release-notes-generator": "^10.0.3",
"@types/chai": "^4.3.3",
"@types/chai-as-promised": "^7.1.4",
"@types/mocha": "^9.1.1",
"@typescript-eslint/eslint-plugin": "^5.33.0",
"@typescript-eslint/parser": "^5.33.0",
"git-cz": "^4.9.0",
"ts-node": "^10.9.1",
"chai": "^4.3.6",
"chai-as-promised": "^7.1.1",
"eslint": "^8.22.0",
"git-cz": "^4.9.0",
"hardhat": "^2.10.1",
"@nomiclabs/hardhat-ethers": "npm:hardhat-deploy-ethers@^0.3.0-beta.13",
"hardhat-deploy": "^0.11.12",
"husky": "^8.0.1",
"lint-staged": "^13.0.3",
"mocha": "^10.1.0",
"prettier": "^2.7.1",
"semantic-release": "^19.0.3",
"ts-mocha": "^10.0.0",
"ts-node": "^10.9.1",
"typescript": "^4.7.4"
},
"dependencies": {
Expand Down
21 changes: 19 additions & 2 deletions src/currencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,26 @@ export const currencySymbolMap: Record<string, string> = Object.entries(currency
if (['GBP', 'USD', 'EUR'].includes(key)) {
// special case for GBP, USD, EUR
result[key] = symbol;
// } else if (dups[symbol] > 1) {
// result[key] = key.slice(0, 2) + symbol;
} else {
// using the 3-letter code as symbol
result[key] = key;
}

return result;
},
{}
);

export const prefixedCurrencySymbolMap: Record<string, string> = Object.entries(currencySymbols).reduce(
(result, [key, symbol]) => {
if (['GBP', 'USD', 'EUR'].includes(key)) {
// special case for GBP, USD, EUR
result[key] = symbol;
} else if (dups[symbol] > 1) {
// return prefixed symbol for duplicates
result[key] = key.slice(0, 2) + symbol;
} else {
// return unique symbols
result[key] = symbol;
}

Expand Down
45 changes: 45 additions & 0 deletions test/currencies.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { expect } from 'chai';
import { currencySymbolMap, prefixedCurrencySymbolMap } from '../src/currencies';

describe('Currencies', () => {
describe('currencySymbolMap', () => {
it('EUR, USD, GBP should return proper symbols', () => {
expect(currencySymbolMap['USD']).eq('$');
expect(currencySymbolMap['EUR']).eq(String.fromCharCode(0x20ac));
expect(currencySymbolMap['GBP']).eq(String.fromCharCode(0xa3));
});

it('AUD, JPY and other currencies should return 3-letter codes', () => {
expect(currencySymbolMap['AUD']).eq('AUD');
expect(currencySymbolMap['JPY']).eq('JPY');
});

it('Invalid currencies should return undefined', () => {
expect(currencySymbolMap['XXX']).to.be.undefined;
expect(currencySymbolMap['213']).to.be.undefined;
});
});

describe('prefixedCurrencySymbolMap', () => {
it('EUR, USD, GBP should return proper symbols', () => {
expect(prefixedCurrencySymbolMap['USD']).eq('$');
expect(prefixedCurrencySymbolMap['EUR']).eq('€');
expect(prefixedCurrencySymbolMap['GBP']).eq('£');
});

it('should return unique symbols for GHS, CRC', () => {
expect(prefixedCurrencySymbolMap['GHS']).eq('GH₵');
expect(prefixedCurrencySymbolMap['CRC']).eq('₡');
});

it('AUD, JPY and other duplicate currency symbols should return prefixed symbols', () => {
expect(prefixedCurrencySymbolMap['AUD']).eq('AU$');
expect(prefixedCurrencySymbolMap['JPY']).eq('JP¥');
});

it('Invalid currencies should return undefined', () => {
expect(prefixedCurrencySymbolMap['XXX']).to.be.undefined;
expect(prefixedCurrencySymbolMap['213']).to.be.undefined;
});
});
});
100 changes: 97 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,11 @@
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.11.tgz#d421b6c527a3037f7c84433fd2c4229e016863d3"
integrity sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ==

"@types/json5@^0.0.29":
version "0.0.29"
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
integrity sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==

"@types/level-errors@*":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@types/level-errors/-/level-errors-3.0.0.tgz#15c1f4915a5ef763b51651b15e90f6dc081b96a8"
Expand Down Expand Up @@ -1617,7 +1622,7 @@ array-union@^2.1.0:
resolved "https://registry.yarnpkg.com/array-union/-/array-union-2.1.0.tgz#b798420adbeb1de828d84acd8a2e23d3efe85e8d"
integrity sha512-HGyxoOTYUyCM6stUe6EJgnd4EoewAI7zMdfqO+kGjnlZmBDz/cR5pf8r/cR4Wq60sL/p0IkcjUEEPwS3GFrIyw==

arrify@^1.0.1:
arrify@^1.0.0, arrify@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/arrify/-/arrify-1.0.1.tgz#898508da2226f380df904728456849c1501a4b0d"
integrity sha512-3CYzex9M9FGQjCGMGyi6/31c8GJbgb0qGyrx5HWxPd0aCwh4cB2YjMb2Xf9UuoogrMrlO9cTqnB5rI5GHZTcUA==
Expand Down Expand Up @@ -1792,7 +1797,7 @@ bs58check@^2.1.2:
create-hash "^1.1.0"
safe-buffer "^5.1.2"

buffer-from@^1.0.0:
buffer-from@^1.0.0, buffer-from@^1.1.0:
version "1.1.2"
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.2.tgz#2b146a6fd72e80b4f55d255f35ed59a3a9a41bd5"
integrity sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==
Expand Down Expand Up @@ -2395,6 +2400,11 @@ diff@5.0.0:
resolved "https://registry.yarnpkg.com/diff/-/diff-5.0.0.tgz#7ed6ad76d859d030787ec35855f5b1daf31d852b"
integrity sha512-/VTCrvm5Z0JGty/BWHljh+BAiw3IK+2j87NGMu8Nwc/f48WoDAC395uomO9ZD117ZOBaHmkX1oyLvkVM/aIT3w==

diff@^3.1.0:
version "3.5.0"
resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12"
integrity sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA==

diff@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
Expand Down Expand Up @@ -3744,6 +3754,13 @@ json-stringify-safe@^5.0.1:
resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"
integrity sha512-ZClg6AaYvamvYEE82d3Iyd3vSSIjQ+odgjaTzRuO3s7toCdFKczob2i0zCh7JE8kWn17yvAWhUVxvqGwUalsRA==

json5@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/json5/-/json5-1.0.1.tgz#779fb0018604fa854eacbf6252180d83543e3dbe"
integrity sha512-aKS4WQjPenRxiQsC93MNfjx+nbF4PAdYzmd/1JIj8HYzqfbu86beTuNgXDzPknWk0n0uARlyewZo4s++ES36Ow==
dependencies:
minimist "^1.2.0"

jsonfile@^2.1.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-2.4.0.tgz#3736a2b428b87bbda0cc83b53fa3d633a35c2ae8"
Expand Down Expand Up @@ -4386,6 +4403,11 @@ minimist@^1.2.0, minimist@^1.2.5:
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.6.tgz#8637a5b759ea0d6e98702cfb3a9283323c93af44"
integrity sha512-Jsjnk4bw3YJqYzbdyBiNsPWHPfO++UGG749Cxs6peCu5Xg4nrena6OVxOYxrQTqww0Jmwt+Ref8rggumkTLz9Q==

minimist@^1.2.6:
version "1.2.7"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.7.tgz#daa1c4d91f507390437c6a8bc01078e7000c4d18"
integrity sha512-bzfL1YUZsP41gmu/qjrEk0Q6i2ix/cVeAhbCbqH9u3zYutS1cLg00qhrD0M2MVdCcx4Sc0UpP2eBWo9rotpq6g==

minipass-collect@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/minipass-collect/-/minipass-collect-1.0.2.tgz#22b813bf745dc6edba2576b940022ad6edc8c617"
Expand Down Expand Up @@ -4457,6 +4479,13 @@ mkdirp-infer-owner@^2.0.0:
infer-owner "^1.0.4"
mkdirp "^1.0.3"

mkdirp@^0.5.1:
version "0.5.6"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.6.tgz#7def03d2432dcae4ba1d611445c48396062255f6"
integrity sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==
dependencies:
minimist "^1.2.6"

mkdirp@^1.0.3, mkdirp@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
Expand Down Expand Up @@ -4497,6 +4526,33 @@ mocha@^10.0.0:
yargs-parser "20.2.4"
yargs-unparser "2.0.0"

mocha@^10.1.0:
version "10.1.0"
resolved "https://registry.yarnpkg.com/mocha/-/mocha-10.1.0.tgz#dbf1114b7c3f9d0ca5de3133906aea3dfc89ef7a"
integrity sha512-vUF7IYxEoN7XhQpFLxQAEMtE4W91acW4B6En9l97MwE9stL1A9gusXfoHZCLVHDUJ/7V5+lbCM6yMqzo5vNymg==
dependencies:
ansi-colors "4.1.1"
browser-stdout "1.3.1"
chokidar "3.5.3"
debug "4.3.4"
diff "5.0.0"
escape-string-regexp "4.0.0"
find-up "5.0.0"
glob "7.2.0"
he "1.2.0"
js-yaml "4.1.0"
log-symbols "4.1.0"
minimatch "5.0.1"
ms "2.1.3"
nanoid "3.3.3"
serialize-javascript "6.0.0"
strip-json-comments "3.1.1"
supports-color "8.1.1"
workerpool "6.2.1"
yargs "16.2.0"
yargs-parser "20.2.4"
yargs-unparser "2.0.0"

modify-values@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022"
Expand Down Expand Up @@ -5696,7 +5752,7 @@ solc@0.7.3:
semver "^5.5.0"
tmp "0.0.33"

source-map-support@^0.5.13:
source-map-support@^0.5.13, source-map-support@^0.5.6:
version "0.5.21"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.21.tgz#04fe7c7f9e1ed2d662233c28cb2b35b9f63f6e4f"
integrity sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==
Expand Down Expand Up @@ -6019,6 +6075,29 @@ trim-newlines@^3.0.0:
resolved "https://registry.yarnpkg.com/true-case-path/-/true-case-path-2.2.1.tgz#c5bf04a5bbec3fd118be4084461b3a27c4d796bf"
integrity sha512-0z3j8R7MCjy10kc/g+qg7Ln3alJTodw9aDuVWZa3uiWqfuBMKeAeP2ocWcxoyM3D73yz3Jt/Pu4qPr4wHSdB/Q==

ts-mocha@^10.0.0:
version "10.0.0"
resolved "https://registry.yarnpkg.com/ts-mocha/-/ts-mocha-10.0.0.tgz#41a8d099ac90dbbc64b06976c5025ffaebc53cb9"
integrity sha512-VRfgDO+iiuJFlNB18tzOfypJ21xn2xbuZyDvJvqpTbWgkAgD17ONGr8t+Tl8rcBtOBdjXp5e/Rk+d39f7XBHRw==
dependencies:
ts-node "7.0.1"
optionalDependencies:
tsconfig-paths "^3.5.0"

ts-node@7.0.1:
version "7.0.1"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-7.0.1.tgz#9562dc2d1e6d248d24bc55f773e3f614337d9baf"
integrity sha512-BVwVbPJRspzNh2yfslyT1PSbl5uIk03EZlb493RKHN4qej/D06n1cEhjlOJG69oFsE7OT8XjpTUcYf6pKTLMhw==
dependencies:
arrify "^1.0.0"
buffer-from "^1.1.0"
diff "^3.1.0"
make-error "^1.1.1"
minimist "^1.2.0"
mkdirp "^0.5.1"
source-map-support "^0.5.6"
yn "^2.0.0"

ts-node@^10.8.1, ts-node@^10.9.1:
version "10.9.1"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-10.9.1.tgz#e73de9102958af9e1f0b168a6ff320e25adcff4b"
Expand All @@ -6038,6 +6117,16 @@ ts-node@^10.8.1, ts-node@^10.9.1:
v8-compile-cache-lib "^3.0.1"
yn "3.1.1"

tsconfig-paths@^3.5.0:
version "3.14.1"
resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-3.14.1.tgz#ba0734599e8ea36c862798e920bcf163277b137a"
integrity sha512-fxDhWnFSLt3VuTwtvJt5fpwxBHg5AdKWMsgcPOOIilyjymcYVZoCQF8fvFRezCNfblEXmi+PcM1eYHeOAgXCOQ==
dependencies:
"@types/json5" "^0.0.29"
json5 "^1.0.1"
minimist "^1.2.6"
strip-bom "^3.0.0"

tslib@^1.8.1, tslib@^1.9.3:
version "1.14.1"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
Expand Down Expand Up @@ -6406,6 +6495,11 @@ yn@3.1.1:
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==

yn@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/yn/-/yn-2.0.0.tgz#e5adabc8acf408f6385fc76495684c88e6af689a"
integrity sha512-uTv8J/wiWTgUTg+9vLTi//leUl5vDQS6uii/emeTb2ssY7vl6QWf2fFbIIGjnhjvbdKlU0ed7QPgY1htTC86jQ==

yocto-queue@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"
Expand Down

0 comments on commit a132972

Please sign in to comment.