This repository contains 2 libraries for reading (parsing) and writing (generating) text data whose lines consists of fields (eg. CSV, TSV and text data with fixed length fields). They work by associating a schema (called Meta) with the text data which allows reading and writing text data in a manner similar to reading and writing from/to databases.
The schema supports a wide variety of field formatting and structure in text data. This includes data with lines that contain different fields depending on the value of key fields - where effectively the data is a database with multiple tables (each having records with different fields).
Fielded Text is ideal for reading and writing database like text data with complex schemas containing multiple tables.
The structure of this meta/schema is specified by the proposed Fielded Text standard. A schema can be created for text data using a Fielded Text editor and then used by the library to read and write data in a similar fashion to reading and writing to/from database tables.
Below is a very simple parsing example:
import { FtReader, FtXmlMetaSerialization } from "@pbkware/fielded-text-web";
// CSV data to be read
const csvData = `Name,Age
John Doe,30
Jane Smith,25`;
// Meta describing the schema of the CSV data
const xmlMeta = `<?xml version="1.0" encoding="utf-8"?>
<FieldedText HeadingLineCount="1">
<Field Name="Name"/>
<Field Name="Age" DataType="Integer"/>
</FieldedText>`;
// Load meta data from XML
const metaReader = new FtXmlMetaSerialization();
const meta = metaReader.deserialize(xmlMeta);
const reader = new FtReader(meta, csvData);
// Read and log the data
while (reader.read()) {
console.log(
reader.fieldList.get(0).asString,
reader.fieldList.get(1).asBigInt,
);
}
There are 2 separate libraries for browser and node environments:
@pbkware/fielded-text-web if @pbkware/fielded-text-node is imported! This is not necessary as @pbkware/fielded-text-node re-exports all types from @pbkware/fielded-text-web.See Change Log for changes - including any breaking changes.