Skip to content

Instantly share code, notes, and snippets.

@kalinchernev
Created February 4, 2017 00:03
Show Gist options
  • Save kalinchernev/15cbee0731612dc6388c2f0e1cafa6b7 to your computer and use it in GitHub Desktop.
Save kalinchernev/15cbee0731612dc6388c2f0e1cafa6b7 to your computer and use it in GitHub Desktop.
Simple function finding all files of a given extension with EventEmitter
// 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