Repository navigation
Expand file tree
/
Copy pathcsv.js
More file actions
134 lines (128 loc) · 2.87 KB
/
Copy pathcsv.js
File metadata and controls
134 lines (128 loc) · 2.87 KB
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
function batch(commands) {
if (!Array.isArray(commands)) {
throw 'expected a list of commands';
}
const results = [];
for (let i = 0; i < commands.length; i++) {
const command = commands[i];
if (!Array.isArray(command) || typeof command[1] !== 'string') {
throw 'malformed command';
}
results.push(run(command[0], command[1], command[2]));
}
return results;
}
function run(op, text, arg) {
const rows = parse(text);
if (op === 'rows') {
return rows;
}
if (op === 'column') {
return column(rows, arg);
}
if (op === 'sum') {
return sum(column(rows, arg));
}
if (op === 'format') {
return format(rows);
}
if (op === 'shape') {
return [rows.length, rows.length === 0 ? 0 : rows[0].length];
}
throw 'unknown operation: ' + op;
}
function parse(text) {
const rows = [];
let row = [];
let field = '';
let quoted = false;
let i = 0;
while (i < text.length) {
const ch = text.charAt(i);
if (quoted) {
if (ch === '"') {
if (i + 1 < text.length && text.charAt(i + 1) === '"') {
field = field + '"';
i = i + 1;
} else {
quoted = false;
}
} else {
field = field + ch;
}
} else if (ch === '"') {
quoted = true;
} else if (ch === ',') {
row.push(field);
field = '';
} else if (ch === '\n') {
row.push(field);
rows.push(row);
row = [];
field = '';
} else {
field = field + ch;
}
i = i + 1;
}
if (quoted) {
throw 'unterminated quote';
}
if (field.length > 0 && row.length > 0) {
row.push(field);
rows.push(row);
}
for (let r = 1; r < rows.length; r++) {
if (rows[r].length !== rows[0].length) {
throw 'ragged row ' + r;
}
}
return rows;
}
function column(rows, name) {
if (rows.length === 0) {
throw 'no header';
}
const index = rows[0].indexOf(name);
if (index < 0) {
throw 'no column ' + name;
}
const values = [];
for (let r = 1; r < rows.length; r++) {
values.push(rows[r][index]);
}
return values;
}
function sum(values) {
let total = 0;
for (let i = 0; i < values.length; i++) {
const n = Number(values[i]);
if (values[i].trim() === '' || Number.isNaN(n)) {
throw 'not a number: ' + values[i];
}
total = total + n;
}
return total;
}
function quote(field) {
if (field.indexOf(',') < 0 && field.indexOf('"') < 0 && field.indexOf('\n') < 0) {
return field;
}
let out = '"';
for (let i = 0; i < field.length; i++) {
const ch = field.charAt(i);
out = out + (ch === '"' ? '""' : ch);
}
return out + '"';
}
function format(rows) {
const lines = [];
for (let r = 0; r < rows.length; r++) {
const fields = [];
for (let c = 0; c < rows[r].length; c++) {
fields.push(quote(rows[r][c]));
}
lines.push(fields.join(','));
}
return lines.join('\n');
}