Node.js readline模塊與util模塊的使用
1. 使用readline模塊逐行讀取流數(shù)據(jù)
1.1. 創(chuàng)建Interface對(duì)象
在readline模塊中,通過(guò)Interface對(duì)象的使用來(lái)實(shí)現(xiàn)逐行讀取流數(shù)據(jù)的處理。因此首先要?jiǎng)?chuàng)建Interface對(duì)象,在readline模塊中,可以通過(guò)createInterface方法來(lái)創(chuàng)建Interface對(duì)象.readline.createInterface(options),options為一個(gè)對(duì)象,屬性如下
- input: 屬性值為一個(gè)可用來(lái)讀取流數(shù)據(jù)的對(duì)象,用于指定讀入數(shù)據(jù)的來(lái)源。
- output: 屬性值為一個(gè)可用來(lái)寫(xiě)入流數(shù)據(jù)的對(duì)象,用于指定數(shù)據(jù)的輸出目標(biāo)。
- computer: 屬性值為一個(gè)函數(shù),用于指定Tab補(bǔ)全處理。函數(shù)的參數(shù)值被自動(dòng)設(shè)定為從該行中讀入的Tab字符之前的數(shù)據(jù),該函數(shù)應(yīng)該返回一個(gè)由所有用于Tab補(bǔ)全時(shí)的匹配字符串組成的數(shù)組以及從該行中讀入的Tab字符之前的數(shù)據(jù)。
- terminal: 該屬性為一個(gè)布爾類型的屬性,當(dāng)需要像一個(gè)終端那樣實(shí)時(shí)地將輸入數(shù)據(jù)流進(jìn)行輸出,且需要在輸出數(shù)據(jù)中寫(xiě)入ANSI/VT100控制字符串時(shí),需要將該屬性值設(shè)置為true,默認(rèn)屬性值等于output屬性值對(duì)象的isTTY屬性值。
// 輸入 exit, quit,q這三個(gè)任意之一的時(shí)候,會(huì)退出
const readline = require('readline');
let rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
completer: completer
});
rl.on('line', (line) => {
if (line === 'exit' || line === 'quit' || line === 'q') {
rl.close();
} else {
console.log('您輸入了:', line);
}
});
rl.on('close', () => {
console.log('行數(shù)據(jù)讀取操作被終止');
});
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對(duì)象逐行讀取文件
原fs.js文件的內(nèi)容
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');
代碼內(nèi)容
const readline = require('readline');
const fs = require('fs');
let file = fs.createReadStream('./fs.js');
let out = fs.createWriteStream('./anotherFs.js');
let index = 1;
out.write('/*line' + index.toString() + ": */");
let rl = readline.createInterface({
input: file,
output: out,
terminal: true
});
rl.on('line', (line) => {
if (line === '') {
rl.close();
} else {
index++;
out.write('/*line' + index.toString() + ': */');
}
});
生成的anotherFs.js文件的內(nèi)容
/*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語(yǔ)言中的printf方法,將第一個(gè)參數(shù)值作為一個(gè)格式化字符串,將其他參數(shù)值作為該格式化字符串中所使用的各中參數(shù),返回一個(gè)經(jīng)過(guò)格式化處理后的字符串.util.format('您輸入了%d個(gè)參數(shù),參數(shù)值分別為%s,%s,%s',3,'nice','excelent','holy');
格式化字符串中,可以使用的參數(shù)指定符號(hào)
- *`%s`:用于指定字符串參數(shù)
- *`%d`:用于指定數(shù)值參數(shù),包括整數(shù)及浮點(diǎn)數(shù)
- *`%j`:用于指定一個(gè)`JSON`對(duì)象
- *`%%`:用于指定一個(gè)百分號(hào)
- *如果格式化字符串中使用的參數(shù)個(gè)數(shù)多于format方法中使用的除了`format`參數(shù)之外的其他參數(shù),則格式化字符串中多于的參數(shù)將不被替換.`console.log(util.format('%s:%s','one'));`
- *如果格式化字符串中使用的參數(shù)個(gè)數(shù)少于`format`方法中使用的除了`format`參數(shù)之外的其他參數(shù),則根據(jù)`format`方法中多于參數(shù)值的類型自動(dòng)將其轉(zhuǎn)換為字符串,中間使用一個(gè)空格進(jìn)行分割.
+inspect(object,[options])返回一個(gè)字符串,該字符串包含了對(duì)象的信息,在調(diào)試應(yīng)用程序的過(guò)程中非常有用.
- *`showHidden<boolean>`如果為`true`,則`object`的不可枚舉的符號(hào)與屬性也會(huì)被包括在格式化后的結(jié)果中.默認(rèn)為`false.`
- *`depth<number>`指定格式化`object`時(shí)遞歸的次數(shù).這對(duì)查看大型復(fù)雜對(duì)象很有用.默認(rèn)為`2`.若要無(wú)限地遞歸則傳入`null`.
- *`colors<boolean>`如果為`true`,則輸出樣式使用`ANSI`顏色代碼.默認(rèn)為`false`.顏色可自定義.
- *`customInspect<boolean>`如果為`false`,則`object`上自定義的`inspect(depth,opts)`函數(shù)不會(huì)被調(diào)用.默認(rèn)為`true`.
- *`showProxy<boolean>`如果為`true`,則`Proxy`對(duì)象的對(duì)象和函數(shù)會(huì)展示它們的`target`和`handler`對(duì)象.默認(rèn)為`false`.
- *`maxArrayLength<number>`指定格式化時(shí)數(shù)組和`TypedArray`元素能包含的最大數(shù)量.默認(rèn)為`100`.設(shè)為`null`則顯式全部數(shù)組元素.設(shè)為`0*`或負(fù)數(shù)則不顯式數(shù)組元素.
- *`breakLength<number>`一個(gè)對(duì)象的鍵被拆分成多行的長(zhǎng)度.設(shè)為`Infinity`則格式化一個(gè)對(duì)象為單行.默認(rèn)為`60`.
+自定義util.inspect顏色
可以通過(guò)util.inspect.styles和util.inspect.colors屬性全局地自定義util.inspect的顏色輸出(如果已啟用)
const util = require('util');
console.log(util.format('您輸入了%d個(gè)參數(shù),參數(shù)值分別為%s,%s,%s', 3, 'nice', 'excelent', 'holy'));
//您輸入了3個(gè)參數(shù),參數(shù)值分別為nice,excelent,holy
console.log(util.format('一個(gè)JSON對(duì)象%j', {'name': 'jack', 'age': 25}));
// 一個(gè)JSON對(duì)象{"name":"jack","age":25}
console.log(util.format('一個(gè)百分號(hào)%'));// 一個(gè)百分號(hào)%
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] } } }
* **/
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
connect中間件session、cookie的使用方法分享
今天大象哥用了下connect的session和cookie,感覺(jué)還挺好用的,分享一下(里面坑挺多的,文檔寫(xiě)的太模糊了,費(fèi)了哥不少時(shí)間)。2014-06-06
node.js使用express-jwt報(bào)錯(cuò):expressJWT?is?not?a?function解決
這篇文章主要給大家介紹了關(guān)于node.js使用express-jwt報(bào)錯(cuò):expressJWT?is?not?a?function解決的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-03-03
node微信小程序登錄實(shí)現(xiàn)登錄的項(xiàng)目實(shí)踐
登陸流程是指小程序用戶進(jìn)行授權(quán)登陸,即獲取用戶的微信賬號(hào)等信息本文就來(lái)介紹一下node微信小程序登錄實(shí)現(xiàn)登錄,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09

