This guide will help you get started with the FieldedText TypeScript library, from installation to your first read and write operations.
npm install @pbkware/fielded-text-web
Meta data defines the structure and format of your fielded text file. Think of it as a schema that describes:
Fields represent individual columns in your data. Each field has:
Sequences define the order and grouping of fields. A simple file has one root sequence containing all fields in order. Advanced files can have nested sequences for repeating groups.
Let's read a simple CSV file. We'll build the meta data in code:
import { FtBooleanMetaField, FtBooleanStyles, FtDataType, FtMeta, FtReader } from "@pbkware/fielded-text-web";
// Sample CSV data
const csvData = `Name,Age,Active
John Doe,30,true
Jane Smith,25,false
Bob Johnson,45,true`;
// Step 1: Create meta data
const meta = new FtMeta();
meta.headingLineCount = 1; // One heading line
// Step 2: Define fields
const nameField = meta.fieldList.new(FtDataType.String);
nameField.name = "Name";
nameField.headings = ["Name"];
const ageField = meta.fieldList.new(FtDataType.Integer);
ageField.name = "Age";
ageField.headings = ["Age"];
const activeField = meta.fieldList.new(FtDataType.Boolean);
activeField.name = "Active";
activeField.headings = ["Active"];
(activeField as FtBooleanMetaField).styles = FtBooleanStyles.IgnoreCase;
// Step 3: Define root sequence (field order)
const rootSequence = meta.sequenceList.new();
rootSequence.name = "Root";
rootSequence.root = true;
rootSequence.itemList.new(nameField);
rootSequence.itemList.new(ageField);
rootSequence.itemList.new(activeField);
// Step 4: Create reader and load meta data
const reader = new FtReader(meta, csvData);
// Step 5: Read records
console.log("Reading CSV data:");
console.log("================\n");
let recordNumber = 0;
while (reader.read()) {
recordNumber++;
// Access fields by index
const name = reader.fieldList.get(0).asString;
const age = Number(reader.fieldList.get(1).asBigInt);
const active = reader.fieldList.get(2).asBoolean;
console.log(
`Record ${recordNumber}: ${name}, Age: ${age}, Active: ${active}`,
);
}
console.log(`\nTotal records read: ${recordNumber}`);
Output:
Reading CSV data:
================
Record 1: John Doe, Age: 30, Active: true
Record 2: Jane Smith, Age: 25, Active: false
Record 3: Bob Johnson, Age: 45, Active: true
Total records read: 3
FtMeta) and set basic propertiesmeta.fieldList.new(dataType)reader.read()reader.fieldList.get(index)Now let's write CSV data:
import { FtDataType, FtIntegerMetaField, FtMeta, FtStringWriter, FtWriter } from "@pbkware/fielded-text-web";
// Step 1: Create meta data (same as reading)
const meta = new FtMeta();
meta.headingLineCount = 1;
const nameField = meta.fieldList.new(FtDataType.String);
nameField.name = "Name";
nameField.headings = ["Name"];
const ageField = meta.fieldList.new(FtDataType.Integer);
(ageField as FtIntegerMetaField).name = "Age";
ageField.headings = ["Age"];
const rootSequence = meta.sequenceList.new();
rootSequence.name = "Root";
rootSequence.root = true;
rootSequence.itemList.new(nameField);
rootSequence.itemList.new(ageField);
// Step 2: Create writer and load meta data
const stringWriter = new FtStringWriter();
const writer = new FtWriter(meta, stringWriter);
// Step 3: Write records
writer.fieldList.get(0).asString = "John Doe";
writer.fieldList.get(1).asBigInt = BigInt(30);
writer.write();
writer.fieldList.get(0).asString = "Jane Smith";
writer.fieldList.get(1).asBigInt = BigInt(25);
writer.write();
writer.fieldList.get(0).asString = "Bob Johnson";
writer.fieldList.get(1).asBigInt = BigInt(45);
writer.write();
// Step 4: Show output
console.log("Generated CSV:");
console.log("==============");
console.log(stringWriter.toString());
Output:
Generated CSV:
==============
Name,Age
John Doe,30
Jane Smith,25
Bob Johnson,45
writer.fieldList.get(index)write() to output the recordwriter.fieldList.get(index) before calling write()Instead of building meta data in code, you can load it from an XML file:
import { FtReader, FtXmlMetaSerialization } from "@pbkware/fielded-text-web";
// XML format meta data string
const xmlMeta = `<?xml version="1.0" encoding="utf-8"?>
<FieldedText HeadingLineCount="1">
<Field Name="Name" Headings="Name"/>
<Field Name="Age" DataType="Integer" Headings="Age"/>
</FieldedText>`;
// Load meta data from XML
const metaReader = new FtXmlMetaSerialization();
const meta = metaReader.deserialize(xmlMeta);
// Use it with FtReader to read CSV data
const csvData = `Name,Age
John Doe,30
Jane Smith,25`;
const reader = new FtReader(meta, csvData);
while (reader.read()) {
console.log(
reader.fieldList.get(0).asString,
reader.fieldList.get(1).asBigInt,
);
}
Advantages of XML Meta data:
See the Meta data Guide for more XML format documentation.
Happy parsing! 🎉