国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

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

2019-11-19 14:16:03
字體:
來源:轉載
供稿:網友

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

1.1. 創(chuàng)建Interface對象

在readline模塊中,通過Interface對象的使用來實現逐行讀取流數據的處理。因此首先要創(chuàng)建Interface對象,在readline模塊中,可以通過createInterface方法來創(chuàng)建Interface對象.readline.createInterface(options),options為一個對象,屬性如下

  1. input: 屬性值為一個可用來讀取流數據的對象,用于指定讀入數據的來源。
  2. output: 屬性值為一個可用來寫入流數據的對象,用于指定數據的輸出目標。
  3. computer: 屬性值為一個函數,用于指定Tab補全處理。函數的參數值被自動設定為從該行中讀入的Tab字符之前的數據,該函數應該返回一個由所有用于Tab補全時的匹配字符串組成的數組以及從該行中讀入的Tab字符之前的數據。
  4. 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});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]}

1.2. 使用Interface對象逐行讀取文件

原fs.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');

代碼內容

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文件的內容

/*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<boolean>`如果為`true`,則`object`的不可枚舉的符號與屬性也會被包括在格式化后的結果中.默認為`false.`
  2. *`depth<number>`指定格式化`object`時遞歸的次數.這對查看大型復雜對象很有用.默認為`2`.若要無限地遞歸則傳入`null`.
  3. *`colors<boolean>`如果為`true`,則輸出樣式使用`ANSI`顏色代碼.默認為`false`.顏色可自定義.
  4. *`customInspect<boolean>`如果為`false`,則`object`上自定義的`inspect(depth,opts)`函數不會被調用.默認為`true`.
  5. *`showProxy<boolean>`如果為`true`,則`Proxy`對象的對象和函數會展示它們的`target`和`handler`對象.默認為`false`.
  6. *`maxArrayLength<number>`指定格式化時數組和`TypedArray`元素能包含的最大數量.默認為`100`.設為`null`則顯式全部數組元素.設為`0*`或負數則不顯式數組元素.
  7. *`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,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'}));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] } } } * **/

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 舞阳县| 龙里县| 黄骅市| 集安市| 通州区| 娄底市| 岗巴县| 高青县| 深水埗区| 南雄市| 永顺县| 平和县| 沅江市| 兴业县| 视频| 萍乡市| 阜宁县| 浮山县| 屏南县| 通江县| 肥乡县| 临沭县| 西吉县| 阳谷县| 南丰县| 文山县| 库伦旗| 河南省| 巴林左旗| 融水| 商洛市| 长顺县| 三亚市| 商丘市| 光泽县| 永善县| 互助| 社会| 龙江县| 锡林浩特市| 裕民县|