UNPKG

ilib

Version:

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

1 lines 3.57 kB
var ilib=require("./ilib.js");var Utils=require("./Utils.js");var JSUtils=require("./JSUtils.js");var Locale=require("./Locale.js");var PhoneNumber=require("./PhoneNumber.js");var NumberingPlan=require("./NumberingPlan.js");var PhoneLocale=require("./PhoneLocale.js");var PhoneFmt=function(e){this.sync=true;this.styleName="default",this.loadParams={};var t=new Locale;if(e){if(e.locale){t=e.locale}if(typeof e.sync!=="undefined"){this.sync=e.sync==true}if(e.loadParams){this.loadParams=e.loadParams}if(e.style){this.style=e.style}}new PhoneLocale({locale:t,mcc:e&&e.mcc,countryCode:e&&e.countryCode,onLoad:ilib.bind(this,function(t){this.locale=t;new NumberingPlan({locale:this.locale,sync:this.sync,loadParms:this.loadParams,onLoad:ilib.bind(this,function(t){this.plan=t;Utils.loadData({name:"phonefmt.json",object:PhoneFmt,locale:this.locale,sync:this.sync,loadParams:JSUtils.merge(this.loadParams,{returnOne:true}),callback:ilib.bind(this,function(t){this.fmtdata=t;if(e&&typeof e.onLoad==="function"){e.onLoad(this)}})})})})})})};PhoneFmt.prototype={_substituteDigits:function(e,t,i){var n,o="",r=0,a,l;if(!e){return o}if(typeof t==="object"){a=typeof t.template!=="undefined"?t.template:t;if(e.length>a.length){throw"part "+e+" is too big. We do not have a format template to format it."}n=a[e.length-1]}else{n=t}for(l=0;l<n.length;l++){if(n.charAt(l)==="X"){o+=e.charAt(r);r++}else{o+=n.charAt(l)}}if(i&&r<e.length-1){throw"too many digits in "+e+" for format "+n}return o},_getStyle:function(e,t){return t[e]||t["default"]},_doFormat:function(e,t,i,n,o,r){var a=true,l={},f,s,d,u,c,h,m="",y,b;if(t){if(typeof t.sync!=="undefined"){a=t.sync==true}if(t.loadParams){l=t.loadParams}}h=this.style;if(e.countryCode){h=e.mobilePrefix?"internationalmobile":"international"}else if(e.mobilePrefix!==undefined){h="mobile"}else if(e.serviceCode!==undefined&&typeof o["service"]!=="undefined"){h="service"}c=!t||!t.partial;y=this._getStyle(h,o);y=(c?y.whole:y.partial)||y;for(var P=i;P<PhoneNumber._fieldOrder.length;P++){d=PhoneNumber._fieldOrder[P];if(e[d]!==undefined){if(y[d]!==undefined){s=y[d];if(d==="trunkAccess"){if(e.areaCode===undefined&&e.serviceCode===undefined&&e.mobilePrefix===undefined){s="X"}}if(b&&typeof y[b].suffix!=="undefined"){if(d!=="extension"&&e[d].search(/[xwtp,;]/i)<=-1){m+=y[b].suffix}}b=d;f=this._substituteDigits(e[d],s,d==="subscriberNumber");m+=f;if(d==="countryCode"){u=e.countryCode.replace(/[wWpPtT\+#\*]/g,"");new PhoneLocale({locale:this.locale,sync:a,loadParms:l,countryCode:u,onLoad:ilib.bind(this,function(i){Utils.loadData({name:"phonefmt.json",object:PhoneFmt,locale:i,sync:a,loadParams:JSUtils.merge(l,{returnOne:true}),callback:ilib.bind(this,function(n){var o="";this._doFormat(e,t,P+1,i,n,function(e){o=e;if(typeof r==="function"){r(m+e)}});m+=o})})})});return m}}else{m+=e[d]}}}if(typeof r==="function"){r(m)}return m},format:function(e,t){var i="",n;n=t&&t.onLoad;try{this._doFormat(e,t,0,this.locale,this.fmtdata,function(e){i=e;if(typeof n==="function"){n(e)}})}catch(o){if(typeof o==="string"){i="";for(var r in PhoneNumber._fieldOrder){if(typeof r==="string"&&typeof PhoneNumber._fieldOrder[r]==="string"&&e[PhoneNumber._fieldOrder[r]]!==undefined){i+=e[PhoneNumber._fieldOrder[r]];if(PhoneNumber._fieldOrder[r]==="countryCode"){i+=" "}}}}else{throw o}if(typeof n==="function"){n(i)}}return i},getAvailableStyles:function(){var e=[],t;if(this.fmtdata){for(t in this.fmtdata){if(this.fmtdata[t].example){e.push(t)}}}return e},getStyleExample:function(e){return this.fmtdata[e].example||undefined}};module.exports=PhoneFmt;