Skip to content

Commit 79f5d18

Browse files
feat(isISO31661Numeric): Add validation for ISO 3166-1 numeric country codes (#2399)
* Create isISO31661Numeric.js * Add tests for ISO 3166-1 numeric country codes * Describe isISO31661Numeric in README.md * Add isISO31661Numeric to index.js
1 parent cd4e7bf commit 79f5d18

4 files changed

Lines changed: 61 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ Validator | Description
126126
**isISO8601(str [, options])** | check if the string is a valid [ISO 8601][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.
127127
**isISO31661Alpha2(str)** | check if the string is a valid [ISO 3166-1 alpha-2][ISO 3166-1 alpha-2] officially assigned country code.
128128
**isISO31661Alpha3(str)** | check if the string is a valid [ISO 3166-1 alpha-3][ISO 3166-1 alpha-3] officially assigned country code.
129+
**isISO31661Numeric(str)** | check if the string is a valid [ISO 3166-1 numeric][ISO 3166-1 numeric] officially assigned country code.
129130
**isISO4217(str)** | check if the string is a valid [ISO 4217][ISO 4217] officially assigned currency code.
130131
**isISRC(str)** | check if the string is an [ISRC][ISRC].
131132
**isISSN(str [, options])** | check if the string is an [ISSN][ISSN].<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.
@@ -261,6 +262,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
261262
[ISO 8601]: https://en.wikipedia.org/wiki/ISO_8601
262263
[ISO 3166-1 alpha-2]: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
263264
[ISO 3166-1 alpha-3]: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3
265+
[ISO 3166-1 numeric]: https://en.wikipedia.org/wiki/ISO_3166-1_numeric
264266
[ISO 4217]: https://en.wikipedia.org/wiki/ISO_4217
265267
[ISRC]: https://en.wikipedia.org/wiki/International_Standard_Recording_Code
266268
[ISSN]: https://en.wikipedia.org/wiki/International_Standard_Serial_Number

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ import isISO8601 from './lib/isISO8601';
9595
import isRFC3339 from './lib/isRFC3339';
9696
import isISO31661Alpha2 from './lib/isISO31661Alpha2';
9797
import isISO31661Alpha3 from './lib/isISO31661Alpha3';
98+
import isISO31661Numeric from './lib/isISO31661Numeric';
9899
import isISO4217 from './lib/isISO4217';
99100

100101
import isBase32 from './lib/isBase32';
@@ -210,6 +211,7 @@ const validator = {
210211
isRFC3339,
211212
isISO31661Alpha2,
212213
isISO31661Alpha3,
214+
isISO31661Numeric,
213215
isISO4217,
214216
isBase32,
215217
isBase58,

src/lib/isISO31661Numeric.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import assertString from './util/assertString';
2+
3+
// from https://en.wikipedia.org/wiki/ISO_3166-1_numeric
4+
const validISO31661NumericCountriesCodes = new Set([
5+
'004', '008', '010', '012', '016', '020', '024', '028', '031', '032', '036', '040', '044', '048', '050', '051',
6+
'052', '056', '060', '064', '068', '070', '072', '074', '076', '084', '086', '090', '092', '096', '100', '104',
7+
'108', '112', '116', '120', '124', '132', '136', '140', '144', '148', '152', '156', '158', '162', '166', '170',
8+
'174', '175', '178', '180', '184', '188', '191', '192', '196', '203', '204', '208', '212', '214', '218', '222',
9+
'226', '231', '232', '233', '234', '238', '239', '242', '246', '248', '250', '254', '258', '260', '262', '266',
10+
'268', '270', '275', '276', '288', '292', '296', '300', '304', '308', '312', '316', '320', '324', '328', '332',
11+
'334', '336', '340', '344', '348', '352', '356', '360', '364', '368', '372', '376', '380', '384', '388', '392',
12+
'398', '400', '404', '408', '410', '414', '417', '418', '422', '426', '428', '430', '434', '438', '440', '442',
13+
'446', '450', '454', '458', '462', '466', '470', '474', '478', '480', '484', '492', '496', '498', '499', '500',
14+
'504', '508', '512', '516', '520', '524', '528', '531', '533', '534', '535', '540', '548', '554', '558', '562',
15+
'566', '570', '574', '578', '580', '581', '583', '584', '585', '586', '591', '598', '600', '604', '608', '612',
16+
'616', '620', '624', '626', '630', '634', '638', '642', '643', '646', '652', '654', '659', '660', '662', '663',
17+
'666', '670', '674', '678', '682', '686', '688', '690', '694', '702', '703', '704', '705', '706', '710', '716',
18+
'724', '728', '729', '732', '740', '744', '748', '752', '756', '760', '762', '764', '768', '772', '776', '780',
19+
'784', '788', '792', '795', '796', '798', '800', '804', '807', '818', '826', '831', '832', '833', '834', '840',
20+
'850', '854', '858', '860', '862', '876', '882', '887', '894',
21+
]);
22+
23+
export default function isISO31661Numeric(str) {
24+
assertString(str);
25+
return validISO31661NumericCountriesCodes.has(str);
26+
}

test/validators.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11546,6 +11546,37 @@ describe('Validators', () => {
1154611546
});
1154711547
});
1154811548

11549+
it('should validate ISO 3166-1 numeric country codes', () => {
11550+
// from https://en.wikipedia.org/wiki/ISO_3166-1_numeric
11551+
test({
11552+
validator: 'isISO31661Numeric',
11553+
valid: [
11554+
'076',
11555+
'208',
11556+
'276',
11557+
'348',
11558+
'380',
11559+
'410',
11560+
'440',
11561+
'528',
11562+
'554',
11563+
'826',
11564+
],
11565+
invalid: [
11566+
'',
11567+
'NL',
11568+
'NLD',
11569+
'002',
11570+
'197',
11571+
'249',
11572+
'569',
11573+
'810',
11574+
'900',
11575+
'999',
11576+
],
11577+
});
11578+
});
11579+
1154911580
it('should validate ISO 4217 corrency codes', () => {
1155011581
// from https://en.wikipedia.org/wiki/ISO_4217
1155111582
test({

0 commit comments

Comments
 (0)