Skip to content

Commit

Permalink
async mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonpecora committed May 14, 2015
1 parent e157cae commit 68c6532
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 12 deletions.
31 changes: 21 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,34 @@ function map(fn) {
}))
}

function postMap(file, next, contents, mapped) {
if (mapped === undefined) mapped = contents
if (file.isBuffer()) file.contents = new Buffer(mapped)
if (file.isStream()) file.contents = from([mapped])

push(file, next)
}

function map(file, next, contents) {
file = file.clone()
contents = arguments.length < 3
? file.contents
: contents

try {
var mapped = fn(contents, file.path)
} catch(err) {
return stream.emit('error', err)
}

if (mapped === undefined) mapped = contents
if (file.isBuffer()) file.contents = new Buffer(mapped)
if (file.isStream()) file.contents = from([mapped])
if (fn.length === 3) {
fn(contents, file.path, function(err, mapped) {
if (err) stream.emit('error', err)
else postMap(file, next, contents, mapped)
})
} else {
try {
var mapped = fn(contents, file.path)
} catch(err) {
return stream.emit('error', err)
}

push(file, next)
postMap(file, next, contents, mapped)
}
}

function push(file, next) {
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
"new-from": "0.0.3"
},
"devDependencies": {
"concat-stream": "^1.4.8",
"gulp": "~3.5.2",
"tape": "~2.3.2",
"vinyl": "~0.2.1",
"uglify-js": "~2.4.12",
"gulp": "~3.5.2"
"vinyl": "~0.2.1"
},
"scripts": {
"test": "node test"
Expand Down
38 changes: 38 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,41 @@ test('first thrown error in sync mapper gets emitted as an error', function(t) {
stream.write(new File({ contents: new Buffer(' ') }))
stream.end()
})

test('async with third arg passed in', function(t) {
t.plan(5)

var contents = fs.readFileSync(__filename)
var file = new File({
contents: contents
})

var stream = map(function(src, filename, done) {
t.ok(Buffer.isBuffer(src), 'Buffer.isBuffer(contents)')
t.equal(String(contents), String(src), 'Buffer contents are correct')
done(null, String(src).toUpperCase())
}).on('data', function(file) {
t.ok(Buffer.isBuffer(file.contents), 'output contents are a buffer')
t.equal(String(file.contents), String(contents).toUpperCase())
})

stream.once('end', function() {
t.pass('Reached "end" event')
}).end(file)
})

test('async emits error if passed to done', function(t) {
t.plan(1)

var stream = map(function(src, filename, done) {
done(new Error('should be caught'))
}).on('error', function(err) {
t.ok(err, 'error was caught and emitted')
}).on('data', function() {
t.fail('should not get emitted as "data"')
})

stream.write(new File({ contents: new Buffer(' ') }))
stream.write(new File({ contents: new Buffer(' ') }))
stream.end()
})

0 comments on commit 68c6532

Please sign in to comment.