diff --git a/README.md b/README.md index e00b7cfac..dd60806ac 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,7 @@ Validator | Description **isISO6346(str)** | check if the string is a valid [ISO 6346](https://en.wikipedia.org/wiki/ISO_6346) shipping container identification. **isISO6391(str)** | check if the string is a valid [ISO 639-1][ISO 639-1] language code. **isISO8601(str [, options])** | check if the string is a valid [ISO 8601][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. +**isISO15924(str)** | check if the string is a valid [ISO 15924][ISO 15924] officially assigned script code. **isISO31661Alpha2(str)** | check if the string is a valid [ISO 3166-1 alpha-2][ISO 3166-1 alpha-2] officially assigned country code. **isISO31661Alpha3(str)** | check if the string is a valid [ISO 3166-1 alpha-3][ISO 3166-1 alpha-3] officially assigned country code. **isISO4217(str)** | check if the string is a valid [ISO 4217][ISO 4217] officially assigned currency code. @@ -295,6 +296,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. [ISIN]: https://en.wikipedia.org/wiki/International_Securities_Identification_Number [ISO 639-1]: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes [ISO 8601]: https://en.wikipedia.org/wiki/ISO_8601 +[ISO 15924]: https://en.wikipedia.org/wiki/ISO_15924 [ISO 3166-1 alpha-2]: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 [ISO 3166-1 alpha-3]: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3 [ISO 4217]: https://en.wikipedia.org/wiki/ISO_4217 diff --git a/src/index.js b/src/index.js index bef4cfff4..a86eb7a69 100644 --- a/src/index.js +++ b/src/index.js @@ -92,6 +92,7 @@ import { isISO6346, isFreightContainerID } from './lib/isISO6346'; import isISO6391 from './lib/isISO6391'; import isISO8601 from './lib/isISO8601'; import isRFC3339 from './lib/isRFC3339'; +import isISO15924 from './lib/isISO15924'; import isISO31661Alpha2 from './lib/isISO31661Alpha2'; import isISO31661Alpha3 from './lib/isISO31661Alpha3'; import isISO4217 from './lib/isISO4217'; @@ -205,6 +206,7 @@ const validator = { isFreightContainerID, isISO6391, isISO8601, + isISO15924, isRFC3339, isISO31661Alpha2, isISO31661Alpha3, diff --git a/src/lib/isISO15924.js b/src/lib/isISO15924.js new file mode 100644 index 000000000..30f9e4a4f --- /dev/null +++ b/src/lib/isISO15924.js @@ -0,0 +1,37 @@ +import assertString from './util/assertString'; + +// from https://www.unicode.org/iso15924/iso15924-codes.html +const validISO15924Codes = new Set([ + 'Adlm', 'Afak', 'Aghb', 'Ahom', 'Arab', 'Aran', 'Armi', 'Armn', 'Avst', + 'Bali', 'Bamu', 'Bass', 'Batk', 'Beng', 'Bhks', 'Blis', 'Bopo', 'Brah', 'Brai', 'Bugi', 'Buhd', + 'Cakm', 'Cans', 'Cari', 'Cham', 'Cher', 'Chis', 'Chrs', 'Cirt', 'Copt', 'Cpmn', 'Cprt', 'Cyrl', 'Cyrs', + 'Deva', 'Diak', 'Dogr', 'Dsrt', 'Dupl', + 'Egyd', 'Egyh', 'Egyp', 'Elba', 'Elym', 'Ethi', + 'Gara', 'Geok', 'Geor', 'Glag', 'Gong', 'Gonm', 'Goth', 'Gran', 'Grek', 'Gujr', 'Gukh', 'Guru', + 'Hanb', 'Hang', 'Hani', 'Hano', 'Hans', 'Hant', 'Hatr', 'Hebr', 'Hira', 'Hluw', 'Hmng', 'Hmnp', 'Hrkt', 'Hung', + 'Inds', 'Ital', + 'Jamo', 'Java', 'Jpan', 'Jurc', + 'Kali', 'Kana', 'Kawi', 'Khar', 'Khmr', 'Khoj', 'Kitl', 'Kits', 'Knda', 'Kore', 'Kpel', 'Krai', 'Kthi', + 'Lana', 'Laoo', 'Latf', 'Latg', 'Latn', 'Leke', 'Lepc', 'Limb', 'Lina', 'Linb', 'Lisu', 'Loma', 'Lyci', 'Lydi', + 'Mahj', 'Maka', 'Mand', 'Mani', 'Marc', 'Maya', 'Medf', 'Mend', 'Merc', 'Mero', 'Mlym', 'Modi', 'Mong', 'Moon', 'Mroo', 'Mtei', 'Mult', 'Mymr', + 'Nagm', 'Nand', 'Narb', 'Nbat', 'Newa', 'Nkdb', 'Nkgb', 'Nkoo', 'Nshu', + 'Ogam', 'Olck', 'Onao', 'Orkh', 'Orya', 'Osge', 'Osma', 'Ougr', + 'Palm', 'Pauc', 'Pcun', 'Pelm', 'Perm', 'Phag', 'Phli', 'Phlp', 'Phlv', 'Phnx', 'Plrd', 'Piqd', 'Prti', 'Psin', + 'Qaaa', 'Qaab', 'Qaac', 'Qaad', 'Qaae', 'Qaaf', 'Qaag', 'Qaah', 'Qaai', 'Qaaj', 'Qaak', 'Qaal', 'Qaam', 'Qaan', 'Qaao', 'Qaap', 'Qaaq', 'Qaar', 'Qaas', 'Qaat', 'Qaau', 'Qaav', 'Qaaw', 'Qaax', 'Qaay', 'Qaaz', 'Qaba', 'Qabb', 'Qabc', 'Qabd', 'Qabe', 'Qabf', 'Qabg', 'Qabh', 'Qabi', 'Qabj', 'Qabk', 'Qabl', 'Qabm', 'Qabn', 'Qabo', 'Qabp', 'Qabq', 'Qabr', 'Qabs', 'Qabt', 'Qabu', 'Qabv', 'Qabw', 'Qabx', + 'Ranj', 'Rjng', 'Rohg', 'Roro', 'Runr', + 'Samr', 'Sara', 'Sarb', 'Saur', 'Sgnw', 'Shaw', 'Shrd', 'Shui', 'Sidd', 'Sidt', 'Sind', 'Sinh', 'Sogd', 'Sogo', 'Sora', 'Soyo', 'Sund', 'Sunu', 'Sylo', 'Syrc', 'Syre', 'Syrj', 'Syrn', + 'Tagb', 'Takr', 'Tale', 'Talu', 'Taml', 'Tang', 'Tavt', 'Tayo', 'Telu', 'Teng', 'Tfng', 'Tglg', 'Thaa', 'Thai', 'Tibt', 'Tirh', 'Tnsa', 'Todr', 'Tols', 'Toto', 'Tutg', + 'Ugar', + 'Vaii', 'Visp', 'Vith', + 'Wara', 'Wcho', 'Wole', + 'Xpeo', 'Xsux', + 'Yezi', 'Yiii', + 'Zanb', 'Zinh', 'Zmth', 'Zsye', 'Zsym', 'Zxxx', 'Zyyy', 'Zzzz', +]); + +export default function isISO15924(str) { + assertString(str); + return validISO15924Codes.has(str); +} + +export const ScriptCodes = validISO15924Codes; diff --git a/test/validators.test.js b/test/validators.test.js index 6c68cd71a..ed5326a17 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -11333,6 +11333,28 @@ describe('Validators', () => { }); }); + it('should validate ISO 15924 script codes', () => { + test({ + validator: 'isISO15924', + valid: [ + 'Adlm', + 'Bass', + 'Copt', + 'Dsrt', + 'Egyd', + 'Latn', + 'Zzzz', + ], + invalid: [ + '', + 'arab', + 'zzzz', + 'Qaby', + 'Lati', + ], + }); + }); + it('should validate RFC 3339 dates', () => { test({ validator: 'isRFC3339',