Fixed-Width Data Parser

3.0.0 · active · verified Tue Apr 21

The `fixed-width-parser` package provides a robust Node.js module for parsing and unparsing data from and to fixed-width string formats. Currently stable at version `3.0.0`, it offers a flexible configuration API where developers define field mappings using an array of objects, specifying `name`, `type` (e.g., 'string', 'int'), `start` index, and `width`. The library handles common parsing challenges such as padding, truncation, default values for undefined fields, and explicit `falsyFallback` options for parsed values. It ships with TypeScript type definitions, making it well-suited for modern TypeScript and JavaScript projects, and primarily targets server-side data processing due to its Node.js engine requirement. While a strict release cadence isn't published, major versions are released to introduce significant features or API adjustments, maintaining a focus on stability and clear API design.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate FixedWidthParser with a schema, parse a multi-line fixed-width string into an array of objects, and then unparse an array of objects back into a fixed-width string.

import { FixedWidthParser } from 'fixed-width-parser';

// Define the schema for your fixed-width data
const schema = [
  {
    type: 'int',
    name: 'age',
    start: 0,
    width: 2,
  },
  {
    name: 'name',
    start: 2,
    width: 12,
  },
];

const fixedWidthParser = new FixedWidthParser(schema);

const inputString = `42         bob
21       alice
33        jeff`;

const parsedResult = fixedWidthParser.parse(inputString);
console.log('Parsed Data:', parsedResult);

const dataToUnparse = [
  { age: 55, name: 'charlie' },
  { age: 18, name: 'diana' }
];

const unparsedString = fixedWidthParser.unparse(dataToUnparse);
console.log('Unparsed String:\n', unparsedString);

view raw JSON →