@rajaniraiyn/raj.js
Version:
A modern, fast, performant and lightweight jQuery alternative.
8 lines (7 loc) • 4.14 kB
JavaScript
/**!
Raj.js
© 2021 Rajaniraiyn
A modern alternative to jQuery.
*/
(t=>{function e(t){if(void 0!==t){return!0===function(t){return!0===(t.includes("#")&&(t.includes(".")||/[~!+:>\\\-()\[\]*]\w*/gm.test(t)))||/[~!+:>\\\-()\[\]*]\w*/gm.test(t)}(t=t.trim())?new Raj(document.querySelectorAll(t),t):!0===t.startsWith("#")?new Raj(document.getElementById(t.replace("#","")),t):!0===t.startsWith(".")?new Raj(document.getElementsByClassName(t.replace(".","")),t):new Raj(document.getElementsByTagName(t),t)}}function s(t,e){for(var s=0,i=t.length;s<i;s++)e(t[s],s)}e.version="1.0.0";class Raj extends Array{selector;constructor(t,e){if(void 0===t.length)t=[t];else t=[...t];return super(...t),this.context=window.document,this.selector=e,this}hide(){return s(this,(t=>{t[e.expando]={display:t.style.display},t.style.setProperty("display","none")})),this}show(){return s(this,(t=>{t.style.setProperty("display",t[e.expando].display)})),this}eq(t){return new Raj(this[(t%this.length+this.length)%this.length],this.selector)}first(){return this.eq(0)}last(){return this.eq(-1)}even(){return this.filter(((t,e)=>e%2!=0))}odd(){return this.filter(((t,e)=>e%2==0))}not(t){var s=e(t);return this.filter((t=>!s.includes(t)))}addClass(t){return s(this,(e=>{e.classList.add(t)})),this}removeClass(t){return s(this,(e=>{e.classList.remove(t)})),this}toggleClass(t){return s(this,(e=>{e.classList.toggle(t)})),this}on(t,e,i){s(this,(s=>{s.addEventListener(t,e,i)}))}off(t,e,i){void 0===i&&s(this,(t=>{}))}click(t,...e){void 0===t&&s(this,(t=>{t.click()})),this.on("click",t,...e)}after(...t){var e=[...t].reverse().join("");return s(this,(t=>{t.insertAdjacentHTML("afterend",e)})),this}append(...t){var e=[...t].join("");return s(this,(t=>{t.insertAdjacentHTML("beforeend",e)})),this}prepend(...t){var e=[...t].reverse().join("");return s(this,(t=>{t.insertAdjacentHTML("afterbegin",e)})),this}before(...t){var e=[...t].join("");return s(this,(t=>{t.insertAdjacentHTML("beforebegin",e)})),this}attr(t,e){if(void 0===e){if("object"!=typeof t)return this[0].getAttribute(t);var i=Object.values(t);s(Object.keys(t),((t,e)=>{this[0].setAttribute(t,i[e])}))}if("function"!=typeof e)return this[0].setAttribute(t,e);s(this,((s,i)=>{e(i,s.getAttribute(t))}))}css(t,e){if(void 0===e){if("object"!=typeof t)return getComputedStyle(this[0])[t];var i=Object.values(t);s(Object.keys(t),((t,e)=>{this[0].style.setProperty(t,i[e])}))}if("function"!=typeof e)return this[0].style.setProperty(t,e);s(this,((s,i)=>{e(i,getComputedStyle(s)[t])}))}empty(){return s(this,((t,e)=>{t.innerHTML=null})),this}hasClass(t){return this[0].classList.contains(t)}height(){return parseFloat(getComputedStyle(this[0]).height)}outerHeight(t){return t?this.height()+parseInt(this.css("margin-top"))+parseInt(this.css("margin-bottom")):this[0].offsetHeight}width(){return parseFloat(getComputedStyle(this[0]).width)}outerWidth(t){return t?this.width()+parseInt(this.css("margin-left"))+parseInt(this.css("margin-right")):this[0].offsetWidth}data(t,s){return e.data(this[0],t,s)}each(t){e.each(this,t)}}e.expando="raj"+Date.now(),document[e.expando]={},e.extend=(t,...s)=>void 0===[...s][0]?Object.assign(e,t):Object.assign(t,...s),e.noConflict=t=>{switch(t){case!0:window.$=window.jQuery=window.raj=void 0;break;case!1:window.$=void 0}return e},e.data=(t,s,i)=>{if(void 0!==i){if(t[e.expando]=++e.cache.latest,void 0===e.cache[t[e.expando]])e.cache[t[e.expando]]={};return e.cache[t[e.expando]][s]=i}if(void 0===e.cache[t[e.expando]])e.cache[t[e.expando]]={};switch(typeof s){case"string":return e.cache[t[e.expando]][s];case"object":return t[e.expando]=++e.cache.latest,0===Object.keys(e.cache[t[e.expando]]).length?e.cache[t[e.expando]]=s:Object.assign(e.cache[t[e.expando]],s)}},e.removeData=(t,s)=>{if(void 0===s)e.cache[t[e.expando]]=void 0;e.cache[t[e.expando]][s]=void 0},e.when=async(...t)=>Object.assign(...t),e.cache={latest:0},e.now=Date.now,e.parseJSON=JSON.parse,e.inArray=(t,e)=>e.indexOf(t),e.isArray=t=>Array.isArray(t),e.isFunction=t=>"function"==typeof t,e.each=(t,e)=>{s(t,((t,s)=>{e(s,t)}))},window.raj=window.jQuery=window.$=e})();
//# sourceMappingURL=raj.min.js.map