fixed-width-parser
fixed-width-parser
is a node module for parsing data to and from fixed width string formats.
Install
npm install fixed-width-parser
Usage
Parse
const { FixedWidthParser } = require('fixed-width-parser');
const fixedWidthParser = new FixedWidthParser([
{
type: 'int',
name: 'age',
start: 0,
width: 2,
},
{
name: 'name',
start: 2,
width: 12,
},
]);
const input = `42 bob
21 alice
33 jeff`;
const result = fixedWidthParser.parse(input);
result
is an array with the following content:
[
{
"age": 42,
"name": "bob"
},
{
"age": 21,
"name": "alice"
},
{
"age": 33,
"name": "jeff"
}
]
Parse Options
interface IParseOptions {
falsyFallback: FalsyFallback;
}
Unparse
const { FixedWidthParser } = require('fixed-width-parser');
const fixedWidthParser = new FixedWidthParser([
{
type: 'int',
name: 'age',
start: 0,
width: 2,
},
{
name: 'name',
start: 2,
width: 12,
},
]);
const input = [
{
age: 42,
name: 'bob',
},
{
age: 21,
name: 'alice',
},
{
age: 33,
name: 'jeff',
},
];
const result = fixedWidthParser.unparse(input);
result
is a string with the following content:
42 bob
21 alice
33 jeff
Config
When initializing a new instance of the FixedWidthParser
class, you must provide an array of parse
configs. These configs define how to convert between lines of text and json objects by providing a
mapping between segments of text and object keys.
All parse configs share the following properties:
interface IBaseParseConfig {
type?: string;
width: number;
start: number;
padPosition?: 'start' | 'end';
padString?: string;
default?: string | number;
truncate?: boolean;
falsyFallback?: 'undefined' | 'null' | 'passthrough';
}
An explicit type
property can be provided in each parse config to specify what data types to use
for values parsed from strings. Several of these data types require additional properties to be
provided to fully define how parse/unparse values.
interface IStringParseConfig {
type: 'string';
name: string;
}
interface IIntegerParseConfig {
type: 'int';
name: string;
radix?: number;
mend?: 'ceil' | 'floor' | 'round' | 'error';
}
interface IFloatParseConfig {
type: 'float';
name: string;
insertDecimal?: boolean;
decimalCount?: number;
}
interface IDateParseConfig {
type: 'date';
name: string;
fixedWidthFormat: string;
jsonFormat: string;
tryParsingRawValueBeforeFallback: boolean;
}
interface IBooleanParseConfig {
type: 'bool';
name: string;
trueValue: string;
falseValue: string;
}
interface ISkipParseConfig {
type: 'skip';
}
Parse Config Validation
When constructing a new FixedWidthParser
instance the provided parse config will be validated. If
any errors are detected an array of validation errors will be thrown to help you find and correct
the invalid configs.
interface IParseConfigValidationError {
index: number;
config: ParseConfig;
errors: string[];
}
Parser Options
interface IFixedWidthParserOptions {
truncate?: boolean;
expectedFullWidth?: number;
characterWhitelist?: ICharacterWhitelist;
}
Character Whitelist
alpha?: boolean;
numeric?: boolean;
special?: boolean;
extended?: boolean;
other?: string[];
Parser Logger
By default any logs from the parser will be handled by the built in console
logger. You can
optionally provide your own ILogger
compatible logger to process logs.
const { FixedWidthParser, ILogger } = require('fixed-width-parser');
const myLogger: ILogger = {
warn: (...data: any[]) => console.warn(data);
}
const fixedWidthParser = new FixedWidthParser(
[
{
type: 'int',
name: 'age',
start: 0,
width: 2,
},
{
name: 'name',
start: 2,
width: 12,
},
],
undefined,
myLogger,
);
Thanks
A huge thanks to @SteveyPugs for his work on fixy
which served as inspiration for
fixed-width-parser
! fixed-width-parser
started out as a fork of fixy
and evolved into its own
library when I got carried away and implemented a new high-level API.
Another huge thanks to @wk-davis for her help with concept discussions and ongoing development help!