搜索功能实现步骤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)
})
复制代码
进行搜索的对象文件接口
好好今天好好里的梅林强无敌好王哈也不赖好仍是咕哒子好好最强好好好赛高
复制代码