UNPKG

ilib

Version:

iLib is a cross-engine library of internationalization (i18n) classes written in pure JS

2 lines 16.3 kB
// !data states idd mnc var ilib=require("../index.js"),Utils=require("./Utils.js"),JSUtils=require("./JSUtils.js"),NumberingPlan=require("./NumberingPlan.js"),PhoneLocale=require("./PhoneLocale.js"),PhoneHandlerFactory=require("./PhoneHandlerFactory.js"),PhoneNumber=function(number,options){var regionSettings;if(this.sync=!0,this.loadParams={},options?("boolean"==typeof options.sync&&(this.sync=options.sync),options.loadParams&&(this.loadParams=options.loadParams),"function"==typeof options.onLoad&&(this.onLoad=options.onLoad)):options={sync:!0},!number||"string"==typeof number&&0===number.length)return"function"==typeof options.onLoad&&options.onLoad(void 0),this;"object"==typeof number&&(this.vsc=number.vsc,this.iddPrefix=number.iddPrefix,this.countryCode=number.countryCode,this.trunkAccess=number.trunkAccess,this.cic=number.cic,this.emergency=number.emergency,this.mobilePrefix=number.mobilePrefix,this.serviceCode=number.serviceCode,this.areaCode=number.areaCode,this.subscriberNumber=number.subscriberNumber,this.extension=number.extension,this.invalid=number.invalid,number.plan)&&number.locale?(this.plan=number.plan,this.locale=number.locale,this.destinationPlan=number.destinationPlan,this.destinationLocale=number.destinationLocale,options&&"function"==typeof options.onLoad&&options.onLoad(this)):new PhoneLocale({locale:options&&options.locale,mcc:options&&options.mcc,sync:this.sync,loadParams:this.loadParams,onLoad:ilib.bind(this,function(loc){this.locale=this.destinationLocale=loc,new NumberingPlan({locale:this.locale,sync:this.sync,loadParms:this.loadParams,onLoad:ilib.bind(this,function(plan){this.plan=this.destinationPlan=plan,"object"==typeof number?"function"==typeof options.onLoad&&options.onLoad(this):Utils.loadData({name:"states.json",object:"PhoneNumber",locale:this.locale,sync:this.sync,loadParams:JSUtils.merge(this.loadParams,{returnOne:!0}),callback:ilib.bind(this,function(stdata){stdata=stdata||PhoneNumber._defaultStates,regionSettings={stateData:stdata,plan:plan,handler:PhoneHandlerFactory(this.locale,plan)},number="^"+number.replace(/\^/g,""),number=PhoneNumber._stripFormatting(number),this._parseNumber(number,regionSettings,options)})})})})})})};PhoneNumber.parseImsi=function(imsi,options){var sync=!0,loadParams={},fields={};if(imsi)return options&&(void 0!==options.sync&&(sync=!!options.sync),options.loadParams)&&(loadParams=options.loadParams),ilib.data.mnc?(fields=PhoneNumber._parseImsi(ilib.data.mnc,imsi),options&&"function"==typeof options.onLoad&&options.onLoad(fields)):Utils.loadData({name:"mnc.json",object:"PhoneNumber",nonlocale:!0,sync:sync,loadParams:loadParams,callback:ilib.bind(this,function(data){ilib.data.mnc=data,fields=PhoneNumber._parseImsi(data,imsi),options&&"function"==typeof options.onLoad&&options.onLoad(fields)})}),fields;options&&"function"==typeof options.onLoad&&options.onLoad(void 0)},PhoneNumber._parseImsi=function(data,imsi){var i,lastLeaf,stateNumber,ch,fields={},consumed=0,currentState=data;if(currentState){for(i=0;i<imsi.length;){if(0<=(ch=PhoneNumber._getCharacterCode(imsi.charAt(i)))){if("object"!=typeof(ch=currentState.s&&currentState.s[ch]))return(void 0===ch||0===ch||"object"==typeof ch&&void 0===ch.l)&&lastLeaf&&(ch=lastLeaf,i=consumed),"number"==typeof ch&&ch||"object"==typeof ch&&void 0!==ch.l?(stateNumber="number"==typeof ch?ch:ch.l,stateNumber="area"===PhoneNumber._states[stateNumber]?i+1:6,fields.mcc=imsi.substring(0,3),fields.mnc=imsi.substring(3,stateNumber),fields.msin=imsi.substring(stateNumber)):6<=imsi.length&&(fields.mcc=imsi.substring(0,3),fields.mnc=imsi.substring(3,6),fields.msin=imsi.substring(6)),fields;void 0!==ch.l&&(lastLeaf=ch,consumed=i),currentState=ch}i++}return i>=imsi.length&&6<=imsi.length?(fields.mcc=imsi.substring(0,3),fields.mnc=imsi.substring(3,6),fields.msin=imsi.substring(6)):fields=void 0,fields}},PhoneNumber._stripFormatting=function(str){for(var ret="",i=0;i<str.length;i++)-1<=PhoneNumber._getCharacterCode(str.charAt(i))&&(ret+=str.charAt(i));return ret},PhoneNumber._getCharacterCode=function(ch){if("0"<=ch&&ch<="9")return ch-"0";switch(ch){case"+":return 10;case"*":return 11;case"#":return 12;case"^":return 13;case"p":case"P":case"t":case"T":case"w":case"W":return-1;case"x":case"X":case",":case";":return-1}return-2},PhoneNumber._states=["none","unknown","plus","idd","cic","service","cell","area","vsc","country","personal","special","trunk","premium","emergency","service2","service3","service4","cic2","cic3","start","local"],PhoneNumber._fieldOrder=["vsc","iddPrefix","countryCode","trunkAccess","cic","emergency","mobilePrefix","serviceCode","areaCode","subscriberNumber","extension"],PhoneNumber._defaultStates={s:[0,21,21,21,21,21,21,21,21,21,0,0,0,{s:[{s:[3],l:12},21,21,21,21,21,21,21,21,21,2]}]},PhoneNumber.prototype={_parseOtherCountry:function(number,regionData,options,countryCode){new PhoneLocale({locale:this.locale,countryCode:countryCode,sync:this.sync,loadParms:this.loadParams,onLoad:ilib.bind(this,function(loc){this.destinationLocale=loc,Utils.loadData({name:"states.json",object:"PhoneNumber",locale:this.destinationLocale,sync:this.sync,loadParams:JSUtils.merge(this.loadParams,{returnOne:!0}),callback:ilib.bind(this,function(stateData){stateData=stateData||PhoneNumber._defaultStates,new NumberingPlan({locale:this.destinationLocale,sync:this.sync,loadParms:this.loadParams,onLoad:ilib.bind(this,function(plan){this.destinationPlan=plan;var regionSettings={stateData:stateData,plan:plan,handler:PhoneHandlerFactory(this.destinationLocale,plan)};plan.getSkipTrunk()||(number="^"+number),this._parseNumber(number,regionSettings,options)})})})})})})},_parseNumber:function(number,regionData,options){for(var ch,currentState=regionData.stateData,lastLeaf=void 0,consumed=0,regionSettings=regionData,i=0;i<number.length;)if(0<=(ch=PhoneNumber._getCharacterCode(number.charAt(i))))if("object"==typeof(newState=!(newState=currentState.s&&currentState.s[ch])&&currentState.s&&currentState.s[14]?currentState.s[14]:newState)&&i+1<number.length)void 0!==newState.l&&(lastLeaf=newState,consumed=i),currentState=newState,i++;else{if((void 0===newState||0===newState||"object"==typeof newState&&void 0===newState.l)&&lastLeaf&&(newState=lastLeaf,i=consumed,lastLeaf=void(consumed=0)),!("number"==typeof newState&&newState||"object"==typeof newState&&void 0!==newState.l)){handlerMethod="number"==typeof newState?"none":"local",result="^"===number.charAt(0)?regionSettings.handler[handlerMethod](number.slice(1),i-1,this,regionSettings):regionSettings.handler[handlerMethod](number,i,this,regionSettings);break}var result,newState="number"==typeof newState?newState:newState.l,handlerMethod=PhoneNumber._states[newState];if(number=(result="^"===number.charAt(0)?regionSettings.handler[handlerMethod](number.slice(1),i-1,this,regionSettings):regionSettings.handler[handlerMethod](number,i,this,regionSettings)).number,i=consumed=0,currentState=regionSettings.stateData,(lastLeaf=void 0)!==result.countryCode)return void this._parseOtherCountry(number,regionData,options,result.countryCode);if(void 0!==result.table)return void Utils.loadData({name:result.table+".json",object:"PhoneNumber",nonlocale:!0,sync:this.sync,loadParams:this.loadParams,callback:ilib.bind(this,function(data){data=data||PhoneNumber._defaultStates,regionSettings={stateData:data,plan:regionSettings.plan,handler:regionSettings.handler},this._parseNumber(number,regionSettings,options)})});void 0!==result.skipTrunk&&(ch=PhoneNumber._getCharacterCode(regionSettings.plan.getTrunkCode()),currentState=currentState.s&&currentState.s[ch])}else i++;i>=number.length&&currentState!==regionData.stateData&&(handlerMethod=void 0===currentState.l||0===currentState?"none":"local",result="^"===number.charAt(0)?regionSettings.handler[handlerMethod](number.slice(1),i-1,this,regionSettings):regionSettings.handler[handlerMethod](number,i,this,regionSettings)),this.onLoad&&this.onLoad(this)},_getPrefix:function(){return this.areaCode||this.serviceCode||this.mobilePrefix||""},_hasPrefix:function(){return""!==this._getPrefix()},_xor:function(left,right){return!(void 0===left&&void 0===right||void 0!==left&&void 0!==right)},_join:function(){var fieldName,formatted="";try{for(var field in PhoneNumber._fieldOrder)"string"==typeof field&&"string"==typeof PhoneNumber._fieldOrder[field]&&void 0!==this[fieldName=PhoneNumber._fieldOrder[field]]&&(formatted+=this[fieldName])}catch(e){throw e}return formatted},compare:function(other){var thisPrefix,otherPrefix,match=100,FRdepartments={590:1,594:1,596:1,262:1},ITcountries={378:1,379:1},currentCountryCode=0;if("string"==typeof this.locale.region&&(currentCountryCode=this.locale._mapRegiontoCC(this.locale.region)),!this.subscriberNumber||!other.subscriberNumber||this.subscriberNumber!==other.subscriberNumber)return 0;if(this._xor(this.extension,other.extension)||this.extension!==other.extension)return 0;if(this._xor(this.countryCode,other.countryCode))switch(this.locale.getRegion()){case"FR":this.countryCode in FRdepartments||other.countryCode in FRdepartments?this.areaCode===other.areaCode&&this.mobilePrefix===other.mobilePrefix||(match-=100):match-=16;break;case"IT":this.countryCode in ITcountries||other.countryCode in ITcountries?this.areaCode!==other.areaCode&&(match-=100):match-=16;break;default:match-=16,(void 0!==this.countryCode&&this.countryCode!==currentCountryCode||void 0!==other.countryCode&&other.countryCode!==currentCountryCode)&&(match-=16)}else this.countryCode!==other.countryCode&&("33"===other.countryCode||"33"===this.countryCode?(this.countryCode in FRdepartments||other.countryCode in FRdepartments)&&this.areaCode===other.areaCode&&this.mobilePrefix===other.mobilePrefix||(match-=100):("39"===this.countryCode||"39"===other.countryCode)&&(this.countryCode in ITcountries||other.countryCode in ITcountries)&&this.areaCode===other.areaCode||(match-=100));return this._xor(this.serviceCode,other.serviceCode)?match-=20:this.serviceCode!==other.serviceCode&&(match-=100),this._xor(this.mobilePrefix,other.mobilePrefix)?match-=20:this.mobilePrefix!==other.mobilePrefix&&(match-=100),this._xor(this.areaCode,other.areaCode)?match-=12:this.areaCode!==other.areaCode&&(match-=100),thisPrefix=this._getPrefix(),otherPrefix=other._getPrefix(),thisPrefix&&otherPrefix&&thisPrefix!==otherPrefix&&(match-=100),match<0?match=0:100<match&&(match=100),match},equals:function equals(other){var _this;return!!(!other.locale||!this.locale||this.locale.equals(other.locale)||this.countryCode&&other.countryCode)&&(_this=this,PhoneNumber._fieldOrder.every(function(field){return _this[field]===other[field]}))},_doNormalize:function(options,norm,homeLocale,currentLocale,currentPlan,destinationLocale,destinationPlan,sync,loadParams){return!norm.invalid&&options&&options.assistedDialing?norm.subscriberNumber&&(!options.manualDialing||norm.iddPrefix||norm.countryCode||norm.trunkAccess)&&(currentLocale.getRegion()!==destinationLocale.getRegion()?(!norm._hasPrefix()&&options.defaultAreaCode&&destinationLocale.getRegion()===homeLocale.getRegion()&&(!destinationPlan.getFieldLength("minLocalLength")||norm.subscriberNumber.length>=destinationPlan.getFieldLength("minLocalLength"))&&(norm.areaCode=options.defaultAreaCode,!destinationPlan.getSkipTrunk())&&destinationPlan.getTrunkCode()&&(norm.trunkAccess=destinationPlan.getTrunkCode()),norm.trunkAccess&&destinationPlan.getSkipTrunk()&&delete norm.trunkAccess,options.sms?"US"===homeLocale.getRegion()&&"US"!==currentLocale.getRegion()?"US"!==destinationLocale.getRegion()?(norm.iddPrefix="011",norm.countryCode=norm.countryCode||homeLocale._mapRegiontoCC(destinationLocale.getRegion())):"cdma"===options.networkType?(delete norm.iddPrefix,delete norm.countryCode,norm.areaCode&&(norm.trunkAccess="1")):norm.areaCode&&(norm.iddPrefix="+",norm.countryCode="1",delete norm.trunkAccess):(norm.iddPrefix="cdma"===options.networkType?currentPlan.getIDDCode():"+",norm.countryCode=norm.countryCode||homeLocale._mapRegiontoCC(destinationLocale.region)):norm._hasPrefix()&&!norm.countryCode&&(norm.countryCode=homeLocale._mapRegiontoCC(destinationLocale.region)),norm.countryCode&&!options.sms&&(norm.iddPrefix="cdma"===options.networkType?currentPlan.getIDDCode():"+")):(options.defaultAreaCode&&("open"===destinationPlan.getPlanStyle()?!norm.trunkAccess&&norm._hasPrefix()&&destinationPlan.getTrunkCode()&&(norm.trunkAccess=destinationPlan.getTrunkCode()):norm._hasPrefix()?destinationPlan.getTrunkRequired()&&destinationPlan.getTrunkCode()&&(norm.trunkAccess=norm.trunkAccess||destinationPlan.getTrunkCode()):destinationLocale.getRegion()===homeLocale.getRegion()&&(norm.areaCode=options.defaultAreaCode,destinationPlan.getTrunkRequired())&&destinationPlan.getTrunkCode()&&(norm.trunkAccess=norm.trunkAccess||destinationPlan.getTrunkCode())),options.sms&&"US"===homeLocale.getRegion()&&"US"!==currentLocale.getRegion()?(norm.iddPrefix="011",destinationPlan.getSkipTrunk()&&norm.trunkAccess&&delete norm.trunkAccess):(norm.iddPrefix||norm.countryCode)&&(delete norm.iddPrefix,delete norm.countryCode,"open"===destinationPlan.getPlanStyle()||destinationPlan.getTrunkRequired())&&destinationPlan.getTrunkCode()&&(norm.trunkAccess=destinationPlan.getTrunkCode()))):norm.invalid||(!norm._hasPrefix()&&options&&options.defaultAreaCode&&destinationLocale.getRegion()===homeLocale.region&&(norm.areaCode=options.defaultAreaCode),!norm.countryCode&&norm._hasPrefix()&&(norm.countryCode=homeLocale._mapRegiontoCC(destinationLocale.getRegion())),norm.countryCode&&(options&&options.networkType&&"cdma"===options.networkType?norm.iddPrefix=currentPlan.getIDDCode():norm.iddPrefix="+",destinationPlan.getSkipTrunk()&&norm.trunkAccess?delete norm.trunkAccess:destinationPlan.getSkipTrunk()||norm.trunkAccess||!destinationPlan.getTrunkCode()||(norm.trunkAccess=destinationPlan.getTrunkCode()))),norm._join()},_doReparse:function(options,norm,homeLocale,currentLocale,currentPlan,destinationLocale,destinationPlan,sync,loadParams,callback){var formatted,tempRegion;options&&options.assistedDialing&&!norm.trunkAccess&&!norm.iddPrefix&&norm.subscriberNumber&&norm.subscriberNumber.length>destinationPlan.getFieldLength("maxLocalLength")?new PhoneNumber("+"+this._join(),{locale:this.locale,sync:sync,loadParms:loadParams,onLoad:ilib.bind(this,function(data){(tempRegion=data.countryCode&&data.locale._mapCCtoRegion(data.countryCode))&&"XX"!==tempRegion&&"SG"!==tempRegion&&(destinationLocale=(norm=data).destinationLocale,destinationPlan=data.destinationPlan),formatted=this._doNormalize(options,norm,homeLocale,currentLocale,currentPlan,destinationLocale,destinationPlan,sync,loadParams),"function"==typeof callback&&callback(formatted)})}):options&&options.assistedDialing&&norm.invalid&&currentLocale.region!==norm.locale.region?new PhoneNumber(this._join(),{locale:currentLocale,sync:sync,loadParms:loadParams,onLoad:ilib.bind(this,function(data){data&&!data.invalid&&(norm=data),formatted=this._doNormalize(options,norm,homeLocale,currentLocale,currentPlan,destinationLocale,destinationPlan,sync,loadParams),"function"==typeof callback&&callback(formatted)})}):(formatted=this._doNormalize(options,norm,homeLocale,currentLocale,currentPlan,destinationLocale,destinationPlan,sync,loadParams),"function"==typeof callback&&callback(formatted))},normalize:function(options){var norm,normalized,sync=!0,loadParams={};return options&&(void 0!==options.sync&&(sync=!!options.sync),options.loadParams)&&(loadParams=options.loadParams),norm=new PhoneNumber(this),!options||void 0===options.mcc&&void 0===options.country?this._doReparse(options,norm,this.locale,this.locale,this.plan,this.destinationLocale,this.destinationPlan,sync,loadParams,function(fmt){normalized=fmt,options&&"function"==typeof options.onLoad&&options.onLoad(fmt)}):new PhoneLocale({mcc:options.mcc,countryCode:options.countryCode,locale:this.locale,sync:sync,loadParams:loadParams,onLoad:ilib.bind(this,function(loc){new NumberingPlan({locale:loc,sync:sync,loadParms:loadParams,onLoad:ilib.bind(this,function(plan){this._doReparse(options,norm,this.locale,loc,plan,this.destinationLocale,this.destinationPlan,sync,loadParams,function(fmt){normalized=fmt,options&&"function"==typeof options.onLoad&&options.onLoad(fmt)})})})})}),normalized}},module.exports=PhoneNumber;