From e5fae2e9978bb07cecccc2bdc88fc10081c6e20a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20Maria=20Pay=C3=A1=20Castillo?= Date: Thu, 12 Aug 2021 10:00:19 +0200 Subject: [PATCH 1/3] feat(isISO4217): add currency code validator (#1703) --- README.md | 1 + src/index.js | 2 ++ src/lib/isISO4217.js | 38 ++++++++++++++++++++++++++++++++++++++ test/validators.js | 26 ++++++++++++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 src/lib/isISO4217.js diff --git a/README.md b/README.md index 25af675ed..fc12f70ea 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,7 @@ Validator | Description **isISO8601(str)** | check if the string is a valid [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date.
`options` is an object which defaults to `{ strict: false, strictSeparator: false }`. If `strict` is true, date strings with invalid dates like `2009-02-29` will be invalid. If `strictSeparator` is true, date strings with date and time separated by anything other than a T will be invalid. **isISO31661Alpha2(str)** | check if the string is a valid [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) officially assigned country code. **isISO31661Alpha3(str)** | check if the string is a valid [ISO 3166-1 alpha-3](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3) officially assigned country code. +**isISO4217(str)** | check if the string is a valid [ISO 4217](https://en.wikipedia.org/wiki/ISO_4217) officially assigned currency code. **isISRC(str)** | check if the string is a [ISRC](https://en.wikipedia.org/wiki/International_Standard_Recording_Code). **isISSN(str [, options])** | check if the string is an [ISSN](https://en.wikipedia.org/wiki/International_Standard_Serial_Number).

`options` is an object which defaults to `{ case_sensitive: false, require_hyphen: false }`. If `case_sensitive` is true, ISSNs with a lowercase `'x'` as the check digit are rejected. **isJSON(str [, options])** | check if the string is valid JSON (note: uses JSON.parse).

`options` is an object which defaults to `{ allow_primitives: false }`. If `allow_primitives` is true, the primitives 'true', 'false' and 'null' are accepted as valid JSON values. diff --git a/src/index.js b/src/index.js index 59b813c1f..a0e8aa63c 100644 --- a/src/index.js +++ b/src/index.js @@ -90,6 +90,7 @@ import isISO8601 from './lib/isISO8601'; import isRFC3339 from './lib/isRFC3339'; import isISO31661Alpha2 from './lib/isISO31661Alpha2'; import isISO31661Alpha3 from './lib/isISO31661Alpha3'; +import isISO4217 from './lib/isISO4217'; import isBase32 from './lib/isBase32'; import isBase58 from './lib/isBase58'; @@ -198,6 +199,7 @@ const validator = { isRFC3339, isISO31661Alpha2, isISO31661Alpha3, + isISO4217, isBase32, isBase58, isBase64, diff --git a/src/lib/isISO4217.js b/src/lib/isISO4217.js new file mode 100644 index 000000000..3c297b536 --- /dev/null +++ b/src/lib/isISO4217.js @@ -0,0 +1,38 @@ +import assertString from './util/assertString'; + +// from https://en.wikipedia.org/wiki/ISO_4217 +const validISO4217CurrencyCodes = [ + 'AED', 'AFN', 'ALL', 'AMD', 'ANG', 'AOA', 'ARS', 'AUD', 'AWG', 'AZN', + 'BAM', 'BBD', 'BDT', 'BGN', 'BHD', 'BIF', 'BMD', 'BND', 'BOB', 'BOV', 'BRL', 'BSD', 'BTN', 'BWP', 'BYN', 'BZD', + 'CAD', 'CDF', 'CHE', 'CHF', 'CHW', 'CLF', 'CLP', 'CNY', 'COP', 'COU', 'CRC', 'CUC', 'CUP', 'CVE', 'CZK', + 'DJF', 'DKK', 'DOP', 'DZD', + 'EGP', 'ERN', 'ETB', 'EUR', + 'FJD', 'FKP', + 'GBP', 'GEL', 'GHS', 'GIP', 'GMD', 'GNF', 'GTQ', 'GYD', + 'HKD', 'HNL', 'HRK', 'HTG', 'HUF', + 'IDR', 'ILS', 'INR', 'IQD', 'IRR', 'ISK', + 'JMD', 'JOD', 'JPY', + 'KES', 'KGS', 'KHR', 'KMF', 'KPW', 'KRW', 'KWD', 'KYD', 'KZT', + 'LAK', 'LBP', 'LKR', 'LRD', 'LSL', 'LYD', + 'MAD', 'MDL', 'MGA', 'MKD', 'MMK', 'MNT', 'MOP', 'MRU', 'MUR', 'MVR', 'MWK', 'MXN', 'MXV', 'MYR', 'MZN', + 'NAD', 'NGN', 'NIO', 'NOK', 'NPR', 'NZD', + 'OMR', + 'PAB', 'PEN', 'PGK', 'PHP', 'PKR', 'PLN', 'PYG', + 'QAR', + 'RON', 'RSD', 'RUB', 'RWF', + 'SAR', 'SBD', 'SCR', 'SDG', 'SEK', 'SGD', 'SHP', 'SLL', 'SOS', 'SRD', 'SSP', 'STN', 'SVC', 'SYP', 'SZL', + 'THB', 'TJS', 'TMT', 'TND', 'TOP', 'TRY', 'TTD', 'TWD', 'TZS', + 'UAH', 'UGX', 'USD', 'USN', 'UYI', 'UYU', 'UYW', 'UZS', + 'VES', 'VND', 'VUV', + 'WST', + 'XAF', 'XAG', 'XAU', 'XBA', 'XBB', 'XBC', 'XBD', 'XCD', 'XDR', 'XOF', 'XPD', 'XPF', 'XPT', 'XSU', 'XTS', 'XUA', 'XXX', + 'YER', + 'ZAR', 'ZMW', 'ZWL', +]; + +export default function isISO4217(str) { + assertString(str); + return validISO4217CurrencyCodes.indexOf(str.toUpperCase()) >= 0; +} + +export const CurrencyCodes = validISO4217CurrencyCodes; diff --git a/test/validators.js b/test/validators.js index 6bb607282..b04db8c1a 100644 --- a/test/validators.js +++ b/test/validators.js @@ -9242,6 +9242,32 @@ describe('Validators', () => { }); }); + it('should validate ISO 4217 corrency codes', () => { + // from https://en.wikipedia.org/wiki/ISO_4217 + test({ + validator: 'isISO4217', + valid: [ + 'AED', + 'AUD', + 'CUC', + 'EUR', + 'GBP', + 'LYD', + 'MYR', + 'SGD', + 'USD', + ], + invalid: [ + '', + '$', + 'US', + 'AAA', + 'RWA', + 'EURO', + ], + }); + }); + it('should validate whitelisted characters', () => { test({ validator: 'isWhitelisted', From 84ed10b5fb5189d23ece8547c842a5637151db9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20Maria=20Pay=C3=A1=20Castillo?= Date: Thu, 12 Aug 2021 14:01:02 +0200 Subject: [PATCH 2/3] perf(isISO4217): use a Set along with .has instead of .indexOf --- src/lib/isISO4217.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/isISO4217.js b/src/lib/isISO4217.js index 3c297b536..0738614c9 100644 --- a/src/lib/isISO4217.js +++ b/src/lib/isISO4217.js @@ -1,7 +1,7 @@ import assertString from './util/assertString'; // from https://en.wikipedia.org/wiki/ISO_4217 -const validISO4217CurrencyCodes = [ +const validISO4217CurrencyCodes = new Set([ 'AED', 'AFN', 'ALL', 'AMD', 'ANG', 'AOA', 'ARS', 'AUD', 'AWG', 'AZN', 'BAM', 'BBD', 'BDT', 'BGN', 'BHD', 'BIF', 'BMD', 'BND', 'BOB', 'BOV', 'BRL', 'BSD', 'BTN', 'BWP', 'BYN', 'BZD', 'CAD', 'CDF', 'CHE', 'CHF', 'CHW', 'CLF', 'CLP', 'CNY', 'COP', 'COU', 'CRC', 'CUC', 'CUP', 'CVE', 'CZK', @@ -28,11 +28,11 @@ const validISO4217CurrencyCodes = [ 'XAF', 'XAG', 'XAU', 'XBA', 'XBB', 'XBC', 'XBD', 'XCD', 'XDR', 'XOF', 'XPD', 'XPF', 'XPT', 'XSU', 'XTS', 'XUA', 'XXX', 'YER', 'ZAR', 'ZMW', 'ZWL', -]; +]); export default function isISO4217(str) { assertString(str); - return validISO4217CurrencyCodes.indexOf(str.toUpperCase()) >= 0; + return validISO4217CurrencyCodes.has(str.toUpperCase()); } export const CurrencyCodes = validISO4217CurrencyCodes; From dc9042befe7952561aa4247505a7b094528e9203 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20Maria=20Pay=C3=A1=20Castillo?= Date: Thu, 12 Aug 2021 14:01:56 +0200 Subject: [PATCH 3/3] refactor(isISO4217): Enhance tests with lowercase examples --- test/validators.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/validators.js b/test/validators.js index b04db8c1a..ef9c66aec 100644 --- a/test/validators.js +++ b/test/validators.js @@ -9248,6 +9248,7 @@ describe('Validators', () => { validator: 'isISO4217', valid: [ 'AED', + 'aed', 'AUD', 'CUC', 'EUR', @@ -9261,9 +9262,12 @@ describe('Validators', () => { '', '$', 'US', + 'us', 'AAA', + 'aaa', 'RWA', 'EURO', + 'euro', ], }); });