-
Notifications
You must be signed in to change notification settings - Fork 8
/
fluent-0.13.0.min.js
7 lines (7 loc) · 9.34 KB
/
fluent-0.13.0.min.js
1
2
3
4
5
6
7
/**
* Minified by jsDelivr using Terser v5.3.5.
* Original file: /npm/fluent@0.13.0/fluent.js
*
* Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define("fluent",["exports"],t):t((e=e||self).Fluent={})}(this,(function(e){"use strict";class t{constructor(e,t){this.value=e,this.opts=t}valueOf(){return this.value}toString(){throw new Error("Subclasses of FluentType must implement toString.")}}class n extends t{valueOf(){return null}toString(){return`{${this.value||"???"}}`}}class r extends t{constructor(e,t){super(parseFloat(e),t)}toString(e){try{return e._memoizeIntlObject(Intl.NumberFormat,this.opts).format(this.value)}catch(e){return this.value}}}class s extends t{constructor(e,t){super(new Date(e),t)}toString(e){try{return e._memoizeIntlObject(Intl.DateTimeFormat,this.opts).format(this.value)}catch(e){return this.value}}}function u(e,t){return Object.assign({},e,function(e){const t={};for(const[n,r]of Object.entries(e))t[n]=r.valueOf();return t}(t))}var o=Object.freeze({NUMBER:function([e],t){return e instanceof n?e:e instanceof r?new r(e.valueOf(),u(e.opts,t)):new n("NUMBER()")},DATETIME:function([e],t){return e instanceof n?e:e instanceof s?new s(e.valueOf(),u(e.opts,t)):new n("DATETIME()")}});const i=2500;function a(e,t,n){if(n===t)return!0;if(n instanceof r&&t instanceof r&&n.value===t.value)return!0;if(t instanceof r&&"string"==typeof n){if(n===e._memoizeIntlObject(Intl.PluralRules,t.opts).select(t.value))return!0}return!1}function l(e,t,r){return t[r]?f(e,t[r]):(e.errors.push(new RangeError("No default")),new n)}function c(e,t){const n=[],r={};for(const s of t)"narg"===s.type?r[s.name]=f(e,s.value):n.push(f(e,s));return[n,r]}function f(e,u){if("string"==typeof u)return e.bundle._transform(u);if(u instanceof n)return u;if(Array.isArray(u))return function(e,t){if(e.dirty.has(t))return e.errors.push(new RangeError("Cyclic reference")),new n;e.dirty.add(t);const r=[],s=e.bundle._useIsolating&&t.length>1;for(const n of t){if("string"==typeof n){r.push(e.bundle._transform(n));continue}const t=f(e,n).toString(e.bundle);s&&r.push(""),t.length>i?(e.errors.push(new RangeError(`Too many characters in placeable (${t.length}, max allowed is 2500)`)),r.push(t.slice(i))):r.push(t),s&&r.push("")}return e.dirty.delete(t),r.join("")}(e,u);switch(u.type){case"str":return u.value;case"num":return new r(u.value,{minimumFractionDigits:u.precision});case"var":return function(e,{name:u}){if(!e.args||!e.args.hasOwnProperty(u))return!1===e.insideTermReference&&e.errors.push(new ReferenceError("Unknown variable: "+u)),new n("$"+u);const o=e.args[u];if(o instanceof t)return o;switch(typeof o){case"string":return o;case"number":return new r(o);case"object":if(o instanceof Date)return new s(o);default:return e.errors.push(new TypeError(`Unsupported variable type: ${u}, ${typeof o}`)),new n("$"+u)}}(e,u);case"mesg":return function(e,{name:t,attr:r}){const s=e.bundle._messages.get(t);if(!s){const r=new ReferenceError("Unknown message: "+t);return e.errors.push(r),new n(t)}if(r){const u=s.attrs&&s.attrs[r];return u?f(e,u):(e.errors.push(new ReferenceError("Unknown attribute: "+r)),new n(`${t}.${r}`))}return f(e,s)}(e,u);case"term":return function(e,{name:t,attr:r,args:s}){const u="-"+t,o=e.bundle._terms.get(u);if(!o){const t=new ReferenceError("Unknown term: "+u);return e.errors.push(t),new n(u)}const[,i]=c(e,s),a={...e,args:i,insideTermReference:!0};if(r){const t=o.attrs&&o.attrs[r];return t?f(a,t):(e.errors.push(new ReferenceError("Unknown attribute: "+r)),new n(`${u}.${r}`))}return f(a,o)}(e,u);case"func":return function(e,{name:t,args:r}){const s=e.bundle._functions[t]||o[t];if(!s)return e.errors.push(new ReferenceError(`Unknown function: ${t}()`)),new n(t+"()");if("function"!=typeof s)return e.errors.push(new TypeError(`Function ${t}() is not callable`)),new n(t+"()");try{return s(...c(e,r))}catch(e){return new n(t+"()")}}(e,u);case"select":return function(e,{selector:t,variants:r,star:s}){let u=f(e,t);if(u instanceof n){return f(e,l(e,r,s))}for(const t of r){const n=f(e,t.key);if(a(e.bundle,u,n))return f(e,t)}const o=l(e,r,s);return f(e,o)}(e,u);case void 0:return null!==u.value&&void 0!==u.value?f(e,u.value):(e.errors.push(new RangeError("No value")),new n);default:return new n}}class h extends Error{}const p=/^(-?[a-zA-Z][\w-]*) *= */gm,w=/\.([a-zA-Z][\w-]*) *= */y,m=/\*?\[/y,g=/(-?[0-9]+(?:\.([0-9]+))?)/y,d=/([a-zA-Z][\w-]*)/y,y=/([$-])?([a-zA-Z][\w-]*)(?:\.([a-zA-Z][\w-]*))?/y,v=/^[A-Z][A-Z0-9_-]*$/,b=/([^{}\n\r]+)/y,_=/([^\\"\n\r]*)/y,x=/\\([\\"])/y,E=/\\u([a-fA-F0-9]{4})|\\U([a-fA-F0-9]{6})/y,I=/^\n+/,R=/ +$/,$=/ *\r?\n/g,A=/( *)$/,F=/{\s*/y,O=/\s*}/y,S=/\[\s*/y,T=/\s*] */y,U=/\s*\(\s*/y,k=/\s*->\s*/y,M=/\s*:\s*/y,j=/\s*,?\s*/y,z=/\s+/y;class D extends Map{static fromString(e){p.lastIndex=0;let t=new this,n=0;for(;;){let r=p.exec(e);if(null===r)break;n=p.lastIndex;try{t.set(r[1],a())}catch(e){if(e instanceof h)continue;throw e}}return t;function r(t){return t.lastIndex=n,t.test(e)}function s(t,r){if(e[n]===t)return n++,!0;if(r)throw new r("Expected "+t);return!1}function u(e,t){if(r(e))return n=e.lastIndex,!0;if(t)throw new t("Expected "+e.toString());return!1}function o(t){t.lastIndex=n;let r=t.exec(e);if(null===r)throw new h("Expected "+t.toString());return n=t.lastIndex,r}function i(e){return o(e)[1]}function a(){let e=l(),t=function(){let e={};for(;r(w);){let t=i(w),n=l();if(null===n)throw new h("Expected attribute value");e[t]=n}return Object.keys(e).length>0?e:null}();if(null===t){if(null===e)throw new h("Expected message value or attributes");return e}return{value:e,attrs:t}}function l(){if(r(b))var t=i(b);if("{"===e[n]||"}"===e[n])return c(t?[t]:[],1/0);let s=C();return s?t?c([t,s],s.length):(s.value=q(s.value,I),c([s],s.length)):t?q(t,R):null}function c(t=[],s){let u=0;for(;;){if(r(b)){t.push(i(b));continue}if("{"===e[n]){if(++u>100)throw new h("Too many placeables");t.push(f());continue}if("}"===e[n])throw new h("Unbalanced closing brace");let o=C();if(!o)break;t.push(o),s=Math.min(s,o.length)}let o=t.length-1;"string"==typeof t[o]&&(t[o]=q(t[o],R));let a=[];for(let e of t)"indent"===e.type?e=e.value.slice(0,e.value.length-s):"str"===e.type&&(e=e.value),e&&a.push(e);return a}function f(){u(F,h);let e=D();if(u(O))return e;if(u(k)){let t=function(){let e,t=[],n=0;for(;r(m);){s("*")&&(e=n);let r=Z(),u=l();if(null===u)throw new h("Expected variant value");t[n++]={key:r,value:u}}if(0===n)return null;if(void 0===e)throw new h("Expected default variant");return{variants:t,star:e}}();return u(O,h),{type:"select",selector:e,...t}}throw new h("Unclosed placeable")}function D(){if("{"===e[n])return f();if(r(y)){let[,t,r,s=null]=o(y);if("$"===t)return{type:"var",name:r};if(u(U)){let o=function(){let t=[];for(;;){switch(e[n]){case")":return n++,t;case void 0:throw new h("Unclosed argument list")}t.push(N()),u(j)}}();if("-"===t)return{type:"term",name:r,attr:s,args:o};if(v.test(r))return{type:"func",name:r,args:o};throw new h("Function names must be all upper-case")}return"-"===t?{type:"term",name:r,attr:s,args:[]}:{type:"mesg",name:r,attr:s}}return P()}function N(){let e=D();return"mesg"!==e.type?e:u(M)?{type:"narg",name:e.name,value:P()}:e}function Z(){u(S,h);let e=r(g)?B():i(d);return u(T,h),e}function P(){if(r(g))return B();if('"'===e[n])return function(){s('"',h);let t="";for(;;){if(t+=i(_),"\\"!==e[n]){if(s('"'))return{type:"str",value:t};throw new h("Unclosed string literal")}t+=W()}}();throw new h("Invalid expression")}function B(){let[,e,t=""]=o(g),n=t.length;return{type:"num",value:parseFloat(e),precision:n}}function W(){if(r(x))return i(x);if(r(E)){let[,e,t]=o(E),n=parseInt(e||t,16);return n<=55295||57344<=n?String.fromCodePoint(n):"�"}throw new h("Unknown escape sequence")}function C(){let t=n;switch(u(z),e[n]){case".":case"[":case"*":case"}":case void 0:return!1;case"{":return J(e.slice(t,n))}return" "===e[n-1]&&J(e.slice(t,n))}function q(e,t){return e.replace(t,"")}function J(e){return{type:"indent",value:e.replace($,"\n"),length:A.exec(e)[1].length}}}}e.FluentBundle=class{constructor(e,{functions:t={},useIsolating:n=!0,transform:r=(e=>e)}={}){this.locales=Array.isArray(e)?e:[e],this._terms=new Map,this._messages=new Map,this._functions=t,this._useIsolating=n,this._transform=r,this._intls=new WeakMap}get messages(){return this._messages[Symbol.iterator]()}hasMessage(e){return this._messages.has(e)}getMessage(e){return this._messages.get(e)}addMessages(e,t){const n=D.fromString(e);return this.addResource(n,t)}addResource(e,{allowOverrides:t=!1}={}){const n=[];for(const[r,s]of e)if(r.startsWith("-")){if(!1===t&&this._terms.has(r)){n.push(`Attempt to override an existing term: "${r}"`);continue}this._terms.set(r,s)}else{if(!1===t&&this._messages.has(r)){n.push(`Attempt to override an existing message: "${r}"`);continue}this._messages.set(r,s)}return n}format(e,t,n){return"string"==typeof e?this._transform(e):null===e||null===e.value?null:"string"==typeof e.value?this._transform(e.value):function(e,t,n,r=[]){return f({bundle:e,args:t,errors:r,dirty:new WeakSet,insideTermReference:!1},n).toString(e)}(this,t,e,n)}_memoizeIntlObject(e,t){const n=this._intls.get(e)||{},r=JSON.stringify(t);return n[r]||(n[r]=new e(this.locales,t),this._intls.set(e,n)),n[r]}},e.FluentDateTime=s,e.FluentError=h,e.FluentNumber=r,e.FluentResource=D,e.FluentType=t,Object.defineProperty(e,"__esModule",{value:!0})}));