utilise
Version:
Lean JavaScript Utilities as Micro-libraries
1 lines • 18.5 kB
JavaScript
!function(){"use strict";function n(n){return function(t){return t==n}}n.fn=function(n){return"function"==typeof n},n.str=function(n){return"string"==typeof n},n.num=function(n){return"number"==typeof n},n.obj=function(n){return"object"==typeof n},n.lit=function(n){return n.constructor==Object},n.bol=function(n){return"boolean"==typeof n},n.truthy=function(n){return 1==!!n},n.falsy=function(n){return 0==!!n},n.arr=function(n){return n instanceof Array},n.null=function(n){return null===n},n.def=function(n){return void 0!==n},n.in=function(n){return function(t){return!!n&&(n.indexOf?~n.indexOf(t):t in n)}},n.promise=function(n){return n instanceof Promise},n.stream=function(n){return!(!n||!n.next)};var t={arr:function(n){return Array.prototype.slice.call(n,0)},obj:function(t){var e="id";return 1==arguments.length?(e=t,r):r.apply(this,arguments);function r(t,r,o){return 0===o&&(t={}),t[n.fn(e)?e(r,o):r[e]]=r,t}}};function e(n,e){var r=!e&&document.head.createShadowRoot?"html /deep/ ":"";return t.arr((e||document).querySelectorAll(r+n))}function r(t,e){var o=arguments.length;return n.str(t)||2!=o?n.str(t)||3!=o?function(r){var u=this||{};return r=(r=(r=u.nodeName||n.fn(u.node)?u:r).node?r.node():r).host||r,o>1&&!1===e?r.removeAttribute(t):o>1?(r.setAttribute(t,e),e):r.attributes.getNamedItem(t)&&r.attributes.getNamedItem(t).value}:r(arguments[1],arguments[2]).call(this,arguments[0]):r(arguments[1]).call(this,arguments[0])}function o(n){return function(){return n}}function u(t){return Object.keys(n.obj(t)||n.fn(t)?t:{})}function i(t){return 0===t?"0":t?n.fn(t)?""+t:n.obj(t)?JSON.stringify(t):String(t):""}function c(t,e){var r=arguments.length>1,u=n.fn(t)?[]:i(t).split(".").filter(Boolean),f=u.shift();return function(i,a){var l={};return i?n.num(t)||t?n.arr(t)?(t.map(function(t){var r=c(t)(i);null!=(r=n.fn(e)?e(r):null==r?e:r)&&c(t,n.fn(r)?o(r):r)(l)}),l):i[t]||!u.length?r?(i[t]=n.fn(e)?e(i[t],a):e,i):n.fn(t)?t(i):i[t]:r?(c(u.join("."),e)(i[f]?i[f]:i[f]={}),i):c(u.join("."))(i[f]):r?function(n,t){return dir(n).map(function(t){delete n[t]}),dir(t).map(function(e){n[e]=t[e]}),n}(i,e):i:void 0}}function f(t,e){var r=1==arguments.length;return function(o){var u=n.fn(t)?t(o):c(t)(o);return u&&e&&u.toLowerCase&&e.toLowerCase?u.toLowerCase()===e.toLowerCase():r?Boolean(u):n.fn(e)?e(u):u==e}}var a="undefined"!=typeof window;function l(n){return n&&JSON.parse(n)}function s(n,t){return t in n}var h=void["red","green","bold","grey","strip"].forEach(function(t){!n.str(String.prototype[t])&&Object.defineProperty(String.prototype,t,{get:function(){return String(this)}})});function p(n,t){return function(e){return t[e]=n[e],e}}function d(n){return n.__data__}var m=a?window:global;function v(n){return function(t){return t.split(n)}}function g(n){return n}var y=w((a?(m.location.search.match(/debug=(.*?)(&|$)/)||[])[1]:c("process.env.DEBUG")(m))||""),b=y.split(",").map(v("/"));function w(n){return n.replace(/(\[|\])/g,"")}function S(n,t,e,r){return n.host&&n.host.nodeName&&(n=n.host),t.name&&(e=t,t=t.name),!s(n,t)&&Object.defineProperty(n,t,{value:e,writable:r}),n[t]}function j(){var n,t,e=new Promise(function(e,r){n=e,t=r});return arguments.length&&n(arguments[0]),e.resolve=n,e.reject=t,e}function C(n){return function(t){return n+t}}function A(n,t){return t instanceof Array&&(t=t.reduce(A,[])),(n=n||[]).concat(t)}var L=function(){};function O(n,t){return t=t||{},S(n=n||{},"emit",function(t,r,o){for(var u=n.on[t.split(".")[0]]||[],i=[],c=0;c<u.length;c++)u[c].ns&&o&&!o(u[c].ns)||i.push(e(u[c].isOnce?u.splice(c--,1)[0]:u[c],r));for(var c=0;c<n.on["*"].length;c++)i.push(e(n.on["*"][c],[t,r]));return i.reduce(A,[])},1),S(n,"once",function(t,e){return n.on(t,e,!0)},1),S(n,"off",function(t,e){r(n.on[t]||[],e),e&&e.ns&&delete n.on[t]["$"+e.ns];return n},1),S(n,"on",function(e,u,i){var c=e.split(".")[0],f=e.split(".")[1],a=n.on[c]=n.on[c]||[],l="function"==typeof u?u:0;return!l&&f?(l=n.on[c]["$"+f])?l:s(o(n,u)):l||f?l&&f?s((r(a,n.on[c]["$"+f]||-1),l)):!(!l||f)&&s(l):s(o(n,u));function s(e){return e.isOnce=i,e.type=c,f&&(n.on[c]["$"+(e.ns=f)]=e),a.push(e),(t.on||L)(e),e.next?e:n}},1),n.on["*"]=n.on["*"]||[],n;function e(t,e){return t.next?t.next(e):e instanceof Array?t.apply(n,e):t.call(n,e)}function r(n,e){for(var r=n.length;~--r;)e!=n[r]&&e!=n[r].fn&&e||(t.off||L)(n.splice(r,1)[0])}function o(n,t){var e=O((t=t||{}).base||j());return e.i=0,e.li=[],e.fn=t.fn,e.parent=n,e.source=t.fn?e.parent.source:e,e.on("stop",function(n){return e.type?e.parent.off(e.type,e):e.parent.off(e),e.reason=n}),e.each=function(n){var t=n.next?n:o(e,{fn:n});return e.li.push(t),t},e.pipe=function(n){return n(e)},e.map=function(n){return e.each(function(t,e,r){return r.next(n(t,e,r))})},e.filter=function(n){return e.each(function(t,e,r){return n(t,e,r)&&r.next(t)})},e.reduce=function(n,t){return e.each(function(e,r,o){return o.next(t=n(t,e,r,o))})},e.unpromise=function(){var n=o(e,{base:{},fn:function(t){return n.next(t)}});return e.li.push(n),n},e.next=function(n){return e.resolve&&e.resolve(n),e.li.length?e.li.map(function(t){return t.fn(n,t.i++,t)}):n},e.until=function(n){return n?n.each?n.each(e.stop):n.then?n.then(e.stop):n.call?e.filter(n).map(e.stop):0:0},e.off=function(n){return r(e.li,n),e},e.start=function(n){return e.until(n),e.source.emit("start"),e},e.stop=function(n){return e.source.emit("stop",n)},e[Symbol.asyncIterator]=function(){return{next:function(){return e.wait=new Promise(function(n){e.wait=!0,e.map(function(t,r,o){delete e.wait,e.off(o),n({value:t,done:!1})}),e.emit("pull",e)})}}},e}}var N={"&":"&","<":"<",">":">",'"':""","'":"'"};function k(n){return function(){return!n.apply(this,arguments)}}function E(n){return function(t){return t&&t.indexOf&&~t.indexOf(n)}}function T(n){return function(t){return c(t)(n)}}function q(){}function B(n){return n&&n[n.length-1]}function D(e){return function(r){if(!m.console||!console.log.apply)return r;n.arr(arguments[2])&&(arguments[2]=arguments[2].length);var o=t.arr(arguments),u="[log]["+(new Date).toISOString()+"]"+e;return o.unshift(u.grey?u.grey:u),r}}function M(n){return m.moment(n)}T.parent=function(n){return d(this.parentNode)[n]},M.format=function(n){return function(t){return M(t).format(n)}},M.iso=function(n){return M(n).format("YYYY-MM-DD")};var I=c,P=/([^\.\[]*)/;function $(n){if(n.on)return;n.listeners={};O(n,{on:e=>{const r=e.type.split(".").shift();n.listeners[r]||n.addEventListener(r,n.listeners[r]=(e=>e.detail&&e.detail.emitted?0:t(r,[e,n.state,n])))},off:t=>{n.on[t.type].length||(n.removeEventListener(t.type,n.listeners[t.type]),delete n.listeners[t.type])}});const{emit:t}=n;n.emit=function(e,r){const o=new CustomEvent(e,{detail:{params:r,emitted:!0},bubbles:!1,cancelable:!0});return n.dispatchEvent(o),t(e,o)}}function _(n,t){return function(){var e=arguments;return t.each(function(){var t=this.host&&this.host.nodeName?this.host:this;t[n]&&t[n].apply(t,e)}),t}}function J(n,t,e){var r=0,o=[],u=[],i=n.call?n(t[e],e):n,c=P.exec(i)[1]||"div",f=document.createElement(c);for((n.call?n.toString():n).replace(/\[(.+?)="(.*?)"\]/g,function(n,t,e){return o[o.length]=[t,e],""}).replace(/\.([^.]+)/g,function(n,t){return u[u.length]=t,""}),r=0;r<o.length;r++)f.setAttribute(o[r][0],o[r][1]);for(r=0;r<u.length;r++)f.classList.add(u[r]);return f.state=t[e]||1,f}function Y(n,t,e,r,o,u,i,c){for(var f,a,l,s=-1,h=t.length,p={};o.children[++s];){if(!o.children[s].matches(n))continue;p[e(o.children[s].state)]=o.children[s]}for(l=r?o.querySelector(r):null;h--;){if(a=p[f=e(t[h])]){if(!0===a)continue;a.state=t[h]}else i.unshift(a=J(n,t,h));p[f]=!0,h!=t.length-1&&l===a.nextSibling||o.insertBefore(a,l),u.unshift(l=a),"function"==typeof a.draw&&a.draw()}for(s in p)!0!==p[s]&&c.unshift(o.removeChild(p[s]))}var z={add:function(t,e,r){n.arr(t)?t.splice(e,0,r):t[e]=r},update:function(t,e,r){if(n.num(e)||e)t[e]=r;else{if(!n.obj(r))return!0;for(var o in t)delete t[o];for(var o in r)t[o]=r[o]}},remove:function(t,e,r){n.arr(t)?t.splice(e,1):delete t[e]}},H=JSON.stringify,R=JSON.parse;function F(t,e){return function(r,o,u){if(!n.obj(r)&&!n.fn(r))return r;if(!n.obj(t)){var i=o||r.log||[],c=r;if(n.def(u)||(u=i.max||0),u||(i=[]),u<0&&(i=i.concat(null)),u>0){var f=H(r);c=R(f),i=i.concat({type:"update",value:R(f),time:i.length})}return S(i,"max",u),c.log?c.log=i:S(O(c,null),"log",i,1),c}return!!n.def(t.key)&&(!!function n(t,e,r,o){var u=r.shift();if(!z[e])return!1;if(r.length){if(!(u in t)){if("remove"==e)return!0;t[u]={}}return n(t[u],e,r,o)}return!z[e](t,u,o)}(r,t.type,(t.key=""+t.key).split(".").filter(Boolean),t.value)&&(r.log&&r.log.max&&r.log.push((t.time=r.log.length,r.log.max>0?t:null)),!e&&r.emit&&r.emit("change",t),r))}}var G=D("[perf]");m.all=e,m.append=function(n){return function(t){return t+n}},m.args=function(e){return function(r,o){return function(){var u=n.arr(e)?e:[e],i=t.arr(arguments).filter(function(t,e){return n.in(u)(e)});return r.apply(o||this,i)}}},m.attr=r,m.az=function(){return function n(t){return function(e,r){if(!t.length)return 0;var o=t[0],u=c(o)(e)||"",i=c(o)(r)||"";return u>i?1:u<i?-1:n(t.slice(1))(e,r)}}(t.arr(arguments))},m.by=f,m.client=a,m.clone=function(t){return n.fn(t)||n.str(t)?t:l(i(t))},m.colorfill=h,m.copy=p,m.datum=d,m.deb=function(e){return"*"==y||b.some(function(n){return n=w(n).split("/"),function(t){return 1==t.length?t[0]==n[0]:2==t.length&&t[0]==n[0]&&t[1]==n[1]}}(e))?function(r){if(!m.console||!console.log.apply)return r;n.arr(arguments[2])&&(arguments[2]=arguments[2].length);var o=t.arr(arguments),u="[deb]["+(new Date).toISOString()+"]"+e;return o.unshift(u.grey?u.grey:u),r}:g},m.debounce=function(t){var e,r=n.num(t)?t:100;return n.fn(t)?o(t):o;function o(n){return function(){var t=this,o=arguments;e&&clearTimeout(e),e=setTimeout(function(){n.apply(t,o)},r)}}},m.def=S,m.defaults=function(t,e,r){return t.host&&(t=t.host),n.obj(e)?(u(e).map(function(n){o(n,e[n])}),t):(o(e,r),t[e]);function o(e,r){n.def(t[e])||(t[e]=r)}},m.delay=function(n,t){var e=j(),r=setTimeout(function(){e.resolve(t)},n);return e.abort=function(){clearTimeout(r)},e},m.done=function(n){return function(t){n.once("response._"+(n.log.length-1),t)}},m.el=function(n){var t=[],e=n.replace(/\[(.+?)=(.*?)\]/g,function(n,e,r){return t.push([e,r]),""}).split("."),o=e.shift(),u=document.createElement(o);return t.forEach(function(n){r(u,n[0],n[1])}),e.forEach(function(n){u.classList.add(n)}),u.toString=function(){return o+e.map(C(".")).join("")},u},m.emitterify=O,m.err=function(e){return function(r){if(!m.console||!console.error.apply)return r;n.arr(arguments[2])&&(arguments[2]=arguments[2].length);var o=t.arr(arguments),u="[err]["+(new Date).toISOString()+"]"+e;return o.unshift(u.red?u.red:u),r}},m.escape=function(n){return n.replace(/[&<>'"]/g,function(n){return N[n]})},m.extend=function(t){return function(e){return u(e).filter(k(n.in(t))).map(p(e,t)),t}},m.falsy=function(){return!1},m.first=function(n){return n&&n[0]},m.flatten=A,m.fn=function(t){return n.fn(t)?t:new Function("return "+t)()},m.form=function(n){var t=r("name"),o={},u=[];return e("[name]",n).map(function(n){var e=t(n);o[e]="object"==typeof n.state&&"value"in n.state?n.state.value:n.files?n.files:"checkbox"==n.type?(o[e]||[]).concat(n.checked?n.value:[]):"radio"==n.type?n.checked?n.value:o[e]:n.value,E("is-invalid")(n.className)&&u.push(n)}),{values:o,invalid:u}},m.from=T,m.grep=function(e,r,o){var u=e[r];return e[r]=function(){return t.arr(arguments).filter(n.str).join(" ").match(o)&&u.apply(this,arguments)},u},m.group=function(n,t){return m.console?(console.groupCollapsed||(console.groupCollapsed=console.groupEnd=function(n){(console.log||q)("*****",n,"*****")}),t()):t()},m.has=s,m.hashcode=function(n){var t=0;if(!n)return t;for(var e=0;e<n.length;e++)t=(t<<5)-t+n.charCodeAt(e),t&=t;return t},m.header=function(n,t){var e=1==arguments.length;return function(r){return r&&r.headers?e?c(n)(r.headers):c(n)(r.headers)==t:null}},m.identity=g,m.iff=function(n){return function(t){return function(){if(n.apply(this,arguments))return t.apply(this,arguments)}}},m.includes=E,m.is=n,m.join=function(t,e){return 1==arguments.length&&(e=t,t=null),function(r,o){if(null!=r){var u=e||[],i=null;if(o&&!n.num(o)||(o="id"),n.str(e)){var a=e.split(".");u=ripple(a.shift()),i=a.join(".")}var l=c(t)(r),s=u.filter(f(o,l)).map(c(i)).pop()||{};return t?c(t,s)(r):s}}},m.key=c,m.keys=u,m.last=B,m.lo=function(n){return(n||"").toLowerCase()},m.log=D,m.merge=function t(e){return function(r){for(x in r)n.obj(r[x])&&n.obj(e[x])?t(e[x])(r[x]):e[x]=r[x];return e}},m.mo=M,m.noop=q,m.not=k,m.nullify=function(t){return n.fn(t)?function(){return!!t.apply(this,arguments)||null}:!!t||null},m.once=function n(t,e,r){for(var o=i.nodes=Array===t.constructor?t:"string"==typeof t?document.querySelectorAll(t):[t],u=o.length;u-- >0;)o[u].on||$(o[u]);return i.node=function(){return o[0]},i.enter=function(){return n(e)},i.exit=function(){return n(r)},i.size=function(){return o.length},i.text=function(n){var t="function"==typeof n;return 0===arguments.length?o[0].textContent:(this.each(function(e,r,o){var u,i=""+(t?n.call(this,r,o):n);this.textContent!==i&&((u=this.firstChild)?"#text"===u.nodeName?u.nodeValue=i:this.textContent=i:this.appendChild(document.createTextNode(i)))}),this)},i.html=function(n){var t="function"==typeof n;return 0===arguments.length?o[0].innerHTML:(this.each(function(e,r,o){var u=""+(t?n.call(this,r,o):n);this.innerHTML!==u&&(this.innerHTML=u)}),this)},i.attr=function(n,t){var e="function"==typeof t;return 1===arguments.length?o[0].getAttribute(n):(this.each(function(r,o,u){var i=e?t.call(this,o,u):t;!i&&this.hasAttribute(n)?this.removeAttribute(n):i&&this.getAttribute(n)!==i&&this.setAttribute(n,i)}),this)},i.classed=function(n,t){var e="function"==typeof t;return 1===arguments.length?o[0].classList.contains(n):(this.each(function(r,o,u){var i=e?t.call(this,o,u):t;i&&!this.classList.contains(n)?this.classList.add(n):!i&&this.classList.contains(n)&&this.classList.remove(n)}),this)},i.property=function(n,t){var e="function"==typeof t;return 1===arguments.length?I(n)(o[0]):(this.each(function(r,o,u){var i=e?t.call(this,o,u):t;void 0!==i&&I(n)(this)!==i&&I(n,function(){return i})(this)}),this)},i.each=function(n){for(u=-1;o[++u];)n.call(o[u],o[u],o[u].state,u);return this},i.remove=function(){return this.each(function(){var n=this.host&&this.host.nodeName?this.host:this;n.parentNode.removeChild(n)}),this},i.closest=function(t){return n(o.map(function(n){return n.closest(t)}).filter(Boolean))},i.draw=_("draw",i),i.once=_("once",i),i.emit=_("emit",i),i.on=_("on",i),i;function i(t,e,r,u){var i,c,f=[],a=[],l=[],s=-1,h=-1,p=-1,d=-1;if(1===arguments.length){if("string"!=typeof t)return n(t);for(;o[++h];)f=f.concat(Array.prototype.slice.call(o[h].querySelectorAll(t),0));return n(f)}if(1===e&&2==arguments.length){for(;o[++h];){for(s=o[h].children.length,i=t.call?t(o[h].state||1,0):t;o[h].children[--s];)if(o[h].children[s].matches(i)){(f[++d]=o[h].children[s]).state=o[h].state||1;break}s<0&&o[h].appendChild(f[++d]=a[a.length]=J(i,[o[h].state||1],0)),"function"==typeof f[d].draw&&f[d].draw()}return n(f,a,l)}for(;o[++h];)if(i="function"==typeof t?t(o[h].state):t,c="function"==typeof e?e(o[h].state):e,1===e&&(c=o[h].state||[1]),"string"==typeof c&&(c=[c]),c||(c=[]),c.constructor!==Array&&(c=[c]),r)Y(i,c,r,u,o[h],f,a,l);else{for(p=-1,s=-1;o[h].children[++s];)o[h].children[s].matches(i)&&(++p>=c.length?(o[h].removeChild(l[l.length]=o[h].children[s]),--s):((f[++d]=o[h].children[s]).state=c[p],"function"==typeof o[h].children[s].draw&&o[h].children[s].draw()));if("string"==typeof i)for(o[h].templates=o[h].templates||{},o[h].templates[i]=o[h].templates[i]||J(i,[],0);++p<c.length;)(u?o[h].insertBefore(f[++d]=a[a.length]=o[h].templates[i].cloneNode(!1),o[h].querySelector(u)):o[h].appendChild(f[++d]=a[a.length]=o[h].templates[i].cloneNode(!1))).state=c[p],"function"==typeof f[d].draw&&f[d].draw();else for(;++p<c.length;)u?o[h].insertBefore(f[++d]=a[a.length]=J(i,c,p),o[h].querySelector(u)):o[h].appendChild(f[++d]=a[a.length]=J(i,c,p)),"function"==typeof f[d].draw&&f[d].draw()}return n(f,a,l)}},m.overwrite=function(n){return function(t){return u(t).map(p(t,n)),n}},m.owner=m,m.parse=l,m.patch=function(n,t){return function(e){return u(t).map(function(r){return F({key:n+"."+r,value:t[r],type:"update"})(e)}),e}},m.perf=function(n,t,e){return function(){var t=a?performance.now():process.hrtime(),e=n.apply(this,arguments);return e instanceof Promise?e.then(function(){r(t)}):r(t),e};function r(r){var o=a?performance.now()-r:process.hrtime(r);a||(o=1e3*o[0]+o[1]/1e6),o=Math.round(100*o)/100,(e||G)(t||n.name,o,"ms")}},m.pop=function(t){return n.arr(t)?F({key:t.length-1,value:B(t),type:"remove"})(t):t},m.prepend=C,m.promise=j,m.proxy=function(t,e,r){return function(){var o=(t||g).apply(r||this,arguments);return n.fn(e)?e.call(r||this,o):e||o}},m.push=function(t){return function(e){return n.arr(e)?F({key:e.length,value:t,type:"add"})(e):e}},m.raw=function(n,t){var e=!t&&document.head.createShadowRoot?"html /deep/ ":"";return(t||document).querySelector(e+n)},m.ready=function(n){return document.body?n():document.addEventListener("DOMContentLoaded",n.bind(this))},m.remove=function(n){return function(t){return F({key:n,value:c(n)(t),type:"remove"})(t)}},m.replace=function(n,t){return function(e){return e.replace(n,t)}},m.set=F,m.slice=function(n,t){return function(e){return e.slice(n,t)}},m.sort=function(n){return function(t){return t.sort(n)}},m.split=v,m.str=i,m.stripws=function(t){return(n.arr(t)?t[0]:t).replace(/([\s]{2,}|[\n])/gim,"")},m.tdraw=function(n,t,e){var r=n.node?n.node():n;return r.state=e||{},r.draw=function(n){return t&&t.call(r,r,r.state)},r.draw(),n},m.th=function(n){return function(){return n(this).apply(this,arguments)}},m.time=function(n,t){return 1===arguments.length?setTimeout(n):setTimeout(t,n)},m.to=t,m.unique=function t(e,r){return r||(t.matched=[]),!n.in(t.matched)(e)&&t.matched.push(e)},m.update=function(n,t){return function(e){return F({key:n,value:t,type:"update"})(e)}},m.values=function(n){return n?u(n).map(T(n)):[]},m.wait=function n(t){return function(e){return function(){t.apply(this,arguments)?e.apply(this,arguments):this.once("change",n(t)(e))}}},m.wrap=o,m.za=function(){return function n(t){return function(e,r){if(!t.length)return 0;var o=t[0],u=c(o)(e)||"",i=c(o)(r)||"";return u<i?1:u>i?-1:n(t.slice(1))(e,r)}}(t.arr(arguments))},(a?window:global).owner=m}();