当前位置: 首页 > 后端技术 > Node.js

Node.js学习之路20——readline模块和util模块

时间:2023-04-03 11:38:20 Node.js

1.1.1使用readline模块逐行读取流数据。创建Interface对象在readline模块中,使用Interface对象实现数据的逐行读取流处理。因此,必须首先创建接口对象。在readline模块中,可以通过createInterface方法创建Interface对象。readline.createInterface(options),options是一个对象,属性如下input:属性值是一个对象,可以用来读取流数据。用于指定读取数据的来源。output:属性值是一个可用于写入流数据的对象,用于指定数据的输出目标。computer:属性值是一个函数,用于指定Tab补全的处理。该函数的参数值自动设置为从该行读取的Tab字符之前的数据,该函数应返回一个数组,该数组由所有用于Tab补全的匹配字符串和从该行读取的Tab字符之前的数据组成。terminal:这个属性是一个布尔属性。当需要像终端一样实时输出输入数据流,并且需要在输出数据中写入ANSI/VT100控制字符串时,需要将该属性的值设置为true,默认属性值为等于输出属性值对象的isTTY属性值。//当输入exit,quit,q中的任意一个时,都会退出});rl.on('line',(line)=>{if(line==='exit'||line==='quit'||line==='q'){rl.close();}else{console.log('Youentered:',line);}});rl.on('close',()=>{console.log('line数据读取操作终止');});函数完成器(行){constcompletions='.help.error.exit.quit.q'.split('');让hits=completions.filter((c)=>{returnc.indexOf(line)===0;});返回[hits.length?点击:完成,行]}1.2。使用Interface对象逐行读取原始fs.js文件的内容console.log('thisisline1');console.log('thisisline2');console.log('thisisline3');console.log('这是第4行');console.log('这是第5行');代码内容constreadline=require('readline');constfs=require('fs');letfile=fs.createReadStream('./fs.js');letout=fs.createWriteStream('./anotherFs.js');letindex=1;out.write('/*line'+index.toString()+":*/");letrl=readline.createInterface({输入:文件,输出:out,terminal:true});rl.on('line',(line)=>{if(line===''){rl.close();}else{index++;out.write('/*line'+index.toString()+':*/');}});生成的anotherFs.js文件的内容/*line1:*/console.log('thisisline1');/*line2:*/console.log('thisisline2');/*line3:*/console.log('这是第3行');/*line4:*/console.log('这是第4行');/*line5:*/console.log('这是第5行');/*第6行:*/2。使用util模块中提供的一些方法+format方法类似于C语言中的printf方法,第一个参数值作为格式化字符串,其他参数值作为使用的参数格式字符串,并返回格式化的字符串。util.format('你输入了%d个参数,参数值为%s,%s,%s',3,'nice','excelent','holy');在格式字符串中,可以使用参数说明符号*`%s`:用于指定字符串参数*`%d`:用于指定数值参数,包括整数和浮点数*`%j`:用于指定`JSON`对象*`%%`:用于指定百分号*如果格式字符串中使用的参数个数多于格式化方法中除`format`参数以外的参数个数,则参数越多格式字符串不会被替换。`console.log(util.format('%s:%s','one'));`*如果格式字符串中使用的参数个数小于`format`方法中使用的`format`参数以外的参数,会根据大于参数值的`format`方法的类型自动转换为字符串,并用空格隔开。+inspect(object,[options])返回一个字符串,其中包含对象的信息,这在调试应用程序的过程中非常有用。*`showHidden`如果为`true`,那么`object`是不可枚举的符号和属性也会包含在格式化结果中。默认为`false.`*`depth`指定格式化`object`时递归的次数。这对于查看大型和复杂的对象很有用。默认为“2”。如果你想无限递归,传入`null`。*`colors`如果为`true`,则输出样式使用`ANSI`颜色代码。默认值为“假”。颜色可以自定义。*`customInspect`如果为`false`,则不会调用`object`上的自定义`inspect(depth,opts)`函数。默认值为true。*showProxy如果为true,则Proxy对象的对象和函数将显示其target和handler对象。默认值为“假”。*`maxArrayLength`指定数组和`TypedArray`元素在格式化时可以包含的最大数量。默认值为“100”。设置为“null”以显示所有数组元素。设置为“0*”或负数以不显示数组元素。*`breakLength`对象的键被分割成多行的长度。设置为“Infinity”以将对象格式化为单行。默认值为“60”。+自定义util.inspect颜色可以通过util.inspect.styles和util.inspect.colors属性自定义util.inspect的颜色输出(如果启用)constutil=require('util');console.log(util.format('您输入了%d个参数,参数值为%s,%s,%s',3,'nice','excelent','holy'));//你输入了3个参数,参数值分别为nice,excelent,holyconsole.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:%sconsole.log(util.format('%s','one','two','three',{'name':'jack'}));functiontest(one,two){returnone+two;}letparent=newObject();parent.name='parent';parent.func=test;letchild1=newObject();child1.name='child1';parent.child1=child1;letchild2=newObject();child2.name='child2';child1.child=child2;letchild3=newObject();child3.name='child3';child2.child=child3;child2.inspect=function(depth){returnutil.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:[对象],检查:[函数]}}}***/