From 598a495239025b3b266b398cb46503d7970843b8 Mon Sep 17 00:00:00 2001 From: freearhey Date: Sat, 30 Jan 2021 07:32:31 +0300 Subject: [PATCH] Update utils.js --- scripts/utils.js | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) 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)