/
frenchkiss.js
6 lines (6 loc) · 2.65 KB
/
frenchkiss.js
1
2
3
4
5
6
/**
* @license frenchkiss 0.3.0
* Copyright (c) 2018-2022 Koala Interactive, Inc.
* License: MIT
*/
var e,n;e=this,n=function(){"use strict";var e=/^\s*\w+\s*$/,n=/^\s*(\w+)\s*,\s*(select|plural)\s*,/i,t=JSON.stringify,r=function(e){return'(p["'+e+'"]||(p["'+e+'"]=="0"?0:"'+e+'" in p?"":v("'+e+'",k,l)))'};function o(e){for(var n={},o=function e(n,o){var f=[];var i=n.length;for(var u=0;u<i;++u){var c=n[u],s=c[0],a=c[1],l="";if(0===s&&a)l=t(a);else if(1===s)l=r(a.trim());else if(2===s){for(var v=c[2],p=c[3],h=c[4],d=p.length,g=0;g<d;++g)h?"="===p[g][0][0]?l+='p["'+v+'"]=='+t(p[g][0].substr(1)):(o[v]=1,l+='m["'+v+'"]=='+t(p[g][0])):l+='p["'+v+'"]=='+t(p[g][0]),l+="?"+e(p[g][1],o)+":";l="("+l+e(a,o)+")"}l&&f.push(l)}return f.join("+")||'""'}(f(e),n),i=Object.keys(n),u=i.length,c=[],s=0;s<u;++s)c[s]=i[s]+':f(p["'+i[s]+'"])';return Function("a","f","k","l","v","var p=a||{}"+(u?",m=f?{"+c+"}:{}":"")+";return "+o)}function f(t){for(var r=0,o="",f=t.length,i=[],u=0;u<f;++u){var s=t[u],a=void 0;"{"===s?r++||(a=[0,o]):"}"===s&&(--r||(a=e.test(o)?[1,o]:n.test(o)?c(o):[0,o])),a?(i.push(a),o=""):o+=s}return i.push([0,o]),i}function i(e,n,t,r){return e.slice(0,t)+n+e.slice(r)}function u(e,n){for(var t=-1;-1!==(t=e.indexOf("#",t+1));)"'"===e[t-1]&&"'"===e[t+1]?e=i(e,"#",t-1,t+2):(e=i(e,"{"+n+"}",t,t+1),t+=n.length+2);return e}function c(e){for(var t=e.match(n),r=t[1],o="p"===t[2][0].toLowerCase(),i=f(e.replace(n,"")),c=i.length,s=[],a=[0,""],l=0;l<c-1;){var v=i[l++][1].trim(),p=i[l++][1];o&&(p=u(p,r));var h=f(p);"other"===v?a=h:o&&"="===v[0]?s.unshift([v,h]):s.push([v,h])}return[2,a,r,s,o]}var s={},a={},l={},v="",p="",h=function(e){return e},d=function(){return""},g=function(e,n){return s[n]&&s[n][e]||a[n]&&"string"==typeof a[n][e]&&(s[n][e]=o(a[n][e]))},y=function e(n,t,r){for(var o=Object.keys(t),f=o.length,i=0;i<f;++i){var u=o[i],c=r+u;"object"==typeof t[u]?(e(n,t[u],c+"."),delete n[u]):n[c]=t[u]+""}},b=function e(n,t,r,o){for(var f=Object.keys(r),i=f.length,u=0;u<i;++u){var c=f[u],s=o+c;"object"==typeof r[c]?e(n,t,r[c],s+"."):n[s]!==r[c]&&(delete t[s],n[s]=r[c]+"")}};return{cache:s,store:a,t:function(e,n,t){var r,o=t||v;return o&&(r=g(e,o))?r(n,l[o],e,o,d):(o=p)&&(r=g(e,o))?r(n,l[o],e,o,d):h(e,n,t||v)},onMissingKey:function(e){h=e},onMissingVariable:function(e){d=e},locale:function(e){return e&&(v=e),v},fallback:function(e){return e&&(p=e),p},set:function(e,n){y(n,n,""),s[e]={},a[e]=n},unset:function(e){delete s[e],delete a[e]},extend:function(e,n){a[e]||(a[e]={}),s[e]||(s[e]={}),b(a[e],s[e],n,"")},plural:function(e,n){l[e]=n}}},"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(e=e||self).frenchkiss=n();