UNPKG

all-iso-language-codes

Version:

List of all iso 639-1, 639-2T, 639-2B, 639-3 codes with their translations in any language

127 lines (126 loc) 5.95 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAll639_3 = exports.getAll639_2T = exports.getAll639_2B = exports.getAll639_1 = exports.getIsoCodesFromNativeName = exports.isValid = exports.getNativeName = exports.getName = exports.getEnglishName = void 0; const languageImports639_1 = __importStar(require("./imports")); const advanced_json_1 = __importDefault(require("../data/advanced.json")); const all_json_1 = __importDefault(require("../data/all.json")); const finder = (isoType, code) => advanced_json_1.default.find(obj => obj[isoType] === code); // https://stackoverflow.com/a/37511463 const normalizeString = (str) => str .toLowerCase() .normalize('NFD') .replace(/[\u0300-\u036f]/g, ''); const getCodeHelper = (code, availableIsoTypes) => { let found = null; if (((availableIsoTypes === null || availableIsoTypes === void 0 ? void 0 : availableIsoTypes.includes('639-3')) || !availableIsoTypes) && !found) { found = finder('639-3', code); } if (((availableIsoTypes === null || availableIsoTypes === void 0 ? void 0 : availableIsoTypes.includes('639-2T')) || !availableIsoTypes) && !found) { found = finder('639-2T', code); } if (((availableIsoTypes === null || availableIsoTypes === void 0 ? void 0 : availableIsoTypes.includes('639-2B')) || !availableIsoTypes) && !found) { found = finder('639-2B', code); } if (((availableIsoTypes === null || availableIsoTypes === void 0 ? void 0 : availableIsoTypes.includes('639-1')) || !availableIsoTypes) && !found) { found = finder('639-1', code); } return found; }; const getNameHelper = (code, language) => { var _a, _b, _c; const found = getCodeHelper(code); if (!found) { return null; } if ((found === null || found === void 0 ? void 0 : found['639-1']) && typeof language !== 'undefined' && (language !== 'en' || language !== found['639-1'])) { // @ts-expect-error Trust me this is the only way to do this return ((_b = (_a = languageImports639_1.default) === null || _a === void 0 ? void 0 : _a[language]) === null || _b === void 0 ? void 0 : _b[`${found === null || found === void 0 ? void 0 : found['639-1']}`]) || null; } return language === 'en' ? found === null || found === void 0 ? void 0 : found.englishName : ((_c = found === null || found === void 0 ? void 0 : found.nativeName) !== null && _c !== void 0 ? _c : null); }; const getEnglishName = (code) => (0, exports.getName)(code); exports.getEnglishName = getEnglishName; const getName = (code, language = 'en') => getNameHelper(code, language); exports.getName = getName; const getNativeName = (code) => getNameHelper(code); exports.getNativeName = getNativeName; const isValid = (code, availableIsoTypes, moreInfo = false) => { const found = getCodeHelper(code, availableIsoTypes); let iso = null; if (found === null || found === void 0 ? void 0 : found['639-1']) { iso = '639-1'; } else if (found === null || found === void 0 ? void 0 : found['639-2T']) { iso = '639-2T'; } else if (found === null || found === void 0 ? void 0 : found['639-2B']) { iso = '639-2B'; } else if (found === null || found === void 0 ? void 0 : found['639-3']) { iso = '639-3'; } return moreInfo ? { iso, found: Boolean(iso), } : Boolean(iso); }; exports.isValid = isValid; const getIsoCodesFromNativeName = (nativeName, normalizeName = false) => { const name = normalizeName ? normalizeString(nativeName) : nativeName; const found = advanced_json_1.default.find(language => (normalizeName ? normalizeString(language.nativeName) : language.nativeName) === name); const isoCodes = found ? Object.entries(found) .map(([key, value]) => value && key !== 'nativeName' && key !== 'englishName' ? key : null) .filter(Boolean) : null; return isoCodes; }; exports.getIsoCodesFromNativeName = getIsoCodesFromNativeName; const getAll639_1 = () => all_json_1.default['639-1']; exports.getAll639_1 = getAll639_1; const getAll639_2B = () => all_json_1.default['639-2B']; exports.getAll639_2B = getAll639_2B; const getAll639_2T = () => all_json_1.default['639-2T']; exports.getAll639_2T = getAll639_2T; const getAll639_3 = () => all_json_1.default['639-3']; exports.getAll639_3 = getAll639_3;