-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
204 lines (192 loc) · 8.1 KB
/
index.html
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
<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<!--
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
-->
<title>csvbeauty</title>
<meta property="og:title" content="csvbeauty">
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="https://code4sabae.github.io/csvbeauty/csvbeauty.png">
<meta property="og:image" content="https://code4sabae.github.io/csvbeauty/csvbeauty.png">
<script type="module">
import { CSV } from "https://code4sabae.github.io/js/CSV.js";
import { SJIS } from "https://code4sabae.github.io/js/SJIS.js";
import { downloadZip } from "https://code4sabae.github.io/js/downloadZip.js";
import { waitDropFiles } from "https://code4sabae.github.io/js/waitDropFiles.js";
import { downloadTextFile } from "https://code4sabae.github.io/js/downloadTextFile.js";
import { readAsArrayBufferAsync } from "https://code4sabae.github.io/js/readAsArrayBufferAsync.js";
import { JISX0213 } from "https://code4fukui.github.io/mojikiban/JISX0213.js";
const saveCheckboxState = (chk) => {
const v = localStorage.getItem("saveCheckboxState." + chk.id);
if (v !== null) {
chk.checked = v == "true";
}
chk.addEventListener("change", () => {
localStorage.setItem("saveCheckboxState." + chk.id, chk.checked.toString());
});
};
const removeComma = (v) => {
return v.toString().trim().replace(/\,/g, "");
};
const addComma = (v) => {
//return parseFloat(v).toLocaleString("en-US", { maximumFractionDigits: 0 }); // 整数化
return parseFloat(v).toLocaleString("en-US");
};
/*
console.log(addComma(100000000));
console.log(addComma(100000000.123));
console.log(removeComma("100,000,000"));
*/
const removeCommaCSV = (csv) => {
for (let i = 0; i < csv.length; i++) {
const line = csv[i];
for (let j = 0; j < line.length; j++) {
const v = line[j].trim();
const rv = removeComma(v);
if (!isNaN(parseFloat(rv)) && addComma(rv) == v) {
line[j] = rv;
}
}
}
};
const trimWhiteSpaceCSV = (csv) => {
for (let i = 0; i < csv.length; i++) {
const line = csv[i];
for (let j = 0; j < line.length; j++) {
line[j] = line[j].trim();
}
}
};
const isNullLine = (csvline) => {
let cnt = 0;
for (let i = 0; i < csvline.length; i++) {
cnt += csvline[i].length;
}
return cnt == 0;
};
const removeNullLine = (csv) => {
const csv2 = [];
for (let i = 0; i < csv.length; i++) {
const line = csv[i];
if (!isNullLine(line)) {
csv2.push(line);
}
}
return csv2;
};
const removeNullRow = (csv) => {
let nrow = csv[0].length - 1;
A: for (; nrow >= 0; nrow--) {
for (let j = 0; j < csv.length; j++) {
if (csv[j][nrow].length != 0) {
break A;
}
}
}
nrow++;
for (let i = 0; i < csv.length; i++) {
csv[i].splice(nrow, csv[i].length - nrow);
}
return csv;
};
const shrinkToJISX0213 = (csv) => {
for (let i = 0; i < csv.length; i++) {
const line = csv[i];
for (let j = 0; j < line.length; j++) {
line[j] = JISX0213.shrink(line[j]);
if (!JISX0213.isValid(line[j])) {
throw new Error(line[j] + " is not valid JIS X 0213");
}
}
}
};
window.onload = async () => {
saveCheckboxState(trimwhitespace);
saveCheckboxState(removecomma);
saveCheckboxState(removenullline);
saveCheckboxState(removenullrow);
saveCheckboxState(shrink2jisx0213);
for (;;) {
divmain.innerHTML = "CSVファイルをドロップしてください";
const items = await waitDropFiles(divmain);
const files = [];
for (const item of items) {
// console.log(item.type); // ignore item.type in Windows
if (!(item.file.name.endsWith(".csv") || item.file.name.endsWith(".json"))) {
alert("ファイル形式が違います。CSVファイルをドロップしてください。")
return;
}
const file = item.file;
const bin = await readAsArrayBufferAsync(file);
const data = SJIS.decodeAuto(new Uint8Array(bin));
if (item.file.name.endsWith(".csv")) {
try {
let csv = CSV.decode(data);
if (trimwhitespace.checked) {
trimWhiteSpaceCSV(csv);
}
if (removecomma.checked) {
removeCommaCSV(csv);
}
if (removenullline.checked) {
csv = removeNullLine(csv);
}
if (removenullrow.checked) {
csv = removeNullRow(csv);
}
if (shrink2jisx0213.checked) {
shrinkToJISX0213(csv);
}
const scsv = CSV.encode(csv);
files.push({ name: file.name, data: scsv });
} catch (e) {
console.log(e);
alert("CSVファイルの読み込みや変換に失敗しました。データが正常かご確認ください。");
return;
}
} else {
try {
const json = JSON.parse(data);
const csv = CSV.fromJSON(json);
const scsv = CSV.encode(csv);
const name = file.name.substring(0, file.name.length - 4) + "csv";
files.push({ name, data: scsv });
} catch (e) {
console.log(e);
alert("JSONファイルの読み込みに失敗しました。データが正常かご確認ください。");
return;
}
}
}
if (files.length == 1) {
downloadTextFile(files[0]);
} else {
downloadZip("csvs.zip", files);
}
}
};
</script>
</head>
<body>
<div class="jumbotron">
<h1 class="display-4">csvbeauty</h1>
<p class="lead">CSVファイルをBOM付きUTF-8に変換します(まとめて変換、JSONファイル対応)</p>
</div>
<div class="mx-5">
<div id="divmain" class="border px-5 mx-auto pt-3 pb-3"></div>
<label><input type="checkbox" id="trimwhitespace" class="m-3" checked>値前後の空白削除</label>
<label><input type="checkbox" id="removecomma" class="m-3" checked>コンマ入り数値のコンマ削除</label>
<label><input type="checkbox" id="removenullline" class="m-3" checked>空行削除</label>
<label><input type="checkbox" id="removenullrow" class="m-3" checked>空列削除</label>
<label><input type="checkbox" id="shrink2jisx0213" class="m-3" checked><a href=https://github.com/code4fukui/BaseRegistry/blob/main/%E7%94%A8%E8%AA%9E.md#jis-x-0213>JIS X 0213</a>に縮退β</label>
</div>
<footer class="text-muted">
<div class="container my-4 mx-0">
<p>Design: <a href="https://getbootstrap.com/">Bootstrap</a> by <a href="https://twitter.com/mdo">@mdo</a></p>
<p>App: CC BY <a href="https://fukuno.jig.jp/3115">福野泰介の一日一創</a> by <a href="https://twitter.com/taisukef">@taisukef</a> (<a href=https://github.com/code4sabae/csvbeauty/>src on GitHub</a>)</p>
</div>
</footer>
</body>
</html>