zsv-lib 中文文档教程

发布于 2年前 浏览 12 项目主页 更新于 2年前

zsv-lib

zsv-lib 是高性能 zsv 解析器库的节点包,用于 CSV 和其他分隔符 或固定宽度的表格文本数据

这是一个用于“非主观”CSV 解析的低级库。请注意相关的 zsv CLI, 它提供了附加功能,例如 ad hoc SQL、标头操作、序列化、扁平化、 转换为 JSON 或 SQLITE3 等,不是此包的一部分,但将来可能会提供 作为单独的 npm 包的一部分。

注意:zsv C 库支持非常广泛的选项;尚未全部暴露于此 包裹。请随时在 https://github.com/liquidaty/zsv/issues 上发布票证 如果您想请求底层库中可用的任何功能,但是 未在此包中公开(或者根本不存在于底层库中)

请(有关性能的进一步说明,请参阅此处)[../README.md#Performance]。

有关 zsv+lib 的更多信息,请访问 https://github.com/liquidaty/zsv

安装

npm install zsv-lib

使用

libzsv 的工作原理大致如下:

  1. 创建自定义行处理函数并使用该函数创建解析器
  2. 通过解析器推送数据块
  3. 解析器为解析的每一行调用您的自定义函数
  4. 您的行处理程序从解析器中提取数据并按照您想要的方式处理它请

参阅源代码(计算行数的简单示例)[test/count.js] 或(另一个将行数据输出为 json 的简单示例)[test/select_all.js]

此包仍处于 ALPHA 状态。示例中使用的 API 预计会发生变化。

API

注:libzsv 支持多种选项;尚未全部暴露于此 包裹。请随时在 https://github.com/liquidaty/zsv/issues 上发布票证 如果您想请求任何功能 要么未从底层库公开,要么尚不存在于 底层库

/**
 * Create a parser
 * @param  rowHandler callback that takes a single (optional) argument
 * @return parser
 */
new(rowHandler, ctx)

/**
 * Incrementally parse a chunk of data
 * @param  parser
 * @param  chunk  byte array of data to parse
 * @return 0 on success, non-zero on error
 */
parseBytes(parser, chunk)

/**
 * Get the number of cells in the current row
 * This function should be called from within your rowHandler callback
 * @param  parser
 * @return number of cells in the current row
 */
cellCount(parser)

/**
 * Get the value of a cell in the current row
 * This function should be called from within your rowHandler callback
 * @param  parser
 * @param  index  0-based index (cell position)
 * @return string value of the cell in the specified index position
 */
getCell(parser, index)

/**
 * Finish parsing, after the final `parseBytes()` call has already been made
 * @param  parser
 */
finish(parser)

/**
 * Destroy a parse that was created with `new()`
 * @param  parser
 */
delete(parser)

/**
 * Queue code to run only after the zsv-lib web assembly module has finished loading
 * @param callback function to call after the zsv-lib module has finished loaded
 */
runOnLoad(callback)

/**
 * Abort parsing prematurely, and do not make any more rowHandler calls
 * @param  parser
 */
abort(z)

贡献

欢迎 Pull 请求。请确保适当更新测试。

许可证

MIT

zsv-lib

zsv-lib is a node package of the high-performance zsv parser library for CSV and other delimited or fixed-width tabular text data

This is a low-level library for "unopinionated" CSV parsing. Note that the related zsv CLI, which offers additional features such as ad hoc SQL, header manipulation, serializing, flattening, converting to JSON or SQLITE3 etc, is not part of this package, but may in the future be offered as part of a separate npm package.

Note: the zsv C library supports a very wide range of options; not all are yet exposed in this package. Please feel free to post a ticket at https://github.com/liquidaty/zsv/issues if you'd like to request any feature that is available in the underlying library but not exposed in this package (or that does not yet exist in the underlying library at all)

Please (see here for further notes re performance)[../README.md#Performance].

For more information about zsv+lib, please visit https://github.com/liquidaty/zsv

Installation

npm install zsv-lib

Usage

libzsv works generally as follows:

  1. Create a custom a row handler function and create a parser with that function
  2. Push chunks of data through the parser
  3. The parser calls your custom function for each row that is parsed
  4. Your row handler extracts data from the parser and processes it however you want

See the source for a (simple example that counts rows)[test/count.js] or (another simple example that outputs row data as json)[test/select_all.js]

THIS PACKAGE IS STILL IN ALPHA. THE API USED IN THE EXAMPLES IS EXPECTED TO CHANGE.

API

Note: libzsv supports a wide range of options; not all are yet exposed in this package. Please feel free to post a ticket at https://github.com/liquidaty/zsv/issues if you'd like to request any feature that is either unexposed from the underlying library, or does not yet exist in the underlying library

/**
 * Create a parser
 * @param  rowHandler callback that takes a single (optional) argument
 * @return parser
 */
new(rowHandler, ctx)

/**
 * Incrementally parse a chunk of data
 * @param  parser
 * @param  chunk  byte array of data to parse
 * @return 0 on success, non-zero on error
 */
parseBytes(parser, chunk)

/**
 * Get the number of cells in the current row
 * This function should be called from within your rowHandler callback
 * @param  parser
 * @return number of cells in the current row
 */
cellCount(parser)

/**
 * Get the value of a cell in the current row
 * This function should be called from within your rowHandler callback
 * @param  parser
 * @param  index  0-based index (cell position)
 * @return string value of the cell in the specified index position
 */
getCell(parser, index)

/**
 * Finish parsing, after the final `parseBytes()` call has already been made
 * @param  parser
 */
finish(parser)

/**
 * Destroy a parse that was created with `new()`
 * @param  parser
 */
delete(parser)

/**
 * Queue code to run only after the zsv-lib web assembly module has finished loading
 * @param callback function to call after the zsv-lib module has finished loaded
 */
runOnLoad(callback)

/**
 * Abort parsing prematurely, and do not make any more rowHandler calls
 * @param  parser
 */
abort(z)

Contributing

Pull requests are welcome. Please make sure to update tests as appropriate.

License

MIT

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文