Skip to content

Commit b039202

Browse files
authored
feat(isISO6391): add ISO 639-1 validator (#1892)
* docs: add ISO 639-1 to readme * feat: implement ISO 639-1 validator and test * update readme * fix: clean up whitespace in locale list * refactor: write more concise test code
1 parent cf8dcea commit b039202

4 files changed

Lines changed: 46 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ Validator | Description
130130
**isIPRange(str [, version])** | check if the string is an IP Range (version 4 or 6).
131131
**isISBN(str [, version])** | check if the string is an ISBN (version 10 or 13).
132132
**isISIN(str)** | check if the string is an [ISIN][ISIN] (stock/security identifier).
133+
**isISO6391(str)** | check if the string is a valid [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) language code.
133134
**isISO8601(str [, options])** | 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.
134135
**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.
135136
**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.

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ import isCurrency from './lib/isCurrency';
8686

8787
import isBtcAddress from './lib/isBtcAddress';
8888

89+
import isISO6391 from './lib/isISO6391';
8990
import isISO8601 from './lib/isISO8601';
9091
import isRFC3339 from './lib/isRFC3339';
9192
import isISO31661Alpha2 from './lib/isISO31661Alpha2';
@@ -195,6 +196,7 @@ const validator = {
195196
isEthereumAddress,
196197
isCurrency,
197198
isBtcAddress,
199+
isISO6391,
198200
isISO8601,
199201
isRFC3339,
200202
isISO31661Alpha2,

src/lib/isISO6391.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import assertString from './util/assertString';
2+
3+
const isISO6391Set = new Set([
4+
'aa', 'ab', 'ae', 'af', 'ak', 'am', 'an', 'ar', 'as', 'av', 'ay', 'az', 'az',
5+
'ba', 'be', 'bg', 'bh', 'bi', 'bm', 'bn', 'bo', 'br', 'bs',
6+
'ca', 'ce', 'ch', 'co', 'cr', 'cs', 'cu', 'cv', 'cy',
7+
'da', 'de', 'dv', 'dz',
8+
'ee', 'el', 'en', 'eo', 'es', 'et', 'eu',
9+
'fa', 'ff', 'fi', 'fj', 'fo', 'fr', 'fy',
10+
'ga', 'gd', 'gl', 'gn', 'gu', 'gv',
11+
'ha', 'he', 'hi', 'ho', 'hr', 'ht', 'hu', 'hy', 'hz',
12+
'ia', 'id', 'ie', 'ig', 'ii', 'ik', 'io', 'is', 'it', 'iu',
13+
'ja', 'jv',
14+
'ka', 'kg', 'ki', 'kj', 'kk', 'kl', 'km', 'kn', 'ko', 'kr', 'ks', 'ku', 'kv', 'kw', 'ky',
15+
'la', 'lb', 'lg', 'li', 'ln', 'lo', 'lt', 'lu', 'lv',
16+
'mg', 'mh', 'mi', 'mk', 'ml', 'mn', 'mr', 'ms', 'mt', 'my',
17+
'na', 'nb', 'nd', 'ne', 'ng', 'nl', 'nn', 'no', 'nr', 'nv', 'ny',
18+
'oc', 'oj', 'om', 'or', 'os',
19+
'pa', 'pi', 'pl', 'ps', 'pt',
20+
'qu',
21+
'rm', 'rn', 'ro', 'ru', 'rw',
22+
'sa', 'sc', 'sd', 'se', 'sg', 'si', 'sk', 'sl', 'sm', 'sn', 'so', 'sq', 'sr', 'ss', 'st', 'su', 'sv', 'sw',
23+
'ta', 'te', 'tg', 'th', 'ti', 'tk', 'tl', 'tn', 'to', 'tr', 'ts', 'tt', 'tw', 'ty',
24+
'ug', 'uk', 'ur', 'uz',
25+
've', 'vi', 'vo',
26+
'wa', 'wo',
27+
'xh',
28+
'yi', 'yo',
29+
'za', 'zh', 'zu',
30+
]);
31+
32+
export default function isISO6391(str) {
33+
assertString(str);
34+
return isISO6391Set.has(str);
35+
}

test/validators.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9792,6 +9792,14 @@ describe('Validators', () => {
97929792
});
97939793
});
97949794

9795+
it('should validate ISO 639-1 language codes', () => {
9796+
test({
9797+
validator: 'isISO6391',
9798+
valid: ['ay', 'az', 'ba', 'be', 'bg'],
9799+
invalid: ['aj', 'al', 'pe', 'pf', 'abc', '123', ''],
9800+
});
9801+
});
9802+
97959803
const validISO8601 = [
97969804
'2009-12T12:34',
97979805
'2009',

0 commit comments

Comments
 (0)