@naruebedt/dtd
v0.1.0
Published
Universal data conversion library — parse any input format into a canonical model and serialize it back out to any other format.
Downloads
20
Maintainers
Readme
dtd
ระบบแปลงข้อมูลแบบ any-to-any — ไม่ว่า input จะมาในรูปแบบไหน ก็แปลง/ส่งออกเป็นอีกฟอร์แมตได้ทันที สร้างด้วย Bun + TypeScript
A universal data conversion library. Parse any supported input format into one neutral, canonical model, then serialize it out to any other format.
ติดตั้ง
# ติดตั้ง global (ใช้คำสั่ง dtd ได้ทุกที่)
bun add -g @naruebeth/dtd
# หรือ
npm install -g @naruebeth/dtdใช้งานเป็น CLI
dtd login # login ครั้งแรก
dtd whoami # ดูสถานะ
dtd -i config.yaml -o config.json # แปลงจากไฟล์ (เดา format จากนามสกุล)
dtd -i data.csv -t yaml # กำหนด output format
dtd --from csv --to json < data.csv # ผ่าน stdin
dtd --list-formats # ดู format ที่รองรับ
dtd logout # logoutฟอร์แมตที่รองรับ
| Format | Names / aliases | Extensions |
| ------ | --------------- | ---------- |
| JSON | json | .json |
| CSV | csv | .csv |
| TSV | tsv, tab | .tsv, .tab |
| YAML | yaml, yml | .yaml, .yml |
| XML | xml | .xml |
แนวคิด (Architecture)
หัวใจคือ canonical intermediate representation (IR) ตัวกลางเดียว ทุก adapter จะแปลงเข้า/ออกจาก IR นี้เท่านั้น ไม่ต้องรู้จักฟอร์แมตอื่น
input (any format) ──parse──▶ IR (canonical model) ──serialize──▶ output (any format)
│
transform (optional)ผลคือ: เพิ่ม adapter ใหม่ 1 ตัว → แปลงไป-กลับกับ ทุก ฟอร์แมตที่มีอยู่ได้ทันที
ใช้งานเป็น Library
import { convert, parse, serialize, detectFormat } from "@naruebeth/dtd";
// แปลงตรงๆ ระบุ from/to
const yaml = convert('{"name":"Aung","tags":["a","b"]}', { from: "json", to: "yaml" });
// ไม่ระบุ from → ตรวจจับฟอร์แมตอัตโนมัติจากเนื้อหา
const xml = convert('{"user":{"id":1}}', { to: "xml" });
// parse เป็น canonical model
const value = parse("name,age\nAung,30\n", "csv");
// → [{ name: "Aung", age: 30 }]
// serialize จาก canonical model
serialize(value, "yaml");
// ตรวจจับฟอร์แมต
detectFormat("---\nname: Aung\n"); // → "yaml"Transform กลางทาง (pipeline)
const out = convert(csvText, {
from: "csv",
to: "json",
transform: (rows) => (rows as any[]).filter((r) => r.age >= 18),
});เพิ่มฟอร์แมตใหม่ (Extensibility)
import { registerAdapter, type FormatAdapter } from "@naruebeth/dtd";
const ndjsonAdapter: FormatAdapter = {
name: "ndjson",
extensions: [".ndjson", ".jsonl"],
parse: (input) =>
input.split("\n").filter(Boolean).map((line) => JSON.parse(line)),
serialize: (value) =>
(value as unknown[]).map((v) => JSON.stringify(v)).join("\n") + "\n",
};
registerAdapter(ndjsonAdapter);เทสต์
bun testLicense
MIT
