0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

RFC 4180 準拠 CSV パーサで CSV エディタを作った

0
Posted at

作ったもの

CSV Toolhttps://sen.ltd/portfolio/csv-tool/

スクリーンショット

  • RFC 4180 準拠パーサ(引用符・エスケープ・埋め込み改行対応)
  • 区切り文字の自動検出(カンマ / タブ / セミコロン / パイプ)
  • テーブル編集(ソート・検索・ページング)
  • 行・列の追加/削除
  • エクスポート: CSV / TSV / JSON / Markdown / HTML
  • 列ごとの型検出(数値 / 日付 / 真偽 / 文字列)

vanilla JS、ゼロ依存、ビルド不要node --test で 76 ケース。

line.split(',') では足りない

RFC 4180 は "foo,bar" のようにフィールドを引用符で囲める。引用符内のカンマはリテラル、"" は引用符エスケープ、改行も埋め込める。state machine で書くのが一番素直。

3 状態のパーサ

  • outside-quotes: 通常の文字読み込み
  • inside-quotes: 引用符内、delimiter も改行もリテラル
  • (暗黙) inside-quotes で " を見たら次の文字で分岐: "" なら 1 個の " として追加、それ以外なら引用モード終了

区切り文字の自動検出

候補 [',', '\t', ';', '|'] について各行のカウントを取り、一貫性を重視してスコアリング。a,b,c / d,e のように行ごとにバラつくと低スコア。

列型検出の順序

if (nonEmpty.every(isValidNumber)) return 'number';
if (nonEmpty.every(isValidDate)) return 'date';
if (nonEmpty.every(isValidBoolean)) return 'boolean';

"1" は数値としても真偽としても valid なので、数値を先にチェック。より有用な分類を優先。

シリーズ

100+ 公開ポートフォリオ シリーズの #54 です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?