搜索功能實現步驟bash
1.打開文件函數
2.觸發newListener事件並採用flowing模式讀取數據測試
3.對數據進行過濾對符合搜索內容的次數計數ui
let SearchTime = require('./SearchTimes')
let reader = new SearchTime('./1.txt',{
text:'好好',//搜索內容
highWaterMark:30//最高水位線
})
reader.on('searchTimes',(data)=>{//監聽searchTimes 返回文件內容
console.log(data)
})
reader.on('end',data=>{//監聽send 返回文件知足條件個數
console.log('count',data)
})
reader.on('error',(error)=>{
console.log('error',error)
})
複製代碼
引入events,fs。this
定義一個SearchTimes類,並繼承event模塊爲了以後使用事件訂閱發佈,添加傳進來的屬性。spa
當給一個對象添加一個新的監聽函數時候會觸發newListener事件。code
this.on('newListener',(type)=>{
if(type == 'searchTimes'){//若是添加了searchTimes和監聽,就開始以flowing模式讀取文件內容
this.read();
}
})
複製代碼
let howMuchToRead = this.end?Math.min(this.end-this.pos+1,this.highWaterMark):this.highWaterMark
複製代碼
SearchTimes類對象
let EventEmitter = require('events')
let fs = require('fs')
class SearchTimes extends EventEmitter {
constructor(path, options) {
super(path, options);
this.path = path;
this.text = options.text || '';
this.highWaterMark = options.highWaterMark || 64 * 1024;
this.buffer = Buffer.alloc(this.highWaterMark);
this.flags = options.flags || 'r';
this.encoding = options.encoding || 'utf-8';
this.mode = options.mode || 0o666;
this.start = options.start || 0;
this.end = options.end;
this.pos = this.start;
this.autoClose = options.autoClose || true;
this.buffers = '';
this.on('newListener',(type)=>{
if(type == 'searchTimes'){
this.read();
}
})
this.open();
}
read(){
if(typeof this.fd != 'number'){
return this.once('open',()=>this.read())
}
let howMuchToRead = this.end?Math.min(this.end-this.pos+1,this.highWaterMark):this.highWaterMark
fs.read(this.fd,this.buffer,0,howMuchToRead,this.pos,(err,bytes)=>{
if(err){
if(this.autoClose)
this.destroy()
return this.emit('err',err)
}
if(bytes){
let data = this.buffer.slice(0,bytes)
this.pos += bytes
data = this.encoding?data.toString(this.encoding):data
this.emit('searchTimes',data)
this.buffers += data
if(this.end && this.pos > this.end){
return this.endFn();
}else{
this.read();
}
}else{
return this.endFn();
}
})
}
getPlaceholderCount(strSource,text) {
var thisCount = 0;
strSource.replace(new RegExp(this.unicode(text),'g'), function (m, i) {
thisCount++;
});
return thisCount;
}
unicode(str){
var value='';
for (var i = 0; i < str.length; i++) {
value += '\\u' + this.left_zero_4(parseInt(str.charCodeAt(i)).toString(16));
}
return value;
}
left_zero_4(str) {
if (str != null && str != '' && str != 'undefined') {
if (str.length == 2) {
return '00' + str;
}
}
return str;
}
endFn(){
const count = this.getPlaceholderCount(this.buffers,this.text)
this.emit('end',count);
this.destroy();
}
open(){
fs.open(this.path,this.flags,this.mode,(err,fd)=>{
if(err){
if(this.autoClose){
this.destroy()
return this.emit('err',err)
}
}
this.fd = fd
this.emit('open')
})
}
destroy(){
fs.close(this.fd,(err)=>{
this.emit('close')
})
}
}
module.exports = SearchTimes
複製代碼
調用文件繼承
let SearchTime = require('./SearchTimes')
let reader = new SearchTime('./1.txt',{
text:'好好',//搜索內容
highWaterMark:30//最高水位線
})
reader.on('searchTimes',(data)=>{//監聽searchTimes 返回文件內容
console.log(data)
})
reader.on('end',data=>{//監聽send 返回文件知足條件個數
console.log('count',data)
})
reader.on('error',(error)=>{
console.log('error',error)
})
複製代碼
進行搜索的對象文件接口
好好今天好好裏的梅林強無敵好王哈也不賴好仍是咕噠子好好最強好好好賽高
複製代碼