-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathi18nextSprintfPostProcessor.min.js
1 lines (1 loc) · 3.5 KB
/
i18nextSprintfPostProcessor.min.js
1
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define("i18nextSprintfPostProcessor",t):e.i18nextSprintfPostProcessor=t()}(this,function(){"use strict";function e(){var t=arguments[0],r=e.cache;return r[t]&&r.hasOwnProperty(t)||(r[t]=e.parse(t)),e.format.call(null,r[t],arguments)}function t(t,r,n){return n=(r||[]).slice(0),n.splice(0,0,t),e.apply(null,n)}function r(e){return Object.prototype.toString.call(e).slice(8,-1).toLowerCase()}function n(e,t){return Array(t+1).join(e)}var o={};o["typeof"]="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol?"symbol":typeof e};var s={not_string:/[^s]/,number:/[diefg]/,json:/[j]/,not_json:/[^j]/,text:/^[^\x25]+/,modulo:/^\x25{2}/,placeholder:/^\x25(?:([1-9]\d*)\$|\(([^\)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-gijosuxX])/,key:/^([a-z_][a-z_\d]*)/i,key_access:/^\.([a-z_][a-z_\d]*)/i,index_access:/^\[(\d+)\]/,sign:/^[\+\-]/};e.format=function(t,o){var i,a,l,c,f,p,u,g=1,y=t.length,d="",h=[],b=!0,x="";for(a=0;y>a;a++)if(d=r(t[a]),"string"===d)h[h.length]=t[a];else if("array"===d){if(c=t[a],c[2])for(i=o[g],l=0;l<c[2].length;l++){if(!i.hasOwnProperty(c[2][l]))throw new Error(e("[sprintf] property '%s' does not exist",c[2][l]));i=i[c[2][l]]}else i=c[1]?o[c[1]]:o[g++];if("function"==r(i)&&(i=i()),s.not_string.test(c[8])&&s.not_json.test(c[8])&&"number"!=r(i)&&isNaN(i))throw new TypeError(e("[sprintf] expecting number but found %s",r(i)));switch(s.number.test(c[8])&&(b=i>=0),c[8]){case"b":i=i.toString(2);break;case"c":i=String.fromCharCode(i);break;case"d":case"i":i=parseInt(i,10);break;case"j":i=JSON.stringify(i,null,c[6]?parseInt(c[6]):0);break;case"e":i=c[7]?i.toExponential(c[7]):i.toExponential();break;case"f":i=c[7]?parseFloat(i).toFixed(c[7]):parseFloat(i);break;case"g":i=c[7]?parseFloat(i).toPrecision(c[7]):parseFloat(i);break;case"o":i=i.toString(8);break;case"s":i=(i=String(i))&&c[7]?i.substring(0,c[7]):i;break;case"u":i>>>=0;break;case"x":i=i.toString(16);break;case"X":i=i.toString(16).toUpperCase()}s.json.test(c[8])?h[h.length]=i:(!s.number.test(c[8])||b&&!c[3]?x="":(x=b?"+":"-",i=i.toString().replace(s.sign,"")),p=c[4]?"0"===c[4]?"0":c[4].charAt(1):" ",u=c[6]-(x+i).length,f=c[6]&&u>0?n(p,u):"",h[h.length]=c[5]?x+i+f:"0"===p?x+f+i:f+x+i)}return h.join("")},e.cache={},e.parse=function(e){for(var t=e,r=[],n=[],o=0;t;){if(null!==(r=s.text.exec(t)))n[n.length]=r[0];else if(null!==(r=s.modulo.exec(t)))n[n.length]="%";else{if(null===(r=s.placeholder.exec(t)))throw new SyntaxError("[sprintf] unexpected placeholder");if(r[2]){o|=1;var i=[],a=r[2],l=[];if(null===(l=s.key.exec(a)))throw new SyntaxError("[sprintf] failed to parse named argument key");for(i[i.length]=l[1];""!==(a=a.substring(l[0].length));)if(null!==(l=s.key_access.exec(a)))i[i.length]=l[1];else{if(null===(l=s.index_access.exec(a)))throw new SyntaxError("[sprintf] failed to parse named argument key");i[i.length]=l[1]}r[2]=i}else o|=2;if(3===o)throw new Error("[sprintf] mixing positional and named placeholders is not (yet) supported");n[n.length]=r}t=t.substring(r[0].length)}return n};var i={name:"sprintf",type:"postProcessor",process:function(r,n,s){return s.sprintf?"[object Array]"===Object.prototype.toString.apply(s.sprintf)?t(r,s.sprintf):"object"===o["typeof"](s.sprintf)?e(r,s.sprintf):r:r},overloadTranslationOptionHandler:function(e){for(var t=[],r=1;r<e.length;r++)t.push(e[r]);return{postProcess:"sprintf",sprintf:t}}};return i});