-
Notifications
You must be signed in to change notification settings - Fork 1
/
CSV.js
320 lines (315 loc) · 7.57 KB
/
CSV.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// https://github.com/code4fukui/CSV/
import { SJIS } from "https://code4sabae.github.io/js/SJIS.js";
const CSV = {};
CSV.parse = (s) => CSV.toJSON(CSV.decode(s));
CSV.stringify = (json) => CSV.encode(CSV.fromJSON(json));
CSV.addBOM = function (s) {
if (s != null) {
return "\ufeff" + s;
}
return s;
};
CSV.removeBOM = function (s) {
if (s && typeof s == "string" && s.length > 0 && s.charAt(0) === "\ufeff") {
return s.substring(1);
}
return s;
};
CSV.normalizeEnter = function(s) {
if (s.indexOf("\r") >= 0 && s.indexOf("\n") == -1) {
s = s.replace(/\r/g, "\n");
}
return s;
};
CSV.decode = function (s) {
s = CSV.removeBOM(s);
s = CSV.normalizeEnter(s);
const res = [];
let st = 0;
let line = [];
let sb = null;
if (!s.endsWith("\n")) s += "\n";
const len = s.length;
for (let i = 0; i < len; i++) {
const c = s.charAt(i);
if (c === "\r") continue;
if (st === 0) {
if (c === "\n") {
if (line.length > 0) line.push("");
res.push(line);
line = [];
} else if (c == ",") {
line.push("");
} else if (c == '"') {
sb = "";
st = 2;
} else {
sb = c;
st = 1;
}
} else if (st === 1) {
if (c === "\n") {
line.push(sb);
res.push(line);
line = [];
st = 0;
sb = null;
} else if (c === ",") {
line.push(sb);
sb = null;
st = 0;
} else {
sb += c;
}
} else if (st === 2) {
if (c === '"') {
st = 3;
} else {
sb += c;
}
} else if (st === 3) {
if (c === '"') {
sb += c;
st = 2;
} else if (c === ",") {
line.push(sb);
sb = null;
st = 0;
} else if (c === "\n") {
line.push(sb);
res.push(line);
line = [];
st = 0;
sb = null;
}
}
}
if (sb != null) line.push(sb);
if (line.length > 0) res.push(line);
return res;
};
CSV.encode = function (csvar) {
let s = [];
for (let i = 0; i < csvar.length; i++) {
let s2 = [];
const line = csvar[i];
for (let j = 0; j < line.length; j++) {
const v = line[j];
if (v == undefined || v.length == 0) {
s2.push("");
} else if (typeof v == "number") {
s2.push(v);
} else if (typeof v != "string") {
s2.push('"' + v + '"');
} else if (v.indexOf('"') >= 0) {
s2.push('"' + v.replace(/\"/g, '""') + '"');
} else if (v.indexOf(",") >= 0 || v.indexOf("\n") >= 0) {
s2.push('"' + v + '"');
} else {
s2.push(v);
}
}
s.push(s2.join(","));
}
return CSV.addBOM(s.join("\n") + "\n");
};
CSV.toJSON = function (csv, removeblacket) {
const res = [];
const head = csv[0];
if (removeblacket) {
for (let i = 0; i < head.length; i++) {
const h = head[i];
const n = h.indexOf("(");
const m = h.indexOf("(");
let l = -1;
if (n === -1) {
l = m;
} else if (m === -1) {
l = n;
} else {
l = Math.min(n, m);
}
head[i] = (l > 0 ? h.substring(0, l) : h).trim();
}
}
for (let i = 1; i < csv.length; i++) {
const d = {};
for (let j = 0; j < head.length; j++) {
d[head[j]] = csv[i][j];
}
res.push(d);
}
return res;
};
CSV.fromJSON = function (json) {
if (!Array.isArray(json)) {
throw "is not array!";
}
const head = [];
for (const d of json) {
for (const name in d) {
if (head.indexOf(name) == -1) {
head.push(name);
}
}
}
const res = [head];
for (const d of json) {
const line = [];
for (let i = 0; i < head.length; i++) {
const v = d[head[i]];
if (v == undefined) {
line.push("");
} else {
line.push(v);
}
}
res.push(line);
}
return res;
};
CSV.toMarkdown = function (csvorjson) {
const csv = Array.isArray(csvorjson[0]) ? csvorjson : CSV.fromJSON(csvorjson);
const res = [];
const head = csv[0];
res.push("# " + head[0]);
res.push("");
for (let i = 1; i < csv.length; i++) {
const d = csv[i];
res.push("## " + d[0]);
res.push("");
for (let j = 1; j < head.length; j++) {
if (d[j] != null) {
if (typeof d[j] == "string" && d[j].indexOf("\n") >= 0) {
res.push("### " + head[j]);
res.push("");
const ss = d[j].split("\n");
for (const s of ss) {
if (s.length > 0 && "\\-#".indexOf(s[0]) >= 0) {
res.push("\\" + s);
res.push("");
} else {
res.push(s);
res.push("");
}
}
} else {
if (res[res.length - 1] == "" && res[res.length - 2][0] == "-") {
res.pop();
}
res.push("- " + head[j] + ": " + d[j]);
res.push("");
}
}
}
}
return res.join("\n");
};
CSV.fromMarkdown = function (s) {
const ss = s.split("\n");
if (!ss[0].startsWith("# ")) {
throw new Error("not supported format");
}
const idname = ss[0].substring(2);
const res = [];
let n = 1;
while (n < ss.length) {
if (!ss[n].startsWith("## ")) {
n++;
continue;
}
const id = ss[n++].substring(3);
const d = {};
d[idname] = id;
while (n < ss.length) {
if (ss[n].startsWith("- ")) {
const ssn = ss[n++];
const m = ssn.indexOf(": ");
if (m < 0) {
throw new Error("not supported format");
}
const name = ssn.substring(2, m);
const val = ssn.substring(m + 2);
d[name] = val;
} else if (ss[n].startsWith("### ")) {
const name = ss[n].substring(4);
n += 2;
const val = [];
for (;;) {
const s1 = ss[n++];
if (s1[0] == "-" || s1[0] == "#" || n == ss.length) {
break;
}
if (s1[0] == "\\") {
val.push(s1.substring(1));
} else {
val.push(s1);
}
if (n < ss.length && ss[n] == "") {
n++;
if (n == ss.length) {
break;
}
}
}
d[name] = val.join("\n");
n--;
} else if (ss[n].startsWith("## ")) {
break;
} else {
n++;
}
}
res.push(d);
}
return res;
};
CSV.fetchOrLoad = async (fn) => {
if (fn.startsWith("https://") || fn.startsWith("http://") || !globalThis.Deno) {
return new Uint8Array(await (await fetch(fn)).arrayBuffer());
} else {
return await Deno.readFile(fn);
}
}
CSV.fetchUtf8 = async (url) => {
const data = await (await fetch(url)).text();
const csv = CSV.decode(data);
return csv;
};
CSV.fetch = async (url) => {
const data = SJIS.decodeAuto(await CSV.fetchOrLoad(url));
const csv = CSV.decode(data);
return csv;
};
CSV.fetchJSON = async (url) => {
return CSV.toJSON(await CSV.fetch(url));
};
CSV.makeTable = (csv) => {
const c = (tag) => document.createElement(tag);
const tbl = c("table");
const tr0 = c("tr");
tbl.appendChild(tr0);
for (let i = 0; i < csv[0].length; i++) {
const th = c("th");
tr0.appendChild(th);
th.textContent = csv[0][i];
}
for (let i = 1; i < csv.length; i++) {
const tr = c("tr");
tbl.appendChild(tr);
for (let j = 0; j < csv[i].length; j++) {
const td = c("td");
tr.appendChild(td);
const s = csv[i][j];
if (typeof s == "string" && (s.startsWith("http://") || s.startsWith("https://"))) {
const a = c("a");
a.href = a.textContent = s;
td.appendChild(a);
} else {
td.textContent = s;
}
}
}
return tbl;
};
export { CSV };