diff --git a/scripts/utils.js b/scripts/utils.js index 063f3e3ae..6981caa2c 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -6,6 +6,10 @@ const urlParser = require('url') const escapeStringRegexp = require('escape-string-regexp') const markdownInclude = require('markdown-include') const iso6393 = require('iso-639-3') +const intlDisplayNames = new Intl.DisplayNames(['en'], { + style: 'narrow', + type: 'region' +}) const utils = {} @@ -180,11 +184,6 @@ utils.code2name = function (code) { return 'United States' } - const intlDisplayNames = new Intl.DisplayNames(['en'], { - style: 'narrow', - type: 'region' - }) - try { return intlDisplayNames.of(code.toUpperCase()) } catch (e) { @@ -192,6 +191,23 @@ utils.code2name = function (code) { } } +utils.codeIsValid = function (code) { + switch (code.toLowerCase()) { + case 'int': + return true + case 'us': + return true + } + + try { + intlDisplayNames.of(code.toUpperCase()) + + return true + } catch (e) { + return false + } +} + utils.language2code = function (name) { const lang = iso6393.find(l => l.name === name)