MCPcopy Create free account
hub / github.com/Keyang/node-csvtojson / toCSVRow

Method toCSVRow

src/rowSplit.ts:55–135  ·  view source on GitHub ↗
(rowArr: string[], trim: boolean, quote: string, delimiter: string)

Source from the content-addressed store, hash-verified

53
54 }
55 private toCSVRow(rowArr: string[], trim: boolean, quote: string, delimiter: string): RowSplitResult {
56 const row: string[] = [];
57 let inquote = false;
58 let quoteBuff = '';
59 for (let i = 0, rowLen = rowArr.length; i < rowLen; i++) {
60 let e = rowArr[i];
61 if (!inquote && trim) {
62 e = trimLeft(e);
63 }
64 const len = e.length;
65 if (!inquote) {
66 if (len === 2 && e === this.quote + this.quote) {
67 row.push("");
68 continue;
69 } else if (this.isQuoteOpen(e)) { //quote open
70 e = e.substr(1);
71 if (this.isQuoteClose(e)) { //quote close
72 e = e.substr(0, e.lastIndexOf(quote));
73 e = this.escapeQuote(e);
74 row.push(e);
75 continue;
76 } else if (e.indexOf(quote) !== -1) {
77 let count = 0;
78 let prev = "";
79 for (const c of e) {
80 // count quotes only if previous character is not escape char
81 if (c === quote && prev !== this.escape) {
82 count++;
83 prev = "";
84 } else {
85 // save previous char to temp variable
86 prev = c;
87 }
88 }
89 if (count % 2 === 1) {
90 if (trim) {
91 e = trimRight(e);
92 }
93 row.push(quote + e);
94 continue;
95 }else{
96 inquote = true;
97 quoteBuff += e;
98 continue;
99 }
100 }
101 else {
102 inquote = true;
103 quoteBuff += e;
104 continue;
105 }
106 } else {
107 if (trim) {
108 e = trimRight(e);
109 }
110 row.push(e);
111 continue;
112 }

Callers 4

parseMethod · 0.95
browser.jsFile · 0.80
csvtojson.min.jsFile · 0.80
rowSplit.jsFile · 0.80

Calls 5

isQuoteOpenMethod · 0.95
isQuoteCloseMethod · 0.95
escapeQuoteMethod · 0.95
trimLeftFunction · 0.90
trimRightFunction · 0.90

Tested by

no test coverage detected