aboutsummaryrefslogtreecommitdiffstats
path: root/csv.mjs
diff options
context:
space:
mode:
authorBrian Cully <bjc@spork.org>2025-07-16 18:48:48 -0400
committerBrian Cully <bjc@spork.org>2025-07-16 18:48:48 -0400
commite1a9e10902de4d927f4c2c87b6c27e6303fa1ead (patch)
tree516d92bd5bf204308919d57698277b7bb9e08078 /csv.mjs
parent5da918119bb2e352f10060a36d9c7e580fa8695e (diff)
downloadpnit-e1a9e10902de4d927f4c2c87b6c27e6303fa1ead.tar.gz
pnit-e1a9e10902de4d927f4c2c87b6c27e6303fa1ead.zip
add js version rendering. still needs download
Diffstat (limited to 'csv.mjs')
-rw-r--r--csv.mjs79
1 files changed, 79 insertions, 0 deletions
diff --git a/csv.mjs b/csv.mjs
new file mode 100644
index 0000000..b096ab4
--- /dev/null
+++ b/csv.mjs
@@ -0,0 +1,79 @@
+export default class {
+ constructor() {
+ this.state = this.awaitingTokenOrComma;
+ this.inProgress = '';
+ this._rowResults = [];
+ this._results = [];
+ }
+
+ push(c) {
+ this.state(c);
+ }
+
+ get results() {
+ return this._results.concat([this._rowResults.concat([this.inProgress])]);
+ }
+
+ awaitingTokenOrComma(c) {
+ switch (c) {
+ case ',':
+ // empty column
+ this._rowResults.push('');
+ break;
+ case '\n':
+ this._results.push(this._rowResults);
+ this._rowResults = [];
+ break;
+ default:
+ if (/\s/.test(c)) {
+ return;
+ }
+ this.inProgress += c;
+ this.state = this.awaitingComma;
+ }
+ }
+
+ awaitingComma(c) {
+ switch (c) {
+ case ',':
+ this._rowResults.push(this.inProgress);
+ this.inProgress = '';
+ this.state = this.awaitingTokenOrComma;
+ break;
+ case '\r':
+ break;
+ case '\n':
+ this._results.push(this._rowResults.concat(this.inProgress));
+ this._rowResults = [];
+ this.inProgress = '';
+ this.state = this.awaitingTokenOrComma;
+ break;
+ case '"':
+ this.state = this.checkForDoubleQuote;
+ break;
+ default:
+ this.inProgress += c;
+ }
+ }
+
+ checkForDoubleQuote(c) {
+ this.inProgress += c
+ switch (c) {
+ case '"':
+ this.state = this.awaitingComma;
+ break;
+ default:
+ this.state = this.awaitingQuote;
+ }
+ }
+
+ awaitingQuote(c) {
+ switch (c) {
+ case '"':
+ this.state = this.awaitingComma;
+ break;
+ default:
+ this.inProgress += c;
+ }
+ }
+}