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