diff --git a/README.md b/README.md index 4557c3e..131ba3d 100644 --- a/README.md +++ b/README.md @@ -13,18 +13,20 @@ general :D ```js var noisyo = require('noisyo') +var spit = noisyo.spit +var slurp = noisyo.slurp // Slurp takes a Stream and returns a Promise of its contents. // Spit takes a Stream, and some contents, and returns a Promise of the // eventual draining. -noisyo.slurp(process.stdin).then(noisyo.spit(process.stdout)) +spit(process.stdout, slurp(process.stdin)) // So, why not just `.pipe()` you faggot? // Well, these work with both Strings and Streams interchangeably. -var input = noisyo.slurp(process.stdin) +var input = slurp(process.stdin) input.then(function(data) { console.log(data) - noisyo.spit(fs.createWriteStream('foo.txt'), data) + spit(fs.createWriteStream('foo.txt'), data) }) // Obviously, you *will* want to use Streams directly if you're interested in diff --git a/package.json b/package.json index 0b825dd..1f6ed54 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "noisyo", - "version": "0.1.0", + "version": "0.1.1", "description": "Drinks and vomits Stream contents. Promised to be really noisy.", "main": "./lib/index.js", "devDependencies": { @@ -40,6 +40,7 @@ "node": ">=0.10.0" }, "dependencies": { - "pinky": "~0.1.2" + "pinky": "~0.1.2", + "pinky-combinators": "~0.1.1" } } diff --git a/src/index.ls b/src/index.ls index 8f5292a..22e281f 100644 --- a/src/index.ls +++ b/src/index.ls @@ -26,6 +26,7 @@ ### -- Dependencies ---------------------------------------------------- pinky = require 'pinky' +{all} = require 'pinky-combinators' stream = require 'stream' @@ -100,29 +101,29 @@ write-to = (dest, source, encoding) -> #### λ slurp # Drains the contents of a Stream and returns a promise for it. # -# :: Stream -> Promise String Error -slurp = (source) -> +# :: Promise Stream a -> Promise String Error +slurp = (source-p) -> promise = pinky! - data = (source.read! || '').to-string! + data = '' - source.on 'readable' grab-chunk - source.on 'error' handle-failure - source.on 'end' fulfill - source.on 'end' clean-up + (pinky source-p).then (source) -> + data := ((source.read 0) or '').to-string! + + source.on 'readable' (grab-chunk source) + source.on 'end' fulfill + source.on 'end' (clean-up source) return promise - function grab-chunk() + + function grab-chunk(source) => -> chunk = source.read! if chunk isnt null => data += chunk.to-string! - function handle-failure(err) - promise.reject err - function fulfill() promise.fulfill data - function clean-up() + function clean-up(source) => -> source.remove-listener 'readable' grab-chunk source.remove-listener 'end' fulfill source.remove-listener 'end' clean-up @@ -131,13 +132,19 @@ slurp = (source) -> #### λ spit # Vomits some contents (String or Stream) into a Stream. # -# :: Stream -> String -> Promise Stream Error -# :: Stream -> Stream -> Promise Stream Error -spit = (dest, source) --> - | is-stream source => pipe-from source, dest - | otherwise => write-to dest, source, arguments[2] +# :: Promise Stream a -> Promise String a -> Promise Stream Error +# :: Promise Stream a -> Promise Stream a -> Promise Stream Error +spit = (dest-p, source-p) --> + promise = pinky! + all [dest-p, source-p] .then ([dest, source]) -> + p2 = switch + | is-stream source => pipe-from source, dest + | otherwise => write-to dest, source, arguments[2] + p2.then promise.fulfill, promise.reject + + return promise ### -- Exports --------------------------------------------------------- diff --git a/test/suite.ls b/test/suite.ls index 0fd268e..a849bde 100644 --- a/test/suite.ls +++ b/test/suite.ls @@ -11,29 +11,25 @@ x = xit duplex = -> d = new stream.Duplex + d.name = '' data = '' - ready = false - d.write = (buf, enc, f) -> - data := buf + d._write = (buf, enc, f) -> + data := buf.to-string! + d.push data process.next-tick -> do - ready := true + d.push null if f => f! - d.emit 'readable' - - - d.read = -> - if ready - process.next-tick -> d.emit 'end' - ready := false - return data - else - return null + d._read = (n) -> do + if d.haz-error => d.emit 'error', (new Error 'no u') + data return d d = null -before-each -> d := duplex! +before-each -> + d := duplex! + d.name = '' text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.' @@ -43,17 +39,13 @@ describe 'λ slurp' -> d.write text expect (slurp d) .to.become text - x 'Should fail if the stream errors.' -> - d.write text - d.haz-error = true - expect (slurp d) .to.be.rejected.with /no u/ describe 'λ spit' -> describe 'with Stream' -> o 'Should fulfill after contents have been piped.' (done) -> t = duplex! d.write text - expect ((spit t, d).then slurp) .to.become text + expect (slurp (spit t, d)) .to.become text o 'Should fulfill after source is read if it\'s a standard output stream.' -> d.write text @@ -61,6 +53,6 @@ describe 'λ spit' -> describe 'with Strings' -> o 'Should fulfill after writing to the source.' -> - expect ((spit d, text).then slurp) .to.become text + expect (slurp (spit d, text)) .to.become text