Created
February 4, 2017 00:03
-
-
Save kalinchernev/15cbee0731612dc6388c2f0e1cafa6b7 to your computer and use it in GitHub Desktop.
Simple function finding all files of a given extension with EventEmitter
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
// Give a list of files all of them which match an extension | |
function findFiles (files, extension) { | |
const emitter = new EventEmitter() | |
if (files.length === 0) { | |
// yield an error | |
emitter.emit('error', 'no files supplied') | |
} | |
// Check for matches | |
function checkFiles () { | |
files.forEach(file => { | |
if (path.extname(file) === extension) { | |
// yield a result | |
emitter.emit('match', file) | |
} | |
}) | |
} | |
// Ask the event loop to loop through our loop ... | |
process.nextTick(checkFiles) | |
// For chainability on on() | |
return emitter | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment