Update utils.js

This commit is contained in:
freearhey 2021-01-30 07:32:31 +03:00
parent 6a33900a10
commit 598a495239
1 changed files with 21 additions and 5 deletions

View File

@ -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)