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.
npm install fixed-width-parserVerified import paths — ran on the pinned version, not inferred.
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.
Carefully define `start` as the zero-based index of the first character and `width` as the total character length for each field.
For non-decimal integers, add `radix: <base_value>` to the integer field's configuration, e.g., `{ type: 'int', name: 'id', start: 0, width: 4, radix: 16 }`.Set `falsyFallback: 'undefined'` or `falsyFallback: 'null'` in `IParseOptions` or specific field configurations to ensure consistent handling of empty or falsy data points.
Explicitly set `truncate: false` in your field configurations if you want unparsing to throw an error for values exceeding the defined `width`, or `truncate: true` if silent truncation is acceptable.
Always use `new FixedWidthParser(...)` to create an instance of the parser.
Ensure the input string contains only valid digits for the `radix` set in the field config. For decimal (base 10), this means only '0'-'9'. Consider using `type: 'string'` if the field may contain non-numeric characters.
Either adjust the field's `width` to accommodate the value, or explicitly set `truncate: true` in the field's configuration to allow the value to be truncated during unparsing.
No dependency data recorded yet.