diff options
| author | Brian Cully <bjc@spork.org> | 2025-07-16 18:48:48 -0400 |
|---|---|---|
| committer | Brian Cully <bjc@spork.org> | 2025-07-16 18:48:48 -0400 |
| commit | e1a9e10902de4d927f4c2c87b6c27e6303fa1ead (patch) | |
| tree | 516d92bd5bf204308919d57698277b7bb9e08078 /csv.mjs | |
| parent | 5da918119bb2e352f10060a36d9c7e580fa8695e (diff) | |
| download | pnit-e1a9e10902de4d927f4c2c87b6c27e6303fa1ead.tar.gz pnit-e1a9e10902de4d927f4c2c87b6c27e6303fa1ead.zip | |
add js version rendering. still needs download
Diffstat (limited to 'csv.mjs')
| -rw-r--r-- | csv.mjs | 79 |
1 files changed, 79 insertions, 0 deletions
@@ -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; + } + } +} |
