Skip to content

Commit

Permalink
feat(isISO4217): add currency code validator (#1706)
Browse files Browse the repository at this point in the history
* feat(isISO4217): add currency code validator (#1703)

* perf(isISO4217): use a Set along with .has instead of .indexOf

* refactor(isISO4217): Enhance tests with lowercase examples
  • Loading branch information
jmpaya authored Sep 26, 2021
1 parent 7376945 commit f34112d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. <br/>`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).<br/><br/>`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).<br/><br/>`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.
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -198,6 +199,7 @@ const validator = {
isRFC3339,
isISO31661Alpha2,
isISO31661Alpha3,
isISO4217,
isBase32,
isBase58,
isBase64,
Expand Down
38 changes: 38 additions & 0 deletions src/lib/isISO4217.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import assertString from './util/assertString';

// from https://en.wikipedia.org/wiki/ISO_4217
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',
'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.has(str.toUpperCase());
}

export const CurrencyCodes = validISO4217CurrencyCodes;
30 changes: 30 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -9242,6 +9242,36 @@ describe('Validators', () => {
});
});

it('should validate ISO 4217 corrency codes', () => {
// from https://en.wikipedia.org/wiki/ISO_4217
test({
validator: 'isISO4217',
valid: [
'AED',
'aed',
'AUD',
'CUC',
'EUR',
'GBP',
'LYD',
'MYR',
'SGD',
'USD',
],
invalid: [
'',
'$',
'US',
'us',
'AAA',
'aaa',
'RWA',
'EURO',
'euro',
],
});
});

it('should validate whitelisted characters', () => {
test({
validator: 'isWhitelisted',
Expand Down

0 comments on commit f34112d

Please sign in to comment.