Last active
February 4, 2017 01:14
-
-
Save kalinchernev/223e3170b53307b5ca0e3d02afcd93ea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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