@evologi/fixed-width

中文文档
1.2.1发布于 2025/09/26MITTypeScriptESM + CJS零依赖近 30 天下载 4.2万

具有流式支持和灵活选项的固定宽度文件格式工具集.

中文使用文档
文档目录27 节
  1. 特征
  2. 固定宽度文件格式
  3. 示例文件
  4. 安装
  5. 使用量
  6. parse(input, options)
  7. stringify(input, options)
  8. Parser.stream(options)
  9. Stringifier.stream(options)
  10. new Parser(options)
  11. new Stringifier(options)
  12. 选项
  13. encoding
  14. eol
  15. eof
  16. pad
  17. trim
  18. from
  19. to
  20. allowLongerLines
  21. allowShorterLines
  22. skipEmptyLines
  23. fields
  24. 错误
  25. UNEXPECTED_LINE_LENGTH
  26. EXPECTED_STRING_VALUE
  27. FIELD_VALUE_OVERFLOW

固定宽度

npm Libraries.io dependency status for latest release JavaScript Style Guide ci

具有流式支持和灵活选项的固定宽度文件格式工具集.

特征

  • 弹性: 很多选项
  • 零依赖关系:小脚印
  • 本地流源支持
  • 测试的很好: 密码覆盖率超过90%
  • 支持大数据集: 生产准备就绪
  • 本地无害环境管理支助: 未来证明
  • 类型脚本支持

固定宽度文件格式

固定宽度文件格式是一种在文件中存储数据的简单方法. 分隔符会分行(一般为新行字符),而所有字段在行上都有固定的宽度.

这种格式在没有任何解析程序的情况下可以直接从文件中查找数据,但它没有压缩机制. 这个限制会导致内存(RAM和磁盘)的过度使用,与其他文件格式(如JSON或XML)一起存储相同的数据.

但是,在这一年[此处插入本年] A.D.,仍然可以找到使用这种文件格式的人. 别害怕,我的朋友! 此 Node.js 库将提供一个工具盘来解析和生成固定宽度的文件 .

示例文件

下一节将包含一个带两个字段的固定宽度数据示例:用户名和年龄. 第一个字段的宽度为12个字符,第二个字段的宽度为3个字符的正对相.

username    age
alice       024
bob         030

安装

npm i @evologi/fixed-width

使用量

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

parse(input, options)

它将文本或缓冲解为一系列元素。 如果它收到缓冲字符串 它返回所有解析项的数组。 它会收到可执行的(可能是) async),返回已输入的同类型可移动输入.

  • input <String> | <Buffer> | <Iterable> | <AsyncIterable> 原始数据来分析.
  • options <Object> 见 选项一节。 。 。 .
  • 回返情况: <Array> | <Iterable> | <AsyncIterable>
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)

它将一系列元素序列化为字符串. 如果参数为数组,输出为字符串. 整个转换在瞬间和记忆中进行. 如果论证是某种可重复的(同步或sync),输出会是同一种可输入可重复的.

  • input <Array> | <Iterable> | <AsyncIterable>
  • options <Object> 见 选项一节。 。 。 .
  • 回返情况: <String> | <Iterable> | <AsyncIterable>
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)

它返回一个 Transform stream ,以接受字符串(或缓冲)作为输入并释放解析出元素.

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)

它返回一个 Transform stream 用于接受对象(o 数组)作为输入并释放串行.

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)

它创造了一个 Parser 举个例子 当需要自定义优化程序时,此对象是有用的 。 此对象被节点.js流和 parse() 函数.

  • options <Object> 见 选项一节。 。 。 .
  • 回返情况: <Parser>

它仅由两种方法组成,所有这些方法都是严格同步的.

Parser#write(stringOrBuffer)

它接受一个字符串或缓冲器作为输入,并返回一个可以输出所有解析对象到最后一行(行)的活性.

  • stringOrBuffer <String> | <Buffer>
  • 回返情况: <Iterable>

Parser#end()

它重装 Parser 状态并返回可输出最后一个部分行(行)所包含其他对象的可操作性.

  • 回返情况: <Iterable>
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)

它创造了一个 Stringifier 举个例子 当需要自定义优化程序时,此对象是有用的 。 此对象被节点.js流和 stringify() 函数.

  • options <Object> 见 选项一节。 。 。 .
  • 回返情况: <Stringifier>

它仅由两种方法组成,所有这些方法都是严格同步的.

Stringifier#write(obj)

按一个对象来序列化 。 返回通过对象的序列文本,包括新的行终止符.

  • obj <*>
  • 回返情况: <String>

Stringifier#end()

关闭解析并返回最后字符串.

  • 回返情况: <String>
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)

选项

encoding

类型 : <String>

默认 : "uft8"

用于处理字符串和缓冲的编码. 仅 节点.js 编码 被支持.

eol

类型 : <String>

这个 页:1无 欧f 时 L内中区分记录行的字符. 它会打倒 os.EOL 用于序列化. 对于解析 Parser 将尝试猜测正确的行分隔符.

eof

类型 : <Boolean>

默认 : true

附加 页:1无 欧f 时 法语单色Char. 若为 true,一个 结束行 在文件末尾添加字符.

pad

类型 : string

默认 : " "

短于其字段宽度的值会在序列化时被添加到此值中. 也是解析时删除的修剪值.

见 trim, (中文(简体) ), field.pad,以及 field.align 选项.

trim

类型 : <Boolean> | <String>

默认 : true

它在解析时启用或禁用值的修剪 。 可能的值是:

  • true: 从两侧( 左右) 调整值
  • false: 禁用修饰
  • "left": 三进制值为左侧
  • "right": Trim 值为右侧
  • "auto":通过看田地来猜测修剪边 align 选项 :
    • 若为 field.align: "right" 然后修剪左边
    • 若为 field.align: "left" 然后右调

修剪值对应字段 嵌入值。 。 。 .

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

from

类型 : <Number>

默认 : 1

第一行在解析时考虑(包含). 这是一个 1个 整数( e)1 为第一行).

to

类型 : <Number>

默认 : Infinity

在解析时要考虑的最后一行( 包含) . 这是一个 1个 整数( e)1 为第一行).

allowLongerLines

类型 : <Boolean>

在解析时允许行长于声明的字段 .

默认 : true

allowShorterLines

类型 : <Boolean>

在解析时允许行比所声明的字段更短.

默认 : false

skipEmptyLines

类型 : <Boolean>

完全忽略所有空行 。 此选项确实 没有 改变 allowShorterLines 选项.

默认 : true

fields

类型 : <Array>

此选项是唯一需要的选项 。 它包含了所有字段的规格.

field.align

类型 : <String>

默认 : "left"

场值对接. 可能会 "left" 或者说 "right"。 。 。 .

field.cast

类型 : <Function>

一个接受原始字符串值并返回已解析的函数。 它还提供了上下文对象作为第二个参数. 只在解析时使用 .

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

类型 : <Number>

战地纵队. 这是 1个 值(一个是第一列)。 它默认为以前定义的所有字段宽度的总和.

field.pad

类型 : <Number>

字段级补丁值 。 它默认到 全球 一个.

field.property

类型 : <String> | <Symbol>

此选项控制输入对象和输出对象的预期格式.

分析

通过定义此选项 Parser 将发出对象。 如果该选项被省略,所释放的值将是数组.

序列化

通过定义此选项 Stringifier 将会需要对象。 如果省去该选项,预期值将是数组.

field.width

类型 : <Number>

田地宽. 要求.

field.stringify

类型 : <Function>

将字段转换成自定义字符串的函数。 用于自定义格式,例如将日期格式化为DD/MM/YYYY格式。 只在字符串时使用 .

field.trim

类型 : <Boolean> | <String>

默认: 回落到全局 trim 选项值.

外地一级 trim 选项. 见 trim 选项 医生.

错误

所有在解析或序列化相中可能发生的错误都包含一个出错代码. 出错对象还包含足够的信息(属性)来调试问题.

可以用其构造器检测自定义的固定宽误差:

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

当找到部分行时, 此错误会上升 .

您可以用 allowLongerLines 或者说 allowShorterLines 选项.

EXPECTED_STRING_VALUE

当一个值不能被串行化为字符串时,这个错误会被提出来.

FIELD_VALUE_OVERFLOW

当一个字符串值的宽度超过其字段的宽度时,这个错误会上升.

版本与发布记录

@evologi/fixed-width 共发布 9 个版本,首次发布于 2022/03/30;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.2.1latest2025/09/261.1 年前
1.2.02025/09/151.2 年前
1.1.02025/06/271.4 年前
1.0.12023/10/203 年前
1.0.02023/06/123 年前
0.3.02022/05/164 年前

关于 @evologi/fixed-width 的常见问题

@evologi/fixed-width 是什么?

具有流式支持和灵活选项的固定宽度文件格式工具集.

如何安装 @evologi/fixed-width?

在项目目录运行 npm install @evologi/fixed-width,或使用 pnpm add / yarn add / bun add。

@evologi/fixed-width 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@evologi/fixed-width 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@evologi/fixed-width 可以商用吗?

@evologi/fixed-width 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@evologi/fixed-width 的下载量有多少?

根据 npm 官方下载统计,@evologi/fixed-width 在 2026/10/14 – 2026/11/12 这 30 天内被下载 42,429 次,日均约 1,414 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@evologi/fixed-width 还在维护吗?

最新版本 1.2.1 发布于 2025/09/26(1.1 年前);至今共发布 9 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/03/30。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。