-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.min.js
1 lines (1 loc) · 2.95 KB
/
index.min.js
1
function l(t,r,n=e=>e){let e=Object.create(null);e.options=r||{},e.reviver=n,e.value="",e.entry=[],e.output=[],e.col=1,e.row=1;let h=/"|,|\r\n|\n|\r|[^",\r\n]+/y,i=/^(\r\n|\n|\r)$/,o=[],s="",a=0;for(;(o=h.exec(t))!==null;)switch(s=o[0],a){case 0:switch(!0){case s==='"':a=3;break;case s===",":a=0,c(e);break;case i.test(s):a=0,c(e),u(e);break;default:e.value+=s,a=2;break}break;case 2:switch(!0){case s===",":a=0,c(e);break;case i.test(s):a=0,c(e),u(e);break;default:throw a=4,Error(`CSVError: Illegal state [row:${e.row}, col:${e.col}]`)}break;case 3:switch(!0){case s==='"':a=4;break;default:a=3,e.value+=s;break}break;case 4:switch(!0){case s==='"':a=3,e.value+=s;break;case s===",":a=0,c(e);break;case i.test(s):a=0,c(e),u(e);break;default:throw Error(`CSVError: Illegal state [row:${e.row}, col:${e.col}]`)}break}return e.entry.length!==0&&(c(e),u(e)),e.output}function c(t){let r=t.options.typed?p(t.value):t.value;t.entry.push(t.reviver(r,t.row,t.col)),t.value="",t.col++}function u(t){t.output.push(t.entry),t.entry=[],t.row++,t.col=1}function p(t){let r=/.\./;switch(!0){case t==="true":case t==="false":return t==="true";case r.test(t):return parseFloat(t);case isFinite(t):return parseInt(t);default:return t}}var d=class extends HTMLElement{static get observedAttributes(){return["src","no-headers"]}attributeChangedCallback(r,n,e){!this.__initialized||n!==e&&(r==="no-headers"?this.noHeaders=e:this[r]=e)}get src(){return this.getAttribute("src")}set src(r){this.setAttribute("src",r),this.setSrc(r)}get value(){return this.__data}set value(r){this.setValue(r)}get noHeaders(){return this.hasAttribute("no-headers")}set noHeaders(r){let n=this.hasAttribute("no-headers");n?this.setAttribute("no-headers",""):this.removeAttribute("no-headers"),this.setNoHeaders(n)}constructor(){super();this.__initialized=!1,this.__headers=!0,this.__data=[],this.__table=document.createElement("table"),this.appendChild(this.__table)}async connectedCallback(){this.hasAttribute("no-headers")&&(this.__headers=!1),this.hasAttribute("src")&&this.setSrc(),this.__initialized=!0}async setSrc(){if(this.hasAttribute("src")){let r=await this.fetchSrc(this.src);this.__data=l(r),this.render()}}async fetchSrc(r){let n=await fetch(r);if(n.status!==200)throw Error(`ERR ${n.status}: ${n.statusText}`);return n.text()}setValue(r){this.__data=l(r),this.render()}setNoHeaders(r){this.__headers=!r,this.render()}render(){let r=[...this.__data],n=document.createElement("table");if(this.__headers){let h=r.shift(),i=document.createElement("thead"),o=document.createElement("tr");h.forEach(s=>{let a=document.createElement("th");a.innerText=s,o.appendChild(a)}),i.append(o),n.appendChild(i)}let e=document.createElement("tbody");r.forEach(h=>{let i=document.createElement("tr");h.forEach(o=>{let s=document.createElement("td");s.innerText=o,i.appendChild(s)}),e.appendChild(i)}),n.appendChild(e),this.removeChild(this.__table),this.__table=n,this.appendChild(this.__table)}};customElements.define("wc-csv-table",d);export{d as WCCSVTable};