UNPKG

ilib

Version:

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

1 lines 12.9 kB
var ilib=require("./ilib.js");var Utils=require("./Utils.js");var JSUtils=require("./JSUtils.js");var Locale=require("./Locale.js");var NumberingPlan=require("./NumberingPlan.js");var PhoneLocale=require("./PhoneLocale.js");var PhoneHandlerFactory=require("./PhoneHandlerFactory.js");var PhoneNumber=function(e,i){var n,t;this.sync=true;this.loadParams={};if(!e||typeof e==="string"&&e.length===0){return this}if(i){if(typeof i.sync==="boolean"){this.sync=i.sync}if(i.loadParams){this.loadParams=i.loadParams}if(typeof i.onLoad==="function"){this.onLoad=i.onLoad}}if(typeof e==="object"){this.vsc=e.vsc;this.iddPrefix=e.iddPrefix;this.countryCode=e.countryCode;this.trunkAccess=e.trunkAccess;this.cic=e.cic;this.emergency=e.emergency;this.mobilePrefix=e.mobilePrefix;this.serviceCode=e.serviceCode;this.areaCode=e.areaCode;this.subscriberNumber=e.subscriberNumber;this.extension=e.extension;this.invalid=e.invalid;if(e.plan&&e.locale){this.plan=e.plan;this.locale=e.locale;this.destinationPlan=e.destinationPlan;this.destinationLocale=e.destinationLocale;if(i&&typeof i.onLoad==="function"){i.onLoad(this)}return}}new PhoneLocale({locale:i&&i.locale,mcc:i&&i.mcc,sync:this.sync,loadParams:this.loadParams,onLoad:ilib.bind(this,function(o){this.locale=this.destinationLocale=o;new NumberingPlan({locale:this.locale,sync:this.sync,loadParms:this.loadParams,onLoad:ilib.bind(this,function(o){this.plan=this.destinationPlan=o;if(typeof e==="object"){return}Utils.loadData({name:"states.json",object:PhoneNumber,locale:this.locale,sync:this.sync,loadParams:JSUtils.merge(this.loadParams,{returnOne:true}),callback:ilib.bind(this,function(r){if(!r){r=PhoneNumber._defaultStates}n=r;t={stateData:n,plan:o,handler:PhoneHandlerFactory(this.locale,o)};e="^"+e.replace(/\^/g,"");e=PhoneNumber._stripFormatting(e);this._parseNumber(e,t,i)})})})})})})};PhoneNumber.parseImsi=function(e,i){var n=true,t={},o={};if(!e){return undefined}if(i){if(typeof i.sync!=="undefined"){n=i.sync==true}if(i.loadParams){t=i.loadParams}}if(ilib.data.mnc){o=PhoneNumber._parseImsi(ilib.data.mnc,e);if(i&&typeof i.onLoad==="function"){i.onLoad(o)}}else{Utils.loadData({name:"mnc.json",object:PhoneNumber,nonlocale:true,sync:n,loadParams:t,callback:ilib.bind(this,function(n){ilib.data.mnc=n;o=PhoneNumber._parseImsi(n,e);if(i&&typeof i.onLoad==="function"){i.onLoad(o)}})})}return o};PhoneNumber._parseImsi=function(e,i){var n,t,o,r,s,a=0,d,c={},l,u=0;o=e;if(!o){return undefined}t=0;while(t<i.length){n=PhoneNumber._getCharacterCode(i.charAt(t));if(n>=0){d=o.s&&o.s[n];if(typeof d==="object"){if(typeof d.l!=="undefined"){l=d;u=t}o=d;t++}else{if((typeof d==="undefined"||d===0||typeof d==="object"&&typeof d.l==="undefined")&&l){d=l;t=u}if(typeof d==="number"&&d||typeof d==="object"&&typeof d.l!=="undefined"){var f=typeof d==="number"?d:d.l;s=PhoneNumber._states[f];if(s==="area"){r=t+1}else{r=6}c.mcc=i.substring(0,3);c.mnc=i.substring(3,r);c.msin=i.substring(r);return c}else{if(i.length>=6){c.mcc=i.substring(0,3);c.mnc=i.substring(3,6);c.msin=i.substring(6)}return c}}}else if(n===-1){t++}else{t++}}if(t>=i.length&&i.length>=6){c.mcc=i.substring(0,3);c.mnc=i.substring(3,6);c.msin=i.substring(6)}else{c=undefined}return c};PhoneNumber._stripFormatting=function(e){var i="";var n;for(n=0;n<e.length;n++){if(PhoneNumber._getCharacterCode(e.charAt(n))>=-1){i+=e.charAt(n)}}return i};PhoneNumber._getCharacterCode=function(e){if(e>="0"&&e<="9"){return e-"0"}switch(e){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(e,i,n,t){new PhoneLocale({locale:this.locale,countryCode:t,sync:this.sync,loadParms:this.loadParams,onLoad:ilib.bind(this,function(i){this.destinationLocale=i;Utils.loadData({name:"states.json",object:PhoneNumber,locale:this.destinationLocale,sync:this.sync,loadParams:JSUtils.merge(this.loadParams,{returnOne:true}),callback:ilib.bind(this,function(i){if(!i){i=PhoneNumber._defaultStates}new NumberingPlan({locale:this.destinationLocale,sync:this.sync,loadParms:this.loadParams,onLoad:ilib.bind(this,function(t){this.destinationPlan=t;var o={stateData:i,plan:t,handler:PhoneHandlerFactory(this.destinationLocale,t)};if(!t.getSkipTrunk()){e="^"+e}this._parseNumber(e,o,n)})})})})})})},_parseNumber:function(e,i,n){var t,o,r,s,a,d,c,l=i.stateData,u=undefined,f=0;r=i;a=14;t=0;while(t<e.length){o=PhoneNumber._getCharacterCode(e.charAt(t));if(o>=0){s=l.s&&l.s[o];if(!s&&l.s&&l.s[a]){s=l.s[a]}if(typeof s==="object"&&t+1<e.length){if(typeof s.l!=="undefined"){u=s;f=t}l=s;t++}else{if((typeof s==="undefined"||s===0||typeof s==="object"&&typeof s.l==="undefined")&&u){s=u;t=f;f=0;u=undefined}if(typeof s==="number"&&s||typeof s==="object"&&typeof s.l!=="undefined"){var h=typeof s==="number"?s:s.l;d=PhoneNumber._states[h];if(e.charAt(0)==="^"){c=r.handler[d](e.slice(1),t-1,this,r)}else{c=r.handler[d](e,t,this,r)}e=c.number;t=f=0;u=undefined;l=r.stateData;if(c.countryCode!==undefined){this._parseOtherCountry(e,i,n,c.countryCode);return}else if(c.table!==undefined){Utils.loadData({name:c.table+".json",object:PhoneNumber,nonlocale:true,sync:this.sync,loadParams:this.loadParams,callback:ilib.bind(this,function(i){if(!i){i=PhoneNumber._defaultStates}r={stateData:i,plan:r.plan,handler:r.handler};this._parseNumber(e,r,n)})});return}else if(c.skipTrunk!==undefined){o=PhoneNumber._getCharacterCode(r.plan.getTrunkCode());l=l.s&&l.s[o]}}else{d=typeof s==="number"?"none":"local";if(e.charAt(0)==="^"){c=r.handler[d](e.slice(1),t-1,this,r)}else{c=r.handler[d](e,t,this,r)}break}}}else if(o===-1){t++}else{t++}}if(t>=e.length&&l!==i.stateData){d=typeof l.l==="undefined"||l===0?"none":"local";if(e.charAt(0)==="^"){c=r.handler[d](e.slice(1),t-1,this,r)}else{c=r.handler[d](e,t,this,r)}}if(this.onLoad){this.onLoad(this)}},_getPrefix:function(){return this.areaCode||this.serviceCode||this.mobilePrefix||""},_hasPrefix:function(){return this._getPrefix()!==""},_xor:function(e,i){if(e===undefined&&i===undefined||e!==undefined&&i!==undefined){return false}else{return true}},_join:function(){var e,i="";try{for(var n in PhoneNumber._fieldOrder){if(typeof n==="string"&&typeof PhoneNumber._fieldOrder[n]==="string"){e=PhoneNumber._fieldOrder[n];if(this[e]!==undefined){i+=this[e]}}}}catch(t){throw t}return i},compare:function(e){var i=100,n={590:1,594:1,596:1,262:1},t={378:1,379:1},o,r,s=0;if(typeof this.locale.region==="string"){s=this.locale._mapRegiontoCC(this.locale.region)}if(!this.subscriberNumber||!e.subscriberNumber||this.subscriberNumber!==e.subscriberNumber){return 0}if(this._xor(this.extension,e.extension)||this.extension!==e.extension){return 0}if(this._xor(this.countryCode,e.countryCode)){switch(this.locale.getRegion()){case"FR":if(this.countryCode in n||e.countryCode in n){if(this.areaCode!==e.areaCode||this.mobilePrefix!==e.mobilePrefix){i-=100}}else{i-=16}break;case"IT":if(this.countryCode in t||e.countryCode in t){if(this.areaCode!==e.areaCode){i-=100}}else{i-=16}break;default:i-=16;if(this.countryCode!==undefined&&this.countryCode!==s||e.countryCode!==undefined&&e.countryCode!==s){i-=16}}}else if(this.countryCode!==e.countryCode){if(e.countryCode==="33"||this.countryCode==="33"){if(this.countryCode in n||e.countryCode in n){if(this.areaCode!==e.areaCode||this.mobilePrefix!==e.mobilePrefix){i-=100}}else{i-=100}}else if(this.countryCode==="39"||e.countryCode==="39"){if(this.countryCode in t||e.countryCode in t){if(this.areaCode!==e.areaCode){i-=100}}else{i-=100}}else{i-=100}}if(this._xor(this.serviceCode,e.serviceCode)){i-=20}else if(this.serviceCode!==e.serviceCode){i-=100}if(this._xor(this.mobilePrefix,e.mobilePrefix)){i-=20}else if(this.mobilePrefix!==e.mobilePrefix){i-=100}if(this._xor(this.areaCode,e.areaCode)){i-=12}else if(this.areaCode!==e.areaCode){i-=100}o=this._getPrefix();r=e._getPrefix();if(o&&r&&o!==r){i-=100}if(i<0){i=0}else if(i>100){i=100}return i},equals:function e(i){if(i.locale&&this.locale&&!this.locale.equals(i.locale)&&(!this.countryCode||!i.countryCode)){return false}for(var n in i){if(n!==undefined&&this[n]!==undefined&&typeof this[n]!=="object"){if(i[n]===undefined){return false}if(this[n]!==i[n]){return false}}}for(var n in i){if(n!==undefined&&i[n]!==undefined&&typeof i[n]!=="object"){if(this[n]===undefined){return false}if(this[n]!==i[n]){return false}}}return true},_doNormalize:function(e,i,n,t,o,r,s,a,d){var c="";if(!i.invalid&&e&&e.assistedDialing){if(i.subscriberNumber&&(!e.manualDialing||i.iddPrefix||i.countryCode||i.trunkAccess)){if(t.getRegion()!==r.getRegion()){if(!i._hasPrefix()&&e.defaultAreaCode&&r.getRegion()===n.getRegion()&&(!s.getFieldLength("minLocalLength")||i.subscriberNumber.length>=s.getFieldLength("minLocalLength"))){i.areaCode=e.defaultAreaCode;if(!s.getSkipTrunk()&&s.getTrunkCode()){i.trunkAccess=s.getTrunkCode()}}if(i.trunkAccess&&s.getSkipTrunk()){delete i.trunkAccess}if(e.sms){if(n.getRegion()==="US"&&t.getRegion()!=="US"){if(r.getRegion()!=="US"){i.iddPrefix="011";i.countryCode=i.countryCode||n._mapRegiontoCC(r.getRegion())}else if(e.networkType==="cdma"){delete i.iddPrefix;delete i.countryCode;if(i.areaCode){i.trunkAccess="1"}}else if(i.areaCode){i.iddPrefix="+";i.countryCode="1";delete i.trunkAccess}}else{i.iddPrefix=e.networkType==="cdma"?o.getIDDCode():"+";i.countryCode=i.countryCode||n._mapRegiontoCC(r.region)}}else if(i._hasPrefix()&&!i.countryCode){i.countryCode=n._mapRegiontoCC(r.region)}if(i.countryCode&&!e.sms){i.iddPrefix=e.networkType==="cdma"?o.getIDDCode():"+"}}else{if(e.defaultAreaCode){if(s.getPlanStyle()==="open"){if(!i.trunkAccess&&i._hasPrefix()&&s.getTrunkCode()){i.trunkAccess=s.getTrunkCode()}}else{if(!i._hasPrefix()){if(r.getRegion()===n.getRegion()){i.areaCode=e.defaultAreaCode;if(s.getTrunkRequired()&&s.getTrunkCode()){i.trunkAccess=i.trunkAccess||s.getTrunkCode()}}}else{if(s.getTrunkRequired()&&s.getTrunkCode()){i.trunkAccess=i.trunkAccess||s.getTrunkCode()}}}}if(e.sms&&n.getRegion()==="US"&&t.getRegion()!=="US"){i.iddPrefix="011";if(s.getSkipTrunk()&&i.trunkAccess){delete i.trunkAccess}}else if(i.iddPrefix||i.countryCode){delete i.iddPrefix;delete i.countryCode;if((s.getPlanStyle()==="open"||s.getTrunkRequired())&&s.getTrunkCode()){i.trunkAccess=s.getTrunkCode()}}}}}else if(!i.invalid){if(!i._hasPrefix()&&e&&e.defaultAreaCode&&r.getRegion()===n.region){i.areaCode=e.defaultAreaCode}if(!i.countryCode&&i._hasPrefix()){i.countryCode=n._mapRegiontoCC(r.getRegion())}if(i.countryCode){if(e&&e.networkType&&e.networkType==="cdma"){i.iddPrefix=o.getIDDCode()}else{i.iddPrefix="+"}if(s.getSkipTrunk()&&i.trunkAccess){delete i.trunkAccess}else if(!s.getSkipTrunk()&&!i.trunkAccess&&s.getTrunkCode()){i.trunkAccess=s.getTrunkCode()}}}c=i._join();return c},_doReparse:function(e,i,n,t,o,r,s,a,d,c){var l,u;if(e&&e.assistedDialing&&!i.trunkAccess&&!i.iddPrefix&&i.subscriberNumber&&i.subscriberNumber.length>s.getFieldLength("maxLocalLength")){new PhoneNumber("+"+this._join(),{locale:this.locale,sync:a,loadParms:d,onLoad:ilib.bind(this,function(f){u=f.countryCode&&f.locale._mapCCtoRegion(f.countryCode);if(u&&u!=="unknown"&&u!=="SG"){i=f;r=f.destinationLocale;s=f.destinationPlan}l=this._doNormalize(e,i,n,t,o,r,s,a,d);if(typeof c==="function"){c(l)}})})}else if(e&&e.assistedDialing&&i.invalid&&t.region!==i.locale.region){new PhoneNumber(this._join(),{locale:t,sync:a,loadParms:d,onLoad:ilib.bind(this,function(u){if(u&&!u.invalid){i=u}l=this._doNormalize(e,i,n,t,o,r,s,a,d);if(typeof c==="function"){c(l)}})})}else{l=this._doNormalize(e,i,n,t,o,r,s,a,d);if(typeof c==="function"){c(l)}}},normalize:function(e){var i,n=true,t={};if(e){if(typeof e.sync!=="undefined"){n=e.sync==true}if(e.loadParams){t=e.loadParams}}i=new PhoneNumber(this);var o;if(e&&(typeof e.mcc!=="undefined"||typeof e.country!=="undefined")){new PhoneLocale({mcc:e.mcc,countryCode:e.countryCode,locale:this.locale,sync:n,loadParams:t,onLoad:ilib.bind(this,function(r){new NumberingPlan({locale:r,sync:n,loadParms:t,onLoad:ilib.bind(this,function(s){this._doReparse(e,i,this.locale,r,s,this.destinationLocale,this.destinationPlan,n,t,function(i){o=i;if(e&&typeof e.onLoad==="function"){e.onLoad(i)}})})})})})}else{this._doReparse(e,i,this.locale,this.locale,this.plan,this.destinationLocale,this.destinationPlan,n,t,function(i){o=i;if(e&&typeof e.onLoad==="function"){e.onLoad(i)}})}return o}};module.exports=PhoneNumber;