npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fs-readline

v2.1.0

Published

按行读取文件,基于文件可读流。

Downloads

94

Readme

fs-readline

按行读取文件,基于文件可读流。

Linux Build Windows Build Coverage Status Dependencies node license MIT

安装

$ npm install fs-readline

或者直接从 GitHub 上安装。

$ npm install 52cik/fs-readline

使用

var readLine = require('fs-readline');

var rl = readLine('./somefile.txt');
rl.on('line', function (line, idx) {
  console.log(idx, line);
});

编码处理

node 支持的编码非常有限, 对于那些不支持的编码可以使用一些第三方库转换, 例如 iconv-lite 模块。

PS: 记得加 retainBuffer 参数防止默认 toString 哦。

var readLine = require('fs-readline');
var iconv = require('iconv-lite');

var rl = readline('./gbkfile.txt', {retainBuffer: true}); // buffer 模式
rl.on('line', function (data, idx){
  var line = iconv.decode(data, 'gbk');
  console.log(idx, line);
});

参数说明

仅仅只有 4 个附加参数,其他参数继承自 fs.createReadStream 参数。

retainBuffer

是否保留 Buffer 数据,而不是转为字符串,默认 false 转为字符串。

var readLine = require('fs-readline');

var rl = readLine('./somefile.txt', {retainBuffer: true}); // buffer 模式
rl.on('line', function (data, idx) {
  console.log(idx, data.toString()); // 这里要手动 toString
});

blankLine

blankLine 是否忽略空行,默认 true 不忽略。

假设有个 file.txt 文件,有如下 5 行内容。

// file.txt
111

222

333
var readLine = require('fs-readline');

var rl = readLine('./file.txt', {blankLine: false});
rl.on('line', function (line, idx) {
  console.log(idx, line);
});

// 输出为:
// 1 '111'
// 3 '222'
// 5 '333'

行号依然是对应的行号,如果要得到连续的新行号,请自己计数。

maxLineLength

maxLineLength 行缓冲大小,默认 8k,也就是一行最多只能容纳 8k 的字符内容。

cutMode

截断模式,默认 false,需 maxLineLength 配合使用,直接看例子好了。

假设有个 file.txt 文件,有如下 5 行内容。

// file.txt
111111
22
333333
444
555555
var readLine = require('fs-readline');

var rl = readLine('./file.txt', {cutMode: true, maxLineLength: 4}); // 截断模式,一行最多容纳 4 个字符
rl.on('line', function (line, idx) {
  console.log(idx, line);
});

/**
 * 输出为:
 * 1 '1111'
 * 2 '22'
 * 3 '3333'
 * 4 '444'
 * 5 '5555'
 */

非常简单直观,行长度超过 4 的都被截断了,在某些特定的场景下还是比较适用的。

方法说明

readLine#abort 终止

假设有个 file.txt 文件,有如下 5 行内容。

// file.txt
111111
22
333333
444
555555
var readLine = require('fs-readline');

var rl = readLine('./file.txt');
rl.on('line', function (line, idx) {
  console.log(idx, line);

  if (idx == 3) { // 第三行后停止输出
    this.abort(); // 调用终止方法
  }
}).on('abort', function () {
  console.log('读取已终止');
}).on('close', function () {
  console.log('文件已关闭');
});

/**
 * 输出为:
 * 1 '1111'
 * 2 '22'
 * 3 '3333'
 * 读取已终止
 * 文件已关闭
 */