ilib-locale
Version:
A BCP-47/IETF locale specifier parser/validator
3 lines (2 loc) • 6.97 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports["default"]=void 0;var _a2toa3regmap=require("./a2toa3regmap.js");var _a1toa3langmap=require("./a1toa3langmap.js");var _scripts=require("./scripts.js");var ilibEnv=_interopRequireWildcard(require("ilib-env"));function _getRequireWildcardCache(e){if("function"!=typeof WeakMap)return null;var r=new WeakMap,t=new WeakMap;return(_getRequireWildcardCache=function _getRequireWildcardCache(e){return e?t:r})(e)}function _interopRequireWildcard(e,r){if(!r&&e&&e.__esModule)return e;if(null===e||"object"!=_typeof(e)&&"function"!=typeof e)return{"default":e};var t=_getRequireWildcardCache(r);if(t&&t.has(e))return t.get(e);var n={__proto__:null},a=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var u in e)if("default"!==u&&{}.hasOwnProperty.call(e,u)){var i=a?Object.getOwnPropertyDescriptor(e,u):null;i&&(i.get||i.set)?Object.defineProperty(n,u,i):n[u]=e[u]}return n["default"]=e,t&&t.set(e,n),n}function _typeof(o){"@babel/helpers - typeof";return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(o){return typeof o}:function(o){return o&&"function"==typeof Symbol&&o.constructor===Symbol&&o!==Symbol.prototype?"symbol":typeof o},_typeof(o)}function _classCallCheck(a,n){if(!(a instanceof n))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,r){for(var t=0;t<r.length;t++){var o=r[t];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,_toPropertyKey(o.key),o)}}function _createClass(e,r,t){return r&&_defineProperties(e.prototype,r),t&&_defineProperties(e,t),Object.defineProperty(e,"prototype",{writable:!1}),e}function _toPropertyKey(t){var i=_toPrimitive(t,"string");return"symbol"==_typeof(i)?i:i+""}function _toPrimitive(t,r){if("object"!=_typeof(t)||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var i=e.call(t,r||"default");if("object"!=_typeof(i))return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===r?String:Number)(t)}function indexOf(array,obj){if(!array||!obj){return-1}if(typeof array.indexOf==="function"){return array.indexOf(obj)}else{for(var i=0;i<array.length;i++){if(array[i]===obj){return i}}return-1}};var Locale=function(){function Locale(language,region,variant,script){_classCallCheck(this,Locale);if(typeof region==="undefined"&&typeof variant==="undefined"&&typeof script==="undefined"){var spec=language||ilibEnv.getLocale();if(typeof spec==="string"){var parts=spec.split(/[-_]/g);for(var i=0;i<parts.length;i++){if(Locale._isExtensionSingleton(parts[i])&&i<parts.length-1){var extensionPart=parts.slice(i).join("-");this.variant=this.variant?this.variant+"-"+extensionPart:extensionPart;break}else if(Locale._isLanguageCode(parts[i])){this.language=parts[i]}else if(Locale._isRegionCode(parts[i])){this.region=parts[i]}else if(Locale._isScriptCode(parts[i])){this.script=parts[i]}else{this.variant=this.variant?this.variant+"-"+parts[i]:parts[i]}}this.language=this.language||undefined;this.region=this.region||undefined;this.script=this.script||undefined;this.variant=this.variant||undefined}else if(_typeof(spec)==="object"){this.language=spec.language||undefined;this.region=spec.region||undefined;this.script=spec.script||undefined;this.variant=spec.variant||undefined}}else{if(language&&typeof language==="string"){language=language.trim();this.language=language.length>0?language.toLowerCase():undefined}else{this.language=undefined}if(region&&typeof region==="string"){region=region.trim();this.region=region.length>0?region.toUpperCase():undefined}else{this.region=undefined}if(variant&&typeof variant==="string"){variant=variant.trim();this.variant=variant.length>0?variant:undefined}else{this.variant=undefined}if(script&&typeof script==="string"){script=script.trim();this.script=script.length>0?script:undefined}else{this.script=undefined}}this._genSpec()}return _createClass(Locale,[{key:"_genSpec",value:function _genSpec(){this.spec=[this.language,this.script,this.region,this.variant].filter(function(part){return part}).join("-")}},{key:"getLanguage",value:function getLanguage(){return this.language}},{key:"getLanguageAlpha3",value:function getLanguageAlpha3(){return Locale.languageAlpha1ToAlpha3(this.language)}},{key:"getRegion",value:function getRegion(){return this.region}},{key:"getRegionAlpha3",value:function getRegionAlpha3(){return Locale.regionAlpha2ToAlpha3(this.region)}},{key:"getScript",value:function getScript(){return this.script}},{key:"getVariant",value:function getVariant(){return this.variant}},{key:"getSpec",value:function getSpec(){if(!this.spec)this._genSpec();return this.spec}},{key:"getLangSpec",value:function getLangSpec(){var spec=this.language;if(spec&&this.script){spec+="-"+this.script}return spec||""}},{key:"toString",value:function toString(){return this.getSpec()}},{key:"equals",value:function equals(other){return this.language===other.language&&this.region===other.region&&this.script===other.script&&this.variant===other.variant}},{key:"isValid",value:function isValid(){if(!this.language&&!this.script&&!this.region)return false;return!!((!this.language||Locale._isLanguageCode(this.language)&&Locale.a1toa3langmap[this.language])&&(!this.script||Locale._isScriptCode(this.script)&&Locale.iso15924.indexOf(this.script)>-1)&&(!this.region||Locale._isRegionCode(this.region)&&Locale.a2toa3regmap[this.region]))}}])}();;Locale.a2toa3regmap=_a2toa3regmap.a2toa3regmap;Locale.a1toa3langmap=_a1toa3langmap.a1toa3langmap;Locale.iso15924=_scripts.iso15924.scripts;Locale._notLower=function(str){var ch=str.charCodeAt(0);return ch<97||ch>122};Locale._notUpper=function(str){var ch=str.charCodeAt(0);return ch<65||ch>90};Locale._notDigit=function(str){var ch=str.charCodeAt(0);return ch<48||ch>57};Locale._isLanguageCode=function(str){if(typeof str==="undefined"||str.length<2||str.length>3){return false}for(var i=0;i<str.length;i++){if(Locale._notLower(str.charAt(i))){return false}}return true};Locale._isRegionCode=function(str){var i;if(typeof str==="undefined"||str.length<2||str.length>3){return false}if(str.length===2){for(i=0;i<str.length;i++){if(Locale._notUpper(str.charAt(i))){return false}}}else{for(i=0;i<str.length;i++){if(Locale._notDigit(str.charAt(i))){return false}}}return true};Locale._isScriptCode=function(str){if(typeof str==="undefined"||str.length!==4||Locale._notUpper(str.charAt(0))){return false}for(var i=1;i<4;i++){if(Locale._notLower(str.charAt(i))){return false}}return true};Locale._isExtensionSingleton=function(str){if(typeof str==="undefined"||str.length!==1){return false}return!Locale._notLower(str.charAt(0))};Locale.regionAlpha2ToAlpha3=function(alpha2){return Locale.a2toa3regmap[alpha2]||alpha2};Locale.languageAlpha1ToAlpha3=function(alpha1){return Locale.a1toa3langmap[alpha1]||alpha1};var _default=exports["default"]=Locale;module.exports=exports.default;
//# sourceMappingURL=Locale.js.map