UNPKG

ilib

Version:

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

2 lines 4.8 kB
// !data address addressres regionnames var ilib=require("../index.js"),Utils=require("./Utils.js"),JSUtils=require("./JSUtils.js"),Locale=require("./Locale.js"),Address=require("./Address.js"),IString=require("./IString.js"),ResBundle=require("./ResBundle.js"),defaultData={formats:{default:"{streetAddress}\n{locality} {region} {postalCode}\n{country}",nocountry:"{streetAddress}\n{locality} {region} {postalCode}"},startAt:"end",fields:[{name:"postalCode",line:"startAtLast",pattern:"[0-9]+",matchGroup:0},{name:"region",line:"last",pattern:"([A-zÀÁÈÉÌÍÑÒÓÙÚÜàáèéìíñòóùúü\\.\\-\\']+\\s*){1,2}$",matchGroup:0},{name:"locality",line:"last",pattern:"([A-zÀÁÈÉÌÍÑÒÓÙÚÜàáèéìíñòóùúü\\.\\-\\']+\\s*){1,2}$",matchGroup:0}],fieldNames:{streetAddress:"Street Address",locality:"City",postalCode:"Zip Code",region:"State",country:"Country"}},AddressFmt=function(options){this.sync=!0,this.styleName="default",this.loadParams={},this.locale=new Locale(),options&&(options.locale&&(this.locale="string"==typeof options.locale?new Locale(options.locale):options.locale),void 0!==options.sync&&(this.sync=!!options.sync),options.style&&(this.styleName=options.style),options.loadParams)&&(this.loadParams=options.loadParams),Utils.loadData({name:"address.json",object:"AddressFmt",locale:this.locale,sync:this.sync,loadParams:this.loadParams,callback:ilib.bind(this,function(info){!info||JSUtils.isEmpty(info)?Utils.loadData({name:"address.json",object:"AddressFmt",locale:new Locale("XX"),sync:this.sync,loadParams:this.loadParams,callback:ilib.bind(this,function(info){this.info=info,this._init(),options&&"function"==typeof options.onLoad&&options.onLoad(this)})}):(this.info=info,this._init(),options&&"function"==typeof options.onLoad&&options.onLoad(this))})})};function isAsianLocale(locale){return"zh"===locale.language||"ja"===locale.language||"ko"===locale.language}function invertAndFilter(object){var p,ret=[],regions=["AQ","EU","EZ","UN","ZZ"];for(p in object)p&&!object[p].match(/\d/)&&-1===regions.indexOf(object[p])&&ret.push({code:object[p],name:p});return ret}AddressFmt.prototype._init=function(){this.info||(this.info=defaultData),this.style=this.info.formats&&this.info.formats[this.styleName],this.style=this.style||this.info.formats&&this.info.formats.default||defaultData.formats.default,this.info.fieldNames||(this.info.fieldNames=defaultData.fieldNames)},AddressFmt.prototype.format=function(address){var format;return address?address.countryCode&&address.countryCode!==this.locale.region&&Locale._isRegionCode(this.locale.region)&&"XX"!==this.locale.region?new AddressFmt({locale:new Locale(address.countryCode),style:this.styleName}).format(address):(format="object"==typeof this.style?this.style[address.format||"latin"]:this.style,address={country:address.country||"",region:address.region||"",locality:address.locality||"",streetAddress:address.streetAddress||"",postalCode:address.postalCode||"",postOffice:address.postOffice||""},new IString(format).format(address).replace(/[ \t]+/g," ").replace("\n ","\n").replace(" \n","\n").replace(/\n+/g,"\n").trim()):""},AddressFmt.prototype.getFormatInfo=function(locale,sync,callback){var info,loc=new Locale(this.locale);return locale&&("string"==typeof locale&&(locale=new Locale(locale)),loc.language=locale.getLanguage(),loc.spec=void 0),Utils.loadData({name:"regionnames.json",object:"AddressFmt",locale:loc,sync:this.sync,loadParams:JSUtils.merge(this.loadParams,{returnOne:!0},!0),callback:ilib.bind(this,function(regions){this.regions=regions,new ResBundle({locale:loc,name:"addressres",sync:this.sync,loadParams:this.loadParams,onLoad:ilib.bind(this,function(rb){var type,format,fields=this.info.fields||defaultData.fields;this.info.multiformat&&(type=isAsianLocale(this.locale)?"asian":"latin",fields=this.info.fields[type]),format="object"==typeof this.style?this.style[type||"latin"]:this.style,new Address(" ",{locale:loc,sync:this.sync,loadParams:this.loadParams,onLoad:ilib.bind(this,function(localeAddress){var rows=format.split(/\n/g);info=rows.map(ilib.bind(this,function(row){return row.split("}").filter(function(component){return 0<component.length}).map(ilib.bind(this,function(component){var name=component.replace(/.*{/,""),component={component:name,label:rb.getStringJS(this.info.fieldNames[name])},field=fields.filter(function(f){return f.name===name});return field&&field[0]&&field[0].pattern&&"string"==typeof field[0].pattern&&(component.constraint=field[0].pattern),"country"===name?component.constraint=invertAndFilter(localeAddress.ctrynames):"region"===name&&this.regions[loc.getRegion()]&&(component.constraint=this.regions[loc.getRegion()]),component}))})),callback&&"function"==typeof callback&&callback(info)})})})})})}),info},module.exports=AddressFmt;