web-csv-toolbox 0.3.2 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.umd.js CHANGED
@@ -1 +1 @@
1
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).CSV={})}(this,(function(e){"use strict";const t=Symbol.for("web-streams-csv.FieldDelimiter"),i=Symbol.for("web-streams-csv.RecordDelimiter"),r=Symbol.for("web-streams-csv.Field"),n="\r\n",s="\n";function o(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}class a extends TransformStream{#e;#t;#i;#r;#n;#s="";get demiliter(){return this.#e}get quotation(){return this.#i}constructor({demiliter:e=",",quotation:t='"'}={}){!function(e){if("string"==typeof e.quotation&&0===e.quotation.length)throw new Error("quotation must not be empty");if("string"==typeof e.demiliter&&0===e.demiliter.length)throw new Error("demiliter must not be empty");if(e.quotation.includes(s)||e.quotation.includes("\r"))throw new Error("quotation must not include CR or LF");if(e.demiliter.includes(s)||e.demiliter.includes("\r"))throw new Error("demiliter must not include CR or LF");if(e.demiliter.includes(e.quotation)||e.quotation.includes(e.demiliter))throw new Error("demiliter and quotation must not include each other as a substring")}({demiliter:e,quotation:t}),super({transform:(e,t)=>{if(0!==e.length){this.#s+=e;for(const e of this.#o({flush:!1}))t.enqueue(e)}},flush:e=>{for(const t of this.#o({flush:!0}))e.enqueue(t)}}),this.#e=e,this.#t=e.length,this.#i=t,this.#r=t.length;const i=o(e),r=o(t);this.#n=new RegExp(`^(?:(?!${r})(?!${i})(?![\\r\\n]))([\\S\\s\\uFEFF\\xA0]+?)(?=${r}|${i}|\\r|\\n|$)`)}*#o({flush:e}){let n=null;for(let s;s=this.#a({flush:e});)switch(s.type){case r:n?n.value+=s.value:n=s;break;case t:case i:n&&(yield n,n=null),yield s}n&&(yield n)}#a({flush:e=!1}={}){if(0===this.#s.length)return null;if(this.#s.startsWith(n))return this.#s=this.#s.slice(2),{type:i,value:n};if(this.#s.startsWith(s))return this.#s=this.#s.slice(1),{type:i,value:s};if(this.#s.startsWith(this.#e))return this.#s=this.#s.slice(this.#t),{type:t,value:this.#e};if(this.#s.startsWith(this.#i))return!1===e&&this.#s.endsWith(this.#i)?null:this.extractQuotedString(e);const o=this.#n.exec(this.#s);return o?!1===e&&o[0].length===this.#s.length?null:(this.#s=this.#s.slice(o[0].length),{type:r,value:o[0]}):null}extractQuotedString(e){let t=this.#r,i="";for(;t<this.#s.length;)if(this.#s.slice(t,t+this.#r)!==this.quotation||this.#s.slice(t+this.#r,t+2*this.#r)!==this.quotation){if(this.#s.slice(t,t+this.#r)===this.quotation)return!1===e&&t+this.#r<this.#s.length&&this.#s.slice(t+this.#r,this.#t)!==this.demiliter&&this.#s.slice(t+this.#r,t+this.#r+2)!==n&&this.#s.slice(t+this.#r,t+this.#r+1)!==s?null:(this.#s=this.#s.slice(t+this.#r),{type:r,value:i});i+=this.#s[t],t++}else i+=this.quotation,t+=2*this.#r;return null}}class h extends TransformStream{#h=0;#u=[];#l;#f=!1;constructor(e={}){super({transform:(e,n)=>{switch(e.type){case r:this.#f=!0,this.#u[this.#h]=e.value;break;case t:this.#h++;break;case i:if(void 0===this.#l)this.#c(this.#u);else if(this.#f){const e=Object.fromEntries(this.#l.filter((e=>e)).map(((e,t)=>[e,this.#u.at(t)])));n.enqueue(e)}this.#h=0,this.#u=new Array(this.#l?.length),this.#f=!1}},flush:e=>{if(0!==this.#h&&void 0!==this.#l&&this.#f){const t=Object.fromEntries(this.#l.filter((e=>e)).map(((e,t)=>[e,this.#u.at(t)])));e.enqueue(t)}}}),void 0!==e.header&&Array.isArray(e.header)&&this.#c(e.header)}#c(e){if(this.#l=e,0===this.#l.length)throw new Error("The header must not be empty.");if(new Set(this.#l).size!==this.#l.length)throw new Error("The header must not contain duplicate fields.")}}class u extends ReadableStream{constructor(e){super({start(t){t.enqueue(e),t.close()}})}}async function l(...e){const t=[];for await(const i of this(...e))t.push(i);return t}async function*f(e,t){let i;const r=new ReadableStream({start:e=>i=e});await e.pipeThrough(new a(t)).pipeThrough(new h(t)).pipeTo(new WritableStream({write:e=>i.enqueue(e),close:()=>i.close()}));const n=r.getReader();try{for(;;){const{value:e,done:t}=await n.read();if(t)break;yield e}}finally{n.releaseLock()}}async function*c(e,t){yield*f(new u(e),t)}async function*d(e,t){const{charset:i,fatal:r,ignoreBOM:n,decomposition:s}=t??{};yield*f([...s?[new DecompressionStream(s)]:[],new TextDecoderStream(i,{fatal:r,ignoreBOM:n})].reduce(((e,t)=>e.pipeThrough(t)),e),t)}function m(e,t){const{headers:i}=e,r=i.get("content-type")??"text/csv",n=function(e){const[t,...i]=e.split(";"),r={type:t.trim(),parameters:{}};for(const e of i){const[t,i]=e.split("=");r.parameters[t.trim()]=i.trim()}return r}(r);if("text/csv"!==n.type)throw new Error(`Invalid mime type: ${r}`);const s=i.get("content-encoding")??void 0,o=n.parameters.charset??"utf-8";if(null===e.body)throw new Error("Response body is null");return d(e.body,{decomposition:s,charset:o,...t})}async function*y(e,t){const[i,r]=e.tee(),n=i.getReader(),{value:s}=await n.read();n.releaseLock(),"string"==typeof s?yield*f(r,t):s instanceof Uint8Array&&(yield*d(r,t))}async function*p(e,t){"string"==typeof e?yield*c(e,t):e instanceof ReadableStream?yield*y(e,t):e instanceof Response&&(yield*m(e,t))}!function(e){e.toArray=l}(f||(f={})),function(e){e.toArray=l}(c||(c={})),function(e){e.toArray=l}(d||(d={})),function(e){e.toArray=l}(m||(m={})),function(e){e.toArray=l}(y||(y={})),function(e){e.toArray=l}(p||(p={})),e.Field=r,e.FieldDelimiter=t,e.LexerTransformer=a,e.RecordAssemblerTransformar=h,e.RecordDelimiter=i,e.parse=p,e.parseBinaryStream=d,e.parseResponse=m,e.parseStream=y,e.parseString=c,e.parseStringStream=f}));
1
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).CSV={})}(this,(function(e){"use strict";const t=Symbol.for("web-csv-toolbox.FieldDelimiter"),r=Symbol.for("web-csv-toolbox.RecordDelimiter"),i=Symbol.for("web-csv-toolbox.Field"),n="\r\n",o="\n";function s(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}class a{#e;#t;#r;#i;#n;#o="";#s=!1;constructor({delimiter:e=",",quotation:t='"'}={}){!function(e){if("string"==typeof e.quotation&&0===e.quotation.length)throw new Error("quotation must not be empty");if("string"==typeof e.delimiter&&0===e.delimiter.length)throw new Error("delimiter must not be empty");if(e.quotation.includes(o)||e.quotation.includes("\r"))throw new Error("quotation must not include CR or LF");if(e.delimiter.includes(o)||e.delimiter.includes("\r"))throw new Error("delimiter must not include CR or LF");if(e.delimiter.includes(e.quotation)||e.quotation.includes(e.delimiter))throw new Error("delimiter and quotation must not include each other as a substring")}({delimiter:e,quotation:t}),this.#e=e,this.#t=e.length,this.#r=t,this.#i=t.length;const r=s(e),i=s(t);this.#n=new RegExp(`^(?:(?!${i})(?!${r})(?![\\r\\n]))([\\S\\s\\uFEFF\\xA0]+?)(?=${i}|${r}|\\r|\\n|$)`)}lex(e,t=!1){return t||(this.#s=!0),"string"==typeof e&&0!==e.length&&(this.#o+=e),this.#a()}flush(){return this.#s=!0,[...this.#a()]}*#a(){this.#s&&(this.#o.endsWith(n)?this.#o=this.#o.slice(0,-2):this.#o.endsWith(o)&&(this.#o=this.#o.slice(0,-1)));let e=null;for(let i;i=this.#u();)switch(i){case t:case r:e&&(yield e,e=null),yield i;break;default:e?e.value+=i.value:e=i}e&&(yield e)}#u(){if(0===this.#o.length)return null;if(!1===this.#s&&(this.#o===n||this.#o===o))return null;if(this.#o.startsWith(n))return this.#o=this.#o.slice(2),r;if(this.#o.startsWith(o))return this.#o=this.#o.slice(1),r;if(this.#o.startsWith(this.#e))return this.#o=this.#o.slice(this.#t),t;if(this.#o.startsWith(this.#r))return!1===this.#s&&this.#o.endsWith(this.#r)?null:this.#l();const e=this.#n.exec(this.#o);return e?!1===this.#s&&e[0].length===this.#o.length?null:(this.#o=this.#o.slice(e[0].length),{type:i,value:e[0]}):null}#l(){let e=this.#i,t="";for(;e<this.#o.length;)if(this.#o.slice(e,e+this.#i)!==this.#r||this.#o.slice(e+this.#i,e+2*this.#i)!==this.#r){if(this.#o.slice(e,e+this.#i)===this.#r)return!1===this.#s&&e+this.#i<this.#o.length&&this.#o.slice(e+this.#i,this.#t)!==this.#e&&this.#o.slice(e+this.#i,e+this.#i+2)!==n&&this.#o.slice(e+this.#i,e+this.#i+1)!==o?null:(this.#o=this.#o.slice(e+this.#i),{type:i,value:t});t+=this.#o[e],e++}else t+=this.#r,e+=2*this.#i;return null}}class u extends TransformStream{constructor(e={}){const t=new a(e);super({transform:(e,r)=>{0!==e.length&&r.enqueue([...t.lex(e,!0)])},flush:e=>{e.enqueue(t.flush())}})}}class l{#f=0;#h=[];#c;#d=!1;constructor(e={}){void 0!==e.header&&Array.isArray(e.header)&&this.#b(e.header)}*assemble(e,i=!0){for(const i of e)switch(i){case t:this.#f++,this.#d=!0;break;case r:void 0===this.#c?this.#b(this.#h):this.#d?yield Object.fromEntries(this.#c.map(((e,t)=>[e,this.#h.at(t)]))):yield Object.fromEntries(this.#c.map((e=>[e,""]))),this.#f=0,this.#h=new Array(this.#c?.length).fill(""),this.#d=!1;break;default:this.#d=!0,this.#h[this.#f]=i.value}i&&(yield*this.flush())}*flush(){void 0!==this.#c&&this.#d&&(yield Object.fromEntries(this.#c.filter((e=>e)).map(((e,t)=>[e,this.#h.at(t)]))))}#b(e){if(this.#c=e,0===this.#c.length)throw new Error("The header must not be empty.");if(new Set(this.#c).size!==this.#c.length)throw new Error("The header must not contain duplicate fields.")}}class f extends TransformStream{constructor(e={}){const t=new l(e);super({transform:(e,r)=>{for(const i of t.assemble(e,!1))r.enqueue(i)},flush:e=>{for(const r of t.flush())e.enqueue(r)}})}}function h(e,t){const r=new a(t),i=new l(t),n=r.lex(e);return[...i.assemble(n)]}function c(e,t){const r=new a(t),i=new l(t),n=r.lex(e);return i.assemble(n)}function d(e,t){const r=new a(t),i=new l(t);return new ReadableStream({start(t){const n=r.lex(e);for(const e of i.assemble(n))t.enqueue(e);t.close()}})}async function b(...e){const t=[];for await(const r of this(...e))t.push(r);return t}async function*m(e,t){yield*c(e,t)}function y(e,t){return new TextDecoder(t?.charset,{ignoreBOM:t?.ignoreBOM,fatal:t?.fatal}).decode(e instanceof ArrayBuffer?new Uint8Array(e):e)}function w(e,t={}){return h(y(e,t),t)}function p(e,t={}){return c(y(e,t),t)}function g(e,t={}){return d(y(e,t),t)}function q(e,t){return function(e){return{async next(){const t=e.next();return Promise.resolve(t)},[Symbol.asyncIterator](){return this}}}(p(e,t))}function v(e,...t){return new ReadableStream({start:r=>{t.reduce(((e,t)=>e.pipeThrough(t)),e).pipeTo(new WritableStream({write:e=>r.enqueue(e),close:()=>r.close()}))}})}function x(e,t){const{charset:r,fatal:i,ignoreBOM:n,decomposition:o}=t??{};return o?v(e,new DecompressionStream(o),new TextDecoderStream(r,{fatal:i,ignoreBOM:n}),new u(t),new f(t)):v(e,new TextDecoderStream(r,{fatal:i,ignoreBOM:n}),new u(t),new f(t))}async function*S(e){const t=e.getReader();for(;;){const{done:e,value:r}=await t.read();if(e)break;yield r}}function L(e,t){return S(x(e,t))}function O(e,t){return v(e,new u(t),new f(t))}function j(e,t){return S(O(e,t))}function A(e,t={}){const{headers:r}=e,i=r.get("content-type")??"text/csv",n=function(e){const[t,...r]=e.split(";"),i={type:t.trim(),parameters:{}};for(const e of r){const[t,r]=e.split("=");i.parameters[t.trim()]=r.trim()}return i}(i);if("text/csv"!==n.type)throw new Error(`Invalid mime type: ${i}`);return{decomposition:r.get("content-encoding")??void 0,charset:n.parameters.charset??"utf-8",...t}}function P(e,t){const r=A(e,t);if(null===e.body)throw new Error("Response body is null");return x(e.body,r)}function E(e,t){const r=A(e,t);if(null===e.body)throw new Error("Response body is null");return L(e.body,r)}async function*R(e,t){if("string"==typeof e)yield*m(e,t);else if(e instanceof Uint8Array||e instanceof ArrayBuffer)yield*q(e,t);else if(e instanceof ReadableStream){const[r,i]=e.tee(),n=r.getReader(),{value:o}=await n.read();n.releaseLock(),"string"==typeof o?yield*j(i,t):o instanceof Uint8Array&&(yield*L(i,t))}else e instanceof Response&&(yield*E(e,t))}!function(e){Object.defineProperty(e,"toArray",{enumerable:!0,writable:!1,value:b}),Object.defineProperty(e,"toArraySync",{enumerable:!0,writable:!1,value:h}),Object.defineProperty(e,"toIterableIterator",{enumerable:!0,writable:!1,value:c}),Object.defineProperty(e,"toStream",{enumerable:!0,writable:!1,value:d})}(m||(m={})),function(e){Object.defineProperty(e,"toArray",{enumerable:!0,writable:!1,value:b}),Object.defineProperty(e,"toArraySync",{enumerable:!0,writable:!1,value:w}),Object.defineProperty(e,"toIterableIterator",{enumerable:!0,writable:!1,value:p}),Object.defineProperty(e,"toStream",{enumerable:!0,writable:!1,value:g})}(q||(q={})),function(e){Object.defineProperty(e,"toArray",{enumerable:!0,writable:!1,value:b}),Object.defineProperty(e,"toStream",{enumerable:!0,writable:!1,value:x})}(L||(L={})),function(e){Object.defineProperty(e,"toArray",{enumerable:!0,writable:!1,value:b}),Object.defineProperty(e,"toStream",{enumerable:!0,writable:!1,value:O})}(j||(j={})),function(e){Object.defineProperty(e,"toArray",{enumerable:!0,writable:!1,value:b}),Object.defineProperty(e,"toStream",{enumerable:!0,writable:!1,value:P})}(E||(E={})),function(e){Object.defineProperty(e,"toArray",{enumerable:!0,writable:!1,value:b})}(R||(R={})),e.Field=i,e.FieldDelimiter=t,e.LexerTransformer=u,e.RecordAssemblerTransformer=f,e.RecordDelimiter=r,e.parse=R,e.parseBinary=q,e.parseResponse=E,e.parseString=m,e.parseStringStream=j,e.parseUint8ArrayStream=L}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "web-csv-toolbox",
3
- "version": "0.3.2",
3
+ "version": "0.5.0",
4
4
  "description": "A CSV Toolbox utilizing Web Standard APIs.",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",