diff --git a/dist/uDSV.cjs.js b/dist/uDSV.cjs.js index 5892ace..b1624ee 100644 --- a/dist/uDSV.cjs.js +++ b/dist/uDSV.cjs.js @@ -4,7 +4,7 @@ * * uDSV.js * A small, fast CSV parser -* https://github.com/leeoniya/uDSV (v0.5.0) +* https://github.com/leeoniya/uDSV (v0.5.1) */ 'use strict'; diff --git a/dist/uDSV.iife.js b/dist/uDSV.iife.js index c2d2592..04d542f 100644 --- a/dist/uDSV.iife.js +++ b/dist/uDSV.iife.js @@ -4,7 +4,7 @@ * * uDSV.js * A small, fast CSV parser -* https://github.com/leeoniya/uDSV (v0.5.0) +* https://github.com/leeoniya/uDSV (v0.5.1) */ var uDSV = (function (exports) { diff --git a/dist/uDSV.iife.min.js b/dist/uDSV.iife.min.js index dcdb08a..1b6033e 100644 --- a/dist/uDSV.iife.min.js +++ b/dist/uDSV.iife.min.js @@ -1,2 +1,2 @@ -/*! https://github.com/leeoniya/uDSV (v0.5.0) */ +/*! https://github.com/leeoniya/uDSV (v0.5.1) */ var uDSV=function(e){"use strict";const t=/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3,})?(?:Z|[-+]\d{2}:?\d{2})$/,l=/^(?:t(?:rue)?|f(?:alse)?|y(?:es)?|n(?:o)?|0|1)$/i,n=["\t","|",";",","],r="d",i="n",s="j",c="b";const o=JSON.stringify,f=e=>"string"==typeof e?o(e):e;function u(e,t){let{type:l}=t,n=`r[${e}]`,u=l===r?`new Date(${n})`:l===s?`JSON.parse(${n})`:l===i?"+"+n:l[0]===c?`${n} === ${o(l.slice(2))} ? true : false`:n,{repl:a}=t,h=void 0!==a.NaN&&l===i?`${n} === 'NaN' ? ${f(a.NaN)} : `:"",d=void 0!==a.null?`${n} === 'null' || ${n} === 'NULL' ? ${f(a.null)} : `:"";return`${void 0!==a.empty?`${n} === '' ? ${f(a.empty)} : `:""} ${d} ${h} ${u}`}const a=/\w+(?:\[|\]?[\.\[]?|$)/gm;function h(e,t=!1,l=!1){let n="";if(t&&l){let t={},l=0,r=e.map((e=>e.name));do{let e=r.shift(),n=/\s/.test(e)?[e]:[...e.matchAll(a)].flatMap((e=>e.map((e=>e.replace("]",""))))),i=t;do{let e=n.shift(),t=e,r=e.at(-1);"."==r||"["==r?(t=e.slice(0,-1),i=i[t]=i[t]??("."==r?{}:[])):i[t]=`¦${l}¦`}while(n.length>0);l++}while(r.length>0);n=o(t).replace(/"¦(\d+)¦"/g,((t,l)=>u(+l,e[+l])))}else!t&&e.every((e=>"s"===e.type))?n="r":(n=t?"{":"[",e.forEach(((e,l)=>{n+=t?o(e.name)+":":"";let r=u(l,e);n+=r+","})),n+=t?"}":"]");return Function("rows",` let arr = Array(rows.length); for (let i = 0; i < rows.length; i++) { let r = rows[i]; arr[i] = ${n}; } return arr; `)}function d(e,t,l,n=0,r=!0,i=1e3,s=null,c=null){let{row:o,col:f,encl:u,esc:a,trim:h}=t,d=c||t.cols.length,p=null!=s,g=null!=c&&p,$=o.length,y=f.length,m=u.charCodeAt(0),A=a.charCodeAt(0),N=o.charCodeAt(0),k=f.charCodeAt(0),w=0,C=0,b=e.length-1,O=0,x=[],v=Array(d).fill(""),E=v.slice(),S=0,L=d-1,T=-1;if(""===u){for(;b>=C;)if(S===L){let t=e.indexOf(o,C);if(-1===t){if(!r)break;t=b+1}let c=e.slice(C,t);if(E[S]=h?c.trim():c,0>--n&&x.push(E),x.length===i){let e=!1===l(x,"");if(x=[],e||p&&++w===s)return}E=v.slice(),S=0,T=-1,C=t+$,O=C}else if(0===S&&e.charCodeAt(C)===N)C+=$;else{let t=e.indexOf(f,C);if(-1===t&&!r)break;let l=e.slice(C,t);E[S]=h?l.trim():l,C=t+y,T=S++}return r&&S===L&&0>--n&&x.push(E),void(r&&0>=x.length||l(x,r?"":e.slice(O)))}const j=g?RegExp(`[^${f}${o}]+`,"my"):null;let R,Y=0,D="";for(;b>=C;){if(R=e.charCodeAt(C),0===Y)if(R===m){if(Y=2,C+=1,C>b)break;R=e.charCodeAt(C)}else if(R===k||R===N){if(R===N&&0===S){C+=$;continue}if(E[S]=D,T=S,S+=1,C+=1,D="",R===N){if(g&&L>T&&0===x.length&&(E.length=v.length=T+1,L=T),0>--n&&x.push(E),x.length===i){let e=!1===l(x,"");if(x=[],e||p&&++w===s)return}E=v.slice(),S=0,T=-1,C+=$-1,O=C}if(C>b)break;R=e.charCodeAt(C)}else if(h&&32===R)for(;32===R;)R=e.charCodeAt(++C);else Y=1;if(2===Y)for(;;)if(R===m)if(m===A){if(C+1>b){C=b+1;break}if(e.charCodeAt(C+1)!==m){Y=0,C+=1;break}if(C+=2,D+=u,C>b)break;R=e.charCodeAt(C)}else{if(e.charCodeAt(C-1)!==A){Y=0,C+=1;break}if(C+=1,D+=u,C>b)break;R=e.charCodeAt(C)}else{let t=e.indexOf(u,C);if(-1===t){C=b+1;break}D+=e.slice(C,m===A?t:t-1),C=t,R=m}else if(1===Y)if(R===k||R===N){if(R===N&&0===S){C+=$;continue}if(E[S]=D,T=S,S+=1,C+=1,D="",R===N){if(g&&L>T&&0===x.length&&(E.length=v.length=T+1,L=T),0>--n&&x.push(E),x.length===i){let e=!1===l(x,"");if(x=[],e||p&&++w===s)return}E=v.slice(),S=0,T=-1,C+=$-1,O=C}Y=0}else if(g){j.lastIndex=C;let t=j.exec(e)[0];D+=t,C+=t.length}else{let t=e.indexOf(S===L?o:f,C);-1===t&&(t=b+1);let l=e.slice(C,t);D+=h?l.trim():l,C=t}}r&&S===L&&(E[S]=D,0>--n&&x.push(E),Y=0),l(x,r||0===Y&&!(-1===T?""!==D:L>T)?"":e.slice(O))}return e.inferSchema=function(e,o,f){let{header:u,col:a,row:h,encl:p,esc:g,trim:$=!1}=o??{};u??=e=>[e[0]],f??=10;const y=RegExp(`(.*)(${h??"\r\n|\r|\n"})`),m=e.match(y),A=m[1];h??=m[2],a??=n.find((e=>A.indexOf(e)>-1))??"",p??=e.indexOf('"')>-1?'"':"",g??=p;const N={skip:1,col:a,row:h,encl:p,esc:g,trim:$,cols:[]},k=A.split(a).length,w=[];d(e,N,(e=>{w.push(...e)}),0,!0,f,1,k);let C=u(w)??[],b=N.skip=C.length,O=C.find((e=>null!=e))??[...Array(w[0].length).keys()];return w.splice(0,b),O.forEach(((e,n)=>{let o=function(e,n){let o=n.findLast((t=>""!==t[e]&&"null"!==t[e]&&"NULL"!==t[e]&&"NaN"!==t[e])),f="s";if(null!=o){let n=o[e];f=t.test(n)?r:+n==+n?i:l.test(n)?c+":"+function(e){let[t,l=""]=e;return"1"==t||"0"==t?"1":"t"==t||"f"==t?""==l?"t":"true":"T"==t||"F"==t?""==l?"T":"R"==l||"A"===l?"TRUE":"True":"y"==t||"n"==t?""==l?"y":"yes":"Y"==t||"N"==t?""==l?"Y":"E"==l||"O"===l?"YES":"Yes":""}(n):function(e){if("["===e[0]||"{"===e[0])try{return JSON.parse(e),!0}catch{}return!1}(n)?s:f}return f}(n,w);N.cols.push({name:e,type:o,repl:{empty:null,NaN:void 0,null:void 0}})})),N},e.initParser=function(e,t){let{skip:l,cols:n}=e,r=null,i=null,s=null,c=null,o=null,f=0,u=0,a=null,p=null,g="",$="",y=null;function m(){u=f=0,$=g="",a=p=y=null}let A=(e,t)=>{t(e)},N=()=>[],k=e=>{y.push(...e)};function w(n,r,i){let s=null;return(c,o=A)=>{s??=i(),y??=n();let a=y,h=0===u||2===u;return d(c,e,((e,t)=>{$=t;let l=o(s(e),r);return!1===l&&0!==u&&m(),l}),0===f?l:0,h,t),h&&(y=null),a}}const C=w(N,k,(()=>(r??=e=>e,r))),b=w(N,k,(()=>(i??=h(n,!1,!1),i))),O=w(N,k,(()=>(s??=h(n,!0,!1),s))),x=w(N,k,(()=>(c??=h(n,!0,!0),c))),v=w((()=>n.map((()=>[]))),(e=>{e.forEach(((e,t)=>{y[t].push(...e)}))}),(()=>(i??=h(n,!1,!1),o??=function(e){return Function("rows",` let cols = [${e.map((()=>"Array(rows.length)")).join(",")}]; for (let i = 0; i < rows.length; i++) { let r = rows[i]; ${e.map(((e,t)=>`cols[${t}][i] = r[${t}]`)).join(";")}; } return cols; `)}(n),e=>o(i(e)))));return{schema:e,stringArrs:C,typedArrs:b,typedObjs:O,typedDeep:x,typedCols:v,chunk(e,t=C,l=A){a??=t,p??=l,1===u&&(a($+g,p),f++),g=e,u=1},end(){u=2;let e=a($+g,p);return m(),e}}},e}({}); diff --git a/dist/uDSV.mjs b/dist/uDSV.mjs index 8828a3b..8006d99 100644 --- a/dist/uDSV.mjs +++ b/dist/uDSV.mjs @@ -4,7 +4,7 @@ * * uDSV.js * A small, fast CSV parser -* https://github.com/leeoniya/uDSV (v0.5.0) +* https://github.com/leeoniya/uDSV (v0.5.1) */ const comma = ','; diff --git a/package.json b/package.json index d8dfa98..bb33285 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "udsv", - "version": "0.5.0", + "version": "0.5.1", "description": "A small, fast CSV parser", "main": "./dist/uDSV.cjs.js", "module": "./dist/uDSV.mjs",