微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Node.js readline模块与util模块的使用

1. 使用readline模块逐行读取流数据

1.1. 创建Interface对象

在readline模块中,通过Interface对象的使用来实现逐行读取流数据的处理。因此首先要创建Interface对象,在readline模块中,可以通过createInterface方法来创建Interface对象.readline.createInterface(options),options为一个对象,属性如下

  1. input: 属性值为一个可用来读取流数据的对象,用于指定读入数据的来源。
  2. output: 属性值为一个可用来写入流数据的对象,用于指定数据的输出目标。
  3. computer: 属性值为一个函数,用于指定Tab补全处理。函数的参数值被自动设定为从该行中读入的Tab字符之前的数据,该函数应该返回一个由所有用于Tab补全时的匹配字符串组成的数组以及从该行中读入的Tab字符之前的数据。
  4. terminal: 该属性一个布尔类型的属性,当需要像一个终端那样实时地将输入数据流进行输出,且需要在输出数据中写入ANSI/VT100控制字符串时,需要将该属性值设置为true,属性值等于output属性值对象的isTTY属性值。
{ if (line === 'exit' || line === 'quit' || line === 'q') { rl.close(); } else { console.log('您输入了:',line); } });

rl.on('close',() => {
console.log('行数据读取操作被终止');
});

function completer(line) {
const completions = '.help .error .exit .quit .q'.split(' ');
let hits = completions.filter((c) => {
return c.indexOf(line) === 0;
});
return [hits.length ? hits : completions,line]
}

1.2. 使用Interface对象逐行读取文件

原fs.js文件内容

rush:js;"> console.log('this is line 1'); console.log('this is line 2'); console.log('this is line 3'); console.log('this is line 4'); console.log('this is line 5');

代码内容

{ if (line === '') { rl.close(); } else { index++; out.write('/*line' + index.toString() + ': */'); } });

生成的anotherFs.js文件内容

rush:js;"> /*line1: */console.log('this is line 1'); /*line2: */console.log('this is line 2'); /*line3: */console.log('this is line 3'); /*line4: */console.log('this is line 4'); /*line5: */console.log('this is line 5');/*line6: */

2. 使用util模块中提供的一些方法

+format方法

类似于C语言中的printf方法,将第一个参数值作为一个格式化字符串,将其他参数值作为该格式化字符串中所使用的各中参数,返回一个经过格式化处理后的字符串.util.format('您输入了%d个参数,参数值分别为%s,%s,%s',3,'nice','excelent','holy'); 格式化字符串中,可以使用的参数指定符号

  1. *`%s`:用于指定字符串参数
  2. *`%d`:用于指定数值参数,包括整数及浮点数
  3. *`%j`:用于指定一个`JSON`对象
  4. *`%%`:用于指定一个百分号
  5. *如果格式化字符串中使用的参数个数多于format方法中使用的除了`format`参数之外的其他参数,则格式化字符串中多于的参数将不被替换.`console.log(util.format('%s:%s','one'));`
  6. *如果格式化字符串中使用的参数个数少于`format`方法中使用的除了`format`参数之外的其他参数,则根据`format`方法中多于参数值的类型自动将其转换为字符串,中间使用一个空格进行分割.

+inspect(object,[options])返回一个字符串,该字符串包含了对象的信息,在调试应用程序的过程中非常有用.

  1. *`showHidden`如果为`true`,则`object`的不可枚举的符号与属性也会被包括在格式化后的结果中.认为`false.`
  2. *`depth`指定格式化`object`时递归的次数.这对查看大型复杂对象很有用.认为`2`.若要无限地递归则传入`null`.
  3. *`colors`如果为`true`,则输出样式使用`ANSI`颜色代码.认为`false`.颜色可自定义.
  4. *`customInspect`如果为`false`,则`object`上自定义的`inspect(depth,opts)`函数不会被调用.认为`true`.
  5. *`showProxy`如果为`true`,则`Proxy`对象的对象和函数会展示它们的`target`和`handler`对象.认为`false`.
  6. *`maxArrayLength`指定格式化时数组和`TypedArray`元素能包含的最大数量.认为`100`.设为`null`则显式全部数组元素.设为`0*`或负数则不显式数组元素.
  7. *`breakLength`一个对象的键被拆分成多行的长度.设为`Infinity`则格式化一个对象为单行.认为`60`.

+自定义util.inspect颜色

可以通过util.inspect.styles和util.inspect.colors属性全局地自定义util.inspect的颜色输出(如果已启用)

rush:js;"> const util = require('util'); console.log(util.format('您输入了%d个参数,参数值分别为%s,'holy')); //您输入了3个参数,参数值分别为nice,excelent,holy console.log(util.format('一个JSON对象%j',{'name': 'jack','age': 25})); // 一个JSON对象{"name":"jack","age":25} console.log(util.format('一个百分号%'));// 一个百分号% console.log(util.format('%s:%s','one'));// one:%s console.log(util.format('%s','one','two','three',{'name': 'jack'}));

function test(one,two) {
return one + two;
}

let parent = new Object();
parent.name = 'parent';
parent.func = test;

let child1 = new Object();
child1.name = 'child1';
parent.child1 = child1;

let child2 = new Object();
child2.name = 'child2';
child1.child = child2;

let child3 = new Object();
child3.name = 'child3';
child2.child = child3;

child2.inspect = function (depth) {
return util.inspect(this,{depth: depth - 2,customInspect: false})
};
console.log(util.inspect(parent,{customInspect: true,depth: 4}));
/**

  • { name: 'parent', func: [Function: test], child1:
  • { name: 'child1',* child: { name: 'child2',child: [Object],inspect: [Function] } } }
  • **/

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐