Skip to content

Instantly share code, notes, and snippets.

@kalinchernev
Last active February 4, 2017 01:14
Show Gist options
  • Save kalinchernev/223e3170b53307b5ca0e3d02afcd93ea to your computer and use it in GitHub Desktop.
Save kalinchernev/223e3170b53307b5ca0e3d02afcd93ea to your computer and use it in GitHub Desktop.
'use strict'
// Dependencies
const EventEmitter = require('events').EventEmitter
const path = require('path')
// Definition
class FindFiles extends EventEmitter {
constructor (extension) {
super()
this.extension = extension
this.files = []
}
addFile (file) {
this.files.push(file)
return this
}
// Check for matches
findFiles () {
process.nextTick(() => {
this.files.forEach(file => {
if (path.extname(file) === this.extension) {
this.emit('match', file)
}
})
})
return this
}
}
// Instantiation of observable object
const FindFilesSearcher = new FindFiles('.js')
// Implementation
FindFilesSearcher
.addFile('file1.js')
.addFile('file2.md')
.addFile('file3.js')
.findFiles()
.on('match', console.log)
.on('error', err => {
return console.error(err)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment