-
Notifications
You must be signed in to change notification settings - Fork 0
/
oxygen.min.js
82 lines (82 loc) · 7.68 KB
/
oxygen.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// +--------------------------------------------------------------------+ \\
// ¦ OxygenJS 0.2.6 - High Performance JavaScript MicroTemplating ¦ \\
// +--------------------------------------------------------------------+ \\
// ¦ Copyright © 2016 Vincent Fontaine ¦ \\
// +---------+----------------------------------------------------------+ \\
// ¦ CREDITS | ¦ \\
// +---------+ ¦ \\
// ¦ * Kru for the reduce tip ¦ \\
// ¦ * NunJucks for the Jinja like filters ¦ \\
// ¦ * John Resig for his excellent work (http://ejohn.org/) ¦ \\
// +--------------------------------------------------------------------+ \\
!function(t){var r={},n=document,e=Object.prototype.toString,i=function(t){return n.getElementById(t)?n.getElementById(t).innerHTML:""},o=function(t){if("string"!=typeof t)return t
this.toString=function(){return t},this.length=t.length
for(var r=["charAt","charCodeAt","concat","contains","endsWith","fromCharCode","indexOf","lastIndexOf","length","localeCompare","match","quote","replace","search","slice","split","startsWith","substr","substring","toLocaleLowerCase","toLocaleUpperCase","toLowerCase","toUpperCase","trim","trimLeft","trimRight"],n=0;n<r.length;n++)this[r[n]]=proxyStr(t[r[n]])},u=function(t,r){return t instanceof o?new o(r):r.toString()},a=function(t,r){return null===t||void 0===t||t===!1?r:t},c={"&":"&",'"':""","'":"'","<":"<",">":">"},f={escape:function(t){return t.replace(/[&"'<>]/g,function(t){return c[t]})},isFunction:function(t){return"[object Function]"==e.call(t)},isArray:Array.isArray||function(t){return"[object Array]"==e.call(t)},isString:function(t){return"[object String]"==e.call(t)},isObject:function(t){return t===Object(t)},repeat:function(t,r){for(var n="",e=0;r>e;e++)n+=t
return n},map:function(t,r){var n=[]
if(null==t)return n
if(Array.prototype.map&&t.map===Array.prototype.map)return t.map(r)
for(var e=0;e<t.length;e++)n[n.length]=r(t[e],e)
return t.length===+t.length&&(n.length=t.length),n}},s=function(t,r){var n=r.split("|").reduce(function(t,r){var n=r.indexOf("(")+1||r.length,e=n==r.length
return"Object.O2.filters."+r.substring(0,n)+(e?"(":"")+t+(e?")":", ")+r.substring(n)})
return"', "+n+", '"}
Object.O2=function(t,n){if(""===t&&void 0===t)return""
var e=/\W/.test(t)?new Function("obj","var p=[];with(obj){p.push('"+t.replace(/{%[ ]*for ([$a-zA-Z_]+) in ([$a-zA-Z_]+([.][$a-zA-Z_]+)*)[ ]*%}/g,"{% for (var $1=0; $1<$2.length; $1++) { %}").replace(/{%[ ]*if (((?!%}).)*[^ ])\s*%}/g,"{% if ($1) { %}").replace(/{%[ ]*end[if|for]*[ ]*%}/g,"{% } %}").replace(/{%[ ]*else[ ]*%}/g,"{% } else { %}").replace(/[\r\t\n]/g," ").split("{%").join(" ").replace(/((^|%})[^\t]*)'/g,"$1\r").replace(/\t=(.*?)%}/g,"',$1,'").split(" ").join("');").split("%}").join("p.push('").split("\r").join("\\'").replace(/{{2}[ ]*([^{}, ]*[^{}]*[^{}, ]+)[ ]*}{2}/g,s)+"');}return p.join('');"):r[t]=r[t]||Object.O2(i(t))
return n?e(n):e},Object.O2.filters={abs:function(t){return Math.abs(t)},batch:function(t,r,n){for(var e=[],i=[],o=0;o<t.length;o++)o%r===0&&i.length&&(e.push(i),i=[]),i.push(t[o])
if(i.length){if(n)for(var o=i.length;r>o;o++)i.push(n)
e.push(i)}return e},capitalize:function(t){t=a(t,"")
var r=t.toLowerCase()
return u(t,r.charAt(0).toUpperCase()+r.slice(1))},center:function(t,r){if(t=a(t,""),r=r||80,t.length>=r)return t
var n=r-t.length,e=f.repeat(" ",n/2-n%2),i=f.repeat(" ",n/2)
return u(t,e+t+i)},"default":function(t,r,n){return n?t?t:r:void 0!==t?t:r},dictsort:function(t,r,n){if(!f.isObject(t))throw new"dictsort filter: val must be an object"
var e=[]
for(var i in t)e.push([i,t[i]])
var o
if(void 0===n||"key"===n)o=0
else{if("value"!==n)throw new"dictsort filter: You can only sort by either key or value"
o=1}return e.sort(function(t,n){var e=t[o],i=n[o]
return r||(f.isString(e)&&(e=e.toUpperCase()),f.isString(i)&&(i=i.toUpperCase())),e>i?1:e===i?0:-1}),e},dump:function(t){return JSON.stringify(t)},escape:function(t){return"string"==typeof t||t instanceof o?f.escape(t):t},safe:function(t){return new o(t)},first:function(t){return t[0]},groupby:function(t,r){for(var n={},e=f.isFunction(r)?r:function(t){return t[r]},i=0;i<t.length;i++){var o=t[i],u=e(o,i);(n[u]||(n[u]=[])).push(o)}return n},indent:function(t,r,n){if(t=a(t,""),""===t)return""
r=r||4
for(var e="",i=t.split("\n"),o=f.repeat(" ",r),c=0;c<i.length;c++)e+=0!==c||n?o+i[c]+"\n":i[c]+"\n"
return u(t,e)},join:function(t,r,n){return r=r||"",n&&(t=f.map(t,function(t){return t[n]})),t.join(r)},last:function(t){return t[t.length-1]},length:function(t){var r=a(val,"")
return void 0!==r?r.length:0},list:function(t){if(f.isString(t))return t.split("")
if(f.isObject(t)){var r=[]
if(Object.keys)r=Object.keys(t)
else for(var n in t)r.push(n)
return f.map(r,function(r){return{key:r,value:t[r]}})}if(f.isArray(t))return t
throw new"list filter: type not iterable"},lower:function(t){return t=a(t,""),t.toLowerCase()},random:function(t){return t[Math.floor(Math.random()*t.length)]},rejectattr:function(t,r){return t.filter(function(t){return!t[r]})},selectattr:function(t,r){return t.filter(function(t){return!!t[r]})},replace:function(t,r,n,e){var i="",a=t
if(r instanceof RegExp)return t.replace(r,n)
if("undefined"==typeof e&&(e=-1),"number"==typeof r)r+=""
else if("string"!=typeof r)return t
if("number"==typeof t&&(t+=""),"string"!=typeof t&&!(t instanceof o))return t
if(""===r)return i=n+t.split("").join(n)+n,u(t,i)
var c=t.indexOf(r)
if(0===e||-1===c)return t
for(var f=0,s=0;c>-1&&(-1===e||e>s);)i+=t.substring(f,c)+n,f=c+r.length,s++,c=t.indexOf(r,f)
return f<t.length&&(i+=t.substring(f)),u(a,i)},reverse:function(t){var r
return r=f.isString(t)?this.list(t):f.map(t,function(t){return t}),r.reverse(),f.isString(t)?u(t,r.join("")):r},round:function(t,r,n){r=r||0
var e,i=Math.pow(10,r)
switch(n){case"ceil":e=Math.ceil
break
case"floor":e=Math.floor
break
default:e=Math.round}return e(t*i)/i},slice:function(t,r,n){for(var e=Math.floor(t.length/r),i=t.length%r,o=0,u=[],a=0;r>a;a++){var c=o+a*e
i>a&&o++
var f=o+(a+1)*e,s=t.slice(c,f)
n&&a>=i&&s.push(n),u.push(s)}return u},sum:function(t,r,n){var e=0
"number"==typeof n&&(e+=n),r&&(t=f.map(t,function(t){return t[r]}))
for(var i=0;i<t.length;i++)e+=t[i]
return e},sort:function(t,r,n,e){return t=f.map(t,function(t){return t}),t.sort(function(t,i){var o,u
return e?(o=t[e],u=i[e]):(o=t,u=i),!n&&f.isString(o)&&f.isString(u)&&(o=o.toLowerCase(),u=u.toLowerCase()),u>o?r?1:-1:o>u?r?-1:1:0}),t},string:function(t){return u(t,t)},striptags:function(t,r){t=a(t,""),r=r||!1
var n=/<\/?([a-z][a-z0-9]*)\b[^>]*>|<!--[\s\S]*?-->/gi,e=filters.trim(t.replace(n,"")),i=""
return i=r?e.replace(/^ +| +$/gm,"").replace(/ +/g," ").replace(/(\r\n)/g,"\n").replace(/\n\n\n+/g,"\n\n"):e.replace(/\s+/gi," "),u(t,i)},title:function(t){t=a(t,"")
for(var r=t.split(" "),n=0;n<r.length;n++)r[n]=this.capitalize(r[n])
return u(t,r.join(" "))},trim:function(t){return u(t,t.replace(/^\s*|\s*$/g,""))},truncate:function(t,r,n,e){var i=t
if(t=a(t,""),r=r||255,t.length<=r)return t
if(n)t=t.substring(0,r)
else{var o=t.lastIndexOf(" ",r);-1===o&&(o=r),t=t.substring(0,o)}return t+=void 0!==e&&null!==e?e:"...",u(i,t)},upper:function(t){return t=a(t,""),t.toUpperCase()},urlencode:function(t){var r,n=encodeURIComponent
if(f.isString(t))return n(t)
if(f.isArray(t))r=t.map(function(t){return n(t[0])+"="+n(t[1])})
else{r=[]
for(var e in t)t.hasOwnProperty(e)&&r.push(n(e)+"="+n(t[e]))}return r.join("&")},wordcount:function(t){return t=a(t,""),t.match(/\w+/g).length},"float":function(t,r){var n=parseFloat(t)
return isNaN(n)?r:n},"int":function(t,r){var n=parseInt(t,10)
return isNaN(n)?r:n}},t.O2=Object.O2}(window)