Node.js readline模塊與util模塊的用法

本篇文章主要介紹了Node.js readline模塊與util模塊的使用,寫的十分的全面細緻,具備必定的參考價值,對此有須要的朋友能夠參考學習下。若有不足之處,歡迎批評指正。css

#1. 使用readline模塊逐行讀取流數據html

1.1. 建立Interface對象前端

在readline模塊中,經過Interface對象的使用來實現逐行讀取流數據的處理。所以首先要建立Interface對象,在readline模塊中,能夠經過createInterface方法來建立Interface對象.readline.createInterface(options),options爲一個對象,屬性以下vue

  • input: 屬性值爲一個可用來讀取流數據的對象,用於指定讀入數據的來源。
  • output: 屬性值爲一個可用來寫入流數據的對象,用於指定數據的輸出目標。
  • computer: 屬性值爲一個函數,用於指定Tab補全處理。函數的參數值被自動設定爲從該行中讀入的Tab字符以前的數據,該函數應該返回一個由全部用於Tab補全時的匹配字符串組成的數組以及從該行中讀入的Tab字符以前的數據。
  • terminal: 該屬性爲一個布爾類型的屬性,當須要像一個終端那樣實時地將輸入數據流進行輸出,且須要在輸出數據中寫入ANSI/VT100控制字符串時,須要將該屬性值設置爲true,默認屬性值等於output屬性值對象的isTTY屬性值。
// 輸入 exit, quit,q這三個任意之一的時候,會退出
const readline = require('readline');
let rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  completer: completer
});//歡迎加入前端全棧開發交流圈一塊兒吹水聊天學習交流:864305860
rl.on('line', (line) => {
  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]
}//歡迎加入前端全棧開發交流圈一塊兒吹水聊天學習交流:864305860
複製代碼

1.2. 使用Interface對象逐行讀取文件 原fs.js文件的內容node

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');
複製代碼

代碼內容webpack

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() + ': */');
  }//歡迎加入前端全棧開發交流圈一塊兒吹水聊天學習交流:864305860
});
複製代碼

生成的anotherFs.js文件的內容web

/*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'); 格式化字符串中,可使用的參數指定符號面試

  • %s:用於指定字符串參數
  • %d:用於指定數值參數,包括整數及浮點數
  • %j:用於指定一個JSON對象
  • %%:用於指定一個百分號
  • 若是格式化字符串中使用的參數個數多於format方法中使用的除了format參數以外的其餘參數,則格式化字符串中多於的參數將不被替換.console.log(util.format('%s:%s','one'));
  • 若是格式化字符串中使用的參數個數少於format方法中使用的除了format參數以外的其餘參數,則根據format方法中多於參數值的類型自動將其轉換爲字符串,中間使用一個空格進行分割. +inspect(object,[options])返回一個字符串,該字符串包含了對象的信息,在調試應用程序的過程當中很是有用.
  • showHidden<boolean>若是爲true,則object的不可枚舉的符號與屬性也會被包括在格式化後的結果中.默認爲false.
  • depth<number>指定格式化object時遞歸的次數.這對查看大型複雜對象頗有用.默認爲2.若要無限地遞歸則傳入null.
  • colors<boolean>若是爲true,則輸出樣式使用ANSI顏色代碼.默認爲false.顏色可自定義.
  • customInspect<boolean>若是爲false,則object上自定義的inspect(depth,opts)函數不會被調用.默認爲true.
  • showProxy<boolean>若是爲true,則Proxy對象的對象和函數會展現它們的targethandler對象.默認爲false.
  • maxArrayLength<number>指定格式化時數組和TypedArray元素能包含的最大數量.默認爲100.設爲null則顯式所有數組元素.設爲0*或負數則不顯式數組元素.
  • breakLength<number>一個對象的鍵被拆分紅多行的長度.設爲Infinity則格式化一個對象爲單行.默認爲60. +自定義util.inspect顏色 能夠經過util.inspect.styles和util.inspect.colors屬性全局地自定義util.inspect的顏色輸出(若是已啓用)
const util = require('util');
console.log(util.format('您輸入了%d個參數,參數值分別爲%s,%s,%s', 3, 'nice', 'excelent', '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'}));
//歡迎加入前端全棧開發交流圈一塊兒吹水聊天學習交流:864305860 
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] } } }
 * **///歡迎加入前端全棧開發交流圈一塊兒吹水聊天學習交流:864305860
複製代碼

結語數組

感謝您的觀看,若有不足之處,歡迎批評指正。bash

本次給你們推薦一個免費的學習羣,裏面歸納移動應用網站開發,css,html,webpack,vue node angular以及面試資源等。 對web開發技術感興趣的同窗,歡迎加入Q羣:864305860,無論你是小白仍是大牛我都歡迎,還有大牛整理的一套高效率學習路線和教程與您免費分享,同時天天更新視頻資料。 最後,祝你們早日學有所成,拿到滿意offer,快速升職加薪,走上人生巔峯。

相關文章
相關標籤/搜索