# @evologi/fixed-width

> A fixed-width file format toolset with streaming support and flexible options.

Latest version **1.2.1** (published 2025-09-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install @evologi/fixed-width
pnpm add @evologi/fixed-width
yarn add @evologi/fixed-width
bun add @evologi/fixed-width
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.1 |
| Published | 2025-09-26 |
| First published | 2022-03-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 49.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Evologi S.r.l. |
| Maintainers | orderpod, greguz |
| Keywords | fields, fixed-width, fixed, width, parse, parser, stringify, stringifier, stream, streaming |

## Links

- npm: https://www.npmjs.com/package/@evologi/fixed-width
- Repository: https://github.com/evologi/fixed-width
- Homepage: https://github.com/evologi/fixed-width#readme
- Issues: https://github.com/evologi/fixed-width/issues
- npm.io page: https://npm.io/package/@evologi/fixed-width

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 1.2.1 (latest) — 2025-09-26
- 1.2.0 — 2025-09-15
- 1.1.0 — 2025-06-27
- 1.0.1 — 2023-10-20
- 1.0.0 — 2023-06-12
- 0.3.0 — 2022-05-16
- 0.2.1 — 2022-04-29
- 0.2.0 — 2022-03-31
- 0.1.0 — 2022-03-30

## README

# fixed-width

[![npm](https://img.shields.io/npm/v/@evologi/fixed-width)](https://www.npmjs.com/package/@evologi/fixed-width)
![Libraries.io dependency status for latest release](https://img.shields.io/librariesio/release/npm/@evologi/fixed-width)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
[![ci](https://github.com/evologi/fixed-width/actions/workflows/ci.yaml/badge.svg?branch=master)](https://github.com/evologi/fixed-width/actions/workflows/ci.yaml)

A fixed-width file format toolset with streaming support and flexible options.

- [Features](#features)
- [File format specs](#fixed-width-file-format)
- [Install](#install)
- [Usage](#usage)
  - [In memory parsing](#parsestringorbuffer-options)
  - [Parse with Node.js streams](#parserstreamoptions)
  - [Custom parsing](#new-parseroptions)
  - [In memory serializing](#stringifyiterable-options)
  - [Serialize with Node.js streams](#stringifierstreamoptions)
  - [Custom serializing](#new-stringifieroptions)
- [Options](#options)
- [Errors](#errors)

## Features

- **Flexible**: lots of options
- **Zero dependencies**: small footprint
- **Native streaming support**
- **Well tested**: code coverage above 90%
- **Support big datasets**: production ready
- **Native ESM support**: future proof
- **TypeScript support**

## Fixed width file format

The fixed-width file format is a simple method to store data on a file. A separator divides rows (typically a newline char), and all fields have a fixed width on the row.

This format is useful to seek data directly from the file without any parsing procedure, but It has no compression mechanism. This limit will result in an over-usage of memory (both RAM and disk) to store the same data with other file formats (like JSON or XML).

But, in the year [insert current year here] A.D., you can still find someone using such file format. Fear not, my friend! This Node.js library will provide a toolset to parse and generate fixed-width files.

### Example file

The next section will contain an example of fixed-width data with two fields: username and age. The first field has a width of 12 chars, and the second one has a width of 3 chars right-aligned.

```
username    age
alice       024
bob         030
```

## Install

```
npm i @evologi/fixed-width
```

## Usage

```javascript
import { parse, stringify, Parser, Stringifier } from '@evologi/fixed-width'
```

### `parse(input, options)`

It parses a text or a buffer into an array of elements. If It receives string of buffer It returns an array of all parsed items. It It receives an iterable (could be `async`), returns the same type of iterable inputted.

- `input` `<String> | <Buffer> | <Iterable> | <AsyncIterable>` The raw data to parse.
- `options` `<Object>` See [options section](#options).
- Returns: `<Array> | <Iterable> | <AsyncIterable>`

```javascript
import { parse } from '@evologi/fixed-width'

const text = 'alice       024\nbob         030\n'

const users = parse(text, {
  eol: '\n',
  fields: [
    {
      property: 'username',
      width: 12
    },
    {
      cast: value => parseInt(value, 10),
      property: 'age',
      width: 3
    }
  ]
})

// [{ username: 'alice', age: 24 }, { username: 'bob', age: 30 }]
console.log(users)
```

### `stringify(input, options)`

It serializes an array of elements into a string. If the argument is an array, the output will be a string. The whole conversion is performed at the moment and in-memory. If the argument is some kind of iterable (sync or async), the output will be the same kind of inputted iterable.

- `input` `<Array> | <Iterable> | <AsyncIterable>`
- `options` `<Object>` See [options section](#options).
- Returns: `<String> | <Iterable> | <AsyncIterable>`

```javascript
import { stringify } from '@evologi/fixed-width'

const users = [
  { username: 'alice', age: 24 },
  { username: 'bob', age: 30 }
]

const text = stringify(users, {
  eof: true,
  eol: '\n',
  fields: [
    {
      align: 'left',
      property: 'username',
      width: 12
    },
    {
      align: 'right',
      pad: '0',
      property: 'age',
      width: 3
    }
  ]
})

// 'alice       024\nbob         030\n'
console.log(text)
```

### `Parser.stream(options)`

It returns a [`Transform stream`](https://nodejs.org/api/stream.html#class-streamtransform) that accepts strings (or buffers) as input and emits the parsed elements.

- `options` `<Object>` See [options section](#options).
- Returns: [`<Transform>`](https://nodejs.org/api/stream.html#class-streamtransform)

```javascript
import { Parser } from '@evologi/fixed-width'

const stream = Parser.stream({
  eol: '\n',
  fields: [
    {
      property: 'username',
      width: 12
    },
    {
      cast: value => parseInt(value, 10),
      property: 'age',
      width: 3
    }
  ]
})

stream
  .on('error', err => console.error(err))
  .on('data', data => console.log(data))
  .on('end', () => console.log('end'))

stream.write('alice       024\nbob         030')
stream.end()
```

### `Stringifier.stream(options)`

It returns a [`Transform stream`](https://nodejs.org/api/stream.html#class-streamtransform) that accepts objects (o arrays) as input and emits the serialized strings.

- `options` `<Object>` See [options section](#options).
- Returns: [`<Transform>`](https://nodejs.org/api/stream.html#class-streamtransform)

```javascript
import { Stringifier } from '@evologi/fixed-width'

const stream = Stringifier.stream({
  eof: true,
  eol: '\n',
  fields: [
    {
      align: 'left',
      property: 'username',
      width: 12
    },
    {
      align: 'right',
      pad: '0',
      property: 'age',
      width: 3
    }
  ]
})

let text = ''

stream
  .on('error', err => console.error(err))
  .on('data', data => { text += data.toString() })
  .on('end', () => {
    // 'alice       024\nbob         030\n'
    console.log(text)
  })

stream.write({ username: 'alice', age: 24 })
stream.write({ username: 'bob', age: 30 })
stream.end()
```

### `new Parser(options)`

It creates a `Parser` instance. This object is useful when a custom optimized procedure is necessary. This object is used internally by the Node.js stream and the `parse()` function.

- `options` `<Object>` See [options section](#options).
- Returns: `<Parser>`

It consists of only two methods, and all those methods are strictly synchronous.

#### `Parser#write(stringOrBuffer)`

It accepts a string or a buffer as input and returns an iterable that outputs all parsed objects up to the last completed row (line).

- `stringOrBuffer` `<String> | <Buffer>`
- Returns: `<Iterable>`

#### `Parser#end()`

It resets the `Parser` status and returns an iterable that could output other objects contained on the last partial row (line).

- Returns: `<Iterable>`

```javascript
import { Parser } from '@evologi/fixed-width'

const parser = new Parser({
  eol: '\n',
  fields: [
    {
      property: 'username',
      width: 12
    },
    {
      cast: value => parseInt(value, 10),
      property: 'age',
      width: 3
    }
  ]
})

const users = Array.from(
  parser.write('alice       024\nbob         030')
).concat(
  Array.from(
    parser.end()
  )
)

// [{ username: 'alice', age: 24 }, { username: 'bob', age: 30 }]
console.log(users)
```

### `new Stringifier(options)`

It creates a `Stringifier` instance. This object is useful when a custom optimized procedure is necessary. This object is used internally by the Node.js stream and the `stringify()` function.

- `options` `<Object>` See [options section](#options).
- Returns: `<Stringifier>`

It consists of only two methods, and all those methods are strictly synchronous.

#### `Stringifier#write(obj)`

Push an object to serialize. Returns the serialized text of the passed object, including new line terminators.

- `obj` `<*>`
- Returns: `<String>`

#### `Stringifier#end()`

Close the parsing and returns a final string.

- Returns: `<String>`

```javascript
import { Stringifier } from '@evologi/fixed-width'

const stringifier = new Stringifier({
  eof: true,
  eol: '\n',
  fields: [
    {
      align: 'left',
      property: 'username',
      width: 12
    },
    {
      align: 'right',
      pad: '0',
      property: 'age',
      width: 3
    }
  ]
})

let text = ''
text += stringifier.write({ username: 'alice', age: 24 })
text += stringifier.write({ username: 'bob', age: 30 })
text += stringifier.end()

// 'alice       024\nbob         030\n'
console.log(text)
```

## Options

### `encoding`

Type: `<String>`

Default: `"uft8"`

The encoding used to handle strings and buffers. Only [Node.js encodings](https://nodejs.org/api/buffer.html#buffers-and-character-encodings) are supported.

### `eol`

Type: `<String>`

The **E**nd **O**f **L**ine character that divides record rows.
It will defautl to [`os.EOL`](https://nodejs.org/api/os.html#oseol) for serialization.
For parsing, the `Parser` will try to guess the correct line separator.

### `eof`

Type: `<Boolean>`

Default: `true`

Appends the **E**nd **O**f **F**ile char. If `true`, an [End Of Line](#eol) character is added at the end of the file.

### `pad`

Type: `string`

Default: `" "`

Values shorter than their field's width will be padded with this value while serializing. It's also the trimming value removed while parsing.

See [`trim`](#trim), [`field.pad`](#fieldpad), and [`field.align`](#fieldalign) options.

### `trim`

Type: `<Boolean> | <String>`

Default: `true`

It enables or disables values' trimming while parsing. Possibles values are:

- `true`: Trim the value from both sides (left and right)
- `false`: Disable trim 
- `"left"`: Trim value's left side
- `"right"`: Trim value's right side
- `"auto"`: Guess the trim side by looking at field's [`align`](#fieldalign) option:
  - if `field.align: "right"` then trim left
  - if `field.align: "left"` then trim right

> The trimmed value corresponds to the field's [padding value](#pad).

```javascript
trim('004200', { pad: '0', trim: 'right' })
// the trimmed value will be '0042'
```

### `from`

Type: `<Number>`

Default: `1`

The first line to consider while parsing (inclusive).
It is a **1-based** integer (`1` is the first line).

### `to`

Type: `<Number>`

Default: `Infinity`

The last line to consider while parsing (inclusive).
It is a **1-based** integer (`1` is the first line).

### `allowLongerLines`

Type: `<Boolean>`

Allow lines to be longer than the declared fields while parsing.

Default: `true`

### `allowShorterLines`

Type: `<Boolean>`

Allow lines to be shorter than the declared fields while parsing.

Default: `false`

### `skipEmptyLines`

Type: `<Boolean>`

Completely ignore all empty lines. This options does **not** change the behaviour of the `allowShorterLines` option.

Default: `true`

### `fields`

Type: `<Array>`

This option is the only required one. It contains the specs for all the fields.

#### `field.align`

Type: `<String>`

Default: `"left"`

Field's value alignment. Can be `"left"` or `"right"`.

#### `field.cast`

Type: `<Function>`

A casting function that accepts the raw string value and returns the parsed one. It also provides a context object as second argument. Only used while parsing.

```javascript
const options = {
  fields: [
    {
      width: 5,
      cast: (value, ctx) => {
        // value is always a string
        // ctx = { column: 1, line: 1, width: 5 }
        return parseInt(value)
      }
    }
  ]
}
```

#### `field.column`

Type: `<Number>`

Field's columns. This is **1-based** value (one is the first column). It defaults to the sum of all the previously defined fields' widths.

#### `field.pad`

Type: `<Number>`

Field level padding value. It defaults to the [global](#pad) one.

#### `field.property`

Type: `<String> | <Symbol>`

This option controls the expected format of both input and output objects.

##### Parsing

By defining this option, the `Parser` will emit objects. If the option is omitted, the emitted values will be arrays.

##### Serializing

By defining this option, the `Stringifier` will expect objects. If the option is omitted, the expected values will be arrays.

#### `field.width`

Type: `<Number>`

Field's width. Required.

#### `field.stringify`

Type: `<Function>`

A function that converts the field into a custom string. Useful for custom formatting, for example, formatting a Date into DD/MM/YYYY format. Only used while stringifying.

#### `field.trim`

Type: `<Boolean> | <String>`

Default: fallbacks to global [`trim`](#trim) option value.

Field-level `trim` option.
See [`trim` option](#trim) docs.

## Errors

All errors that can occur during the parsing or serializing phase contain an error code. Error objects also contain enough info (properties) to debug the problem.

It's possible to detect custom fixed-width errors with their constructor:

```javascript
import { FixedWidthError } from '@evologi/fixed-width'

try {
  // parse or stringify...
} catch (err) {
  if (err instanceof FixedWidthError) {
    console.log(err.code)
  } else {
    console.log('UNKNOWN_ERROR')
  }
}
```

### `UNEXPECTED_LINE_LENGTH`

This error is raised when a partial line is found.

You can suppress this error with [`allowLongerLines`](#allowLongerLines) or [`allowShorterLines`](#allowShorterLines) options.

### `EXPECTED_STRING_VALUE`

This error is raised when a value cannot be serialized into a string.

### `FIELD_VALUE_OVERFLOW`

This error is raised when a string value has a width that exceeds its field's width.

---
_Source: https://npm.io/package/@evologi/fixed-width · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
