Module 698982

2,597 characters · in no known chunk
Dependencies
Required by (1)
Lazily required by (0)none
Requires (0)none
Lazily requires (0)none
Source Raw
function(e,t){"use strict";function n(e,t,n){this.locales=e,this.formats=t,this.pluralFn=n}function i(e){this.id=e}function r(e,t,n,i,r){this.id=e,this.useOrdinal=t,this.offset=n,this.options=i,this.pluralFn=r}function s(e,t,n,i){this.id=e,this.offset=t,this.numberFormat=n,this.string=i}function a(e,t){this.id=e,this.options=t}t.default=n,n.prototype.compile=function(e){return this.pluralStack=[],this.currentPlural=null,this.pluralNumberFormat=null,this.compileMessage(e)},n.prototype.compileMessage=function(e){if(!(e&&"messageFormatPattern"===e.type))throw Error('Message AST is not of type: "messageFormatPattern"');var t,n,i,r=e.elements,s=[];for(t=0,n=r.length;t<n;t+=1)switch((i=r[t]).type){case"messageTextElement":s.push(this.compileMessageText(i));break;case"argumentElement":s.push(this.compileArgument(i));break;default:throw Error("Message element does not have a valid type")}return s},n.prototype.compileMessageText=function(e){return this.currentPlural&&/(^|[^\\])#/g.test(e.value)?(this.pluralNumberFormat||(this.pluralNumberFormat=new Intl.NumberFormat(this.locales)),new s(this.currentPlural.id,this.currentPlural.format.offset,this.pluralNumberFormat,e.value)):e.value.replace(/\\#/g,"#")},n.prototype.compileArgument=function(e){var t=e.format;if(!t)return new i(e.id);var n,s=this.formats,o=this.locales,l=this.pluralFn;switch(t.type){case"numberFormat":return n=s.number[t.style],{id:e.id,format:new Intl.NumberFormat(o,n).format};case"dateFormat":return n=s.date[t.style],{id:e.id,format:new Intl.DateTimeFormat(o,n).format};case"timeFormat":return n=s.time[t.style],{id:e.id,format:new Intl.DateTimeFormat(o,n).format};case"pluralFormat":return n=this.compileOptions(e),new r(e.id,t.ordinal,t.offset,n,l);case"selectFormat":return n=this.compileOptions(e),new a(e.id,n);default:throw Error("Message element does not have a valid format type")}},n.prototype.compileOptions=function(e){var t,n,i,r=e.format,s=r.options,a={};for(this.pluralStack.push(this.currentPlural),this.currentPlural="pluralFormat"===r.type?e:null,t=0,n=s.length;t<n;t+=1)a[(i=s[t]).selector]=this.compileMessage(i.value);return this.currentPlural=this.pluralStack.pop(),a},i.prototype.format=function(e){return e?"string"==typeof e?e:String(e):""},r.prototype.getOption=function(e){var t=this.options;return t["="+e]||t[this.pluralFn(e-this.offset,this.useOrdinal)]||t.other},s.prototype.format=function(e){var t=this.numberFormat.format(e-this.offset);return this.string.replace(/(^|[^\\])#/g,"$1"+t).replace(/\\#/g,"#")},a.prototype.getOption=function(e){var t=this.options;return t[e]||t.other}}