generated from jsonicjs/directive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv.min.js
1 lines (1 loc) · 4.6 KB
/
csv.min.js
1
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).JsonicCsv=e()}}((function(){var e={};Object.defineProperty(e,"__esModule",{value:!0}),e.Csv=void 0,e.buildCsvStringMatcher=t;const n=(e,n)=>{var r;const l=!!n.strict,o=!!n.object,s=!!n.header;let i=!!n.trim,d=!!n.comment,c=!!n.number,u=!!n.value,a=!!(null===(r=n.record)||void 0===r?void 0:r.empty);const f=n.stream;if(l?(!1!==n.string.csv&&e.options({lex:{match:{stringcsv:{order:1e5,make:t(n)}}}}),e.options({rule:{exclude:"jsonic,imp"}})):(!0===n.string.csv&&e.options({lex:{match:{stringcsv:{order:1e5,make:t(n)}}}}),i=null===n.trim||i,d=null===n.comment||d,c=null===n.number||c,u=null===n.value||u,e.options({rule:{exclude:"imp"}})),f){let n=e.internal().parser,t=n.start.bind(n);n.start=(...e)=>{try{return t(...e)}catch(n){f("error",n)}}}let p={};l&&(p={"#OB":null,"#CB":null,"#OS":null,"#CS":null,"#CL":null}),n.field.separation&&(p["#CA"]=n.field.separation);let v=e.tokenSet.VAL,m={rule:{start:"csv"},fixed:{token:p},tokenSet:{IGNORE:[l?null:void 0,null,void 0]},number:{lex:c},value:{lex:u},comment:{lex:d},lex:{emptyResult:[]},line:{single:a,chars:null==n.record.separators?void 0:n.record.separators,rowChars:null==n.record.separators?void 0:n.record.separators},error:{csv_extra_field:"unexpected extra field value: $fsrc",csv_missing_field:"missing field"},hint:{csv_extra_field:"Row $row has too many fields (the first of which is: $fsrc). Only $len\nfields per row are expected.",csv_missing_field:"Row $row has too few fields. $len fields per row are expected."}};e.options(m);let{LN:x,CA:h,SP:b,ZZ:g}=e.token;e.rule("csv",e=>(e.bo((e,n)=>{n.u.recordI=0,f&&f("start"),e.node=[]}).open([{s:[g]},a?null:{s:[x],p:"newline"},{p:"record"}]).ac(()=>{f&&f("end")}),e)),e.rule("newline",e=>{e.open([{s:[x,x],r:"newline"},{s:[x],r:"newline"},{s:[g]},{r:"record"}]).close([{s:[x,x],r:"newline"},{s:[x],r:"newline"},{s:[g]},{r:"record"}])}),e.rule("record",e=>(e.open([{p:"list"}]).close([{s:[g]},{s:[x,g]},{s:[x],r:a?"record":"newline"}]).bc((e,t)=>{let r=t.u.fields||n.field.names;if(0===t.u.recordI&&s)t.u.fields=void 0===e.child.node?[]:e.child.node;else{let l=e.child.node||[];if(o){let e={},o=0;if(r){if(n.field.exact&&l.length!==r.length)return t.t0.bad(l.length>r.length?"csv_extra_field":"csv_missing_field");let s=0;for(;s<r.length;s++)e[r[s]]=void 0===l[s]?n.field.empty:l[s];o=s}for(;o<l.length;o++)e[n.field.nonameprefix+o]=void 0===l[o]?n.field.empty:l[o];l=e}else for(let e=0;e<l.length;e++)l[e]=void 0===l[e]?n.field.empty:l[e];f?f("record",l):e.node.push(l)}t.u.recordI++}),e)),e.rule("list",e=>e.open([{s:[x],b:1}]).close([{s:[x],b:1},{s:[g]}])),e.rule("elem",e=>e.open([{s:[h],b:1,a:e=>{e.node.push(n.field.empty),e.u.done=!0}}]).close([{s:[h,[x,g]],b:1,a:e=>e.node.push(n.field.empty)},{s:[x],b:1}])),e.rule("val",e=>e.open([{s:[v,b],b:2,p:"text"},{s:[b],b:1,p:"text"},{s:[x],b:1}])),e.rule("text",e=>e.open([{s:[v,b],b:1,r:"text",n:{text:1},g:"csv,space,follows",a:e=>{let n=1===e.n.text?e:e.prev;e.node=n.node=(1===e.n.text?"":e.prev.node)+e.o0.val}},{s:[b,v],r:"text",n:{text:1},g:"csv,space,leads",a:e=>{let n=1===e.n.text?e:e.prev;e.node=n.node=(1===e.n.text?"":e.prev.node)+(2<=e.n.text||!i?e.o0.src:"")+e.o1.src}},{s:[b,[h,x,g]],b:1,n:{text:1},g:"csv,end",a:e=>{let n=1===e.n.text?e:e.prev;e.node=n.node=(1===e.n.text?"":e.prev.node)+(i?"":e.o0.src)}},{s:[b],n:{text:1},g:"csv,space",a:e=>{if(l){let n=1===e.n.text?e:e.prev;e.node=n.node=(1===e.n.text?"":e.prev.node)+(i?"":e.o0.src)}},p:l?void 0:"val"},{}]).bc(e=>{e.parent.node=void 0===e.child.node?e.node:e.child.node}))};function t(e){return function(n,t){return function(t){let r={[e.string.quote]:!0},{pnt:l,src:o}=t,{sI:s,rI:i,cI:d}=l,c=o.length;if(r[o[s]]){const e=o[s],r=s,u=i;++s,++d;let a=[];for(;s<c;s++)if(d++,e===o[s]){if(s++,d++,e!==o[s])break;a.push(e)}else{let r=s,u=e.charCodeAt(0),f=o.charCodeAt(s);for(;s<c&&32<=f&&u!==f;)f=o.charCodeAt(++s),d++;if(d--,n.line.chars[o[s]])n.line.rowChars[o[s]]&&(l.rI=++i),d=1,a.push(o.substring(r,s+1));else{if(f<32)return l.sI=s,l.cI=d,t.bad("unprintable",s,s+1);a.push(o.substring(r,s)),s--}}if(o[s-1]!==e||l.sI===s-1)return l.rI=u,t.bad("unterminated_string",r,s);const f=t.token("#ST",a.join(""),o.substring(l.sI,s),l);return l.sI=s,l.rI=i,l.cI=d,f}}}}return e.Csv=n,n.defaults={trim:null,comment:null,number:null,value:null,header:!0,object:!0,stream:null,strict:!0,field:{separation:null,nonameprefix:"field~",empty:"",names:void 0,exact:!1},record:{separators:null,empty:!1},string:{quote:'"',csv:null}},e}));