Skip to content
This repository has been archived by the owner on May 23, 2022. It is now read-only.

Commit

Permalink
Auto-lifts values into promises.
Browse files Browse the repository at this point in the history
  • Loading branch information
robotlolita committed Apr 1, 2013
1 parent 763d7f9 commit 4ca9c13
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 43 deletions.
8 changes: 5 additions & 3 deletions README.md
Expand Up @@ -13,18 +13,20 @@ general :D


```js ```js
var noisyo = require('noisyo') var noisyo = require('noisyo')
var spit = noisyo.spit
var slurp = noisyo.slurp


// Slurp takes a Stream and returns a Promise of its contents. // Slurp takes a Stream and returns a Promise of its contents.
// Spit takes a Stream, and some contents, and returns a Promise of the // Spit takes a Stream, and some contents, and returns a Promise of the
// eventual draining. // eventual draining.
noisyo.slurp(process.stdin).then(noisyo.spit(process.stdout)) spit(process.stdout, slurp(process.stdin))


// So, why not just `.pipe()` you faggot? // So, why not just `.pipe()` you faggot?
// Well, these work with both Strings and Streams interchangeably. // Well, these work with both Strings and Streams interchangeably.
var input = noisyo.slurp(process.stdin) var input = slurp(process.stdin)
input.then(function(data) { input.then(function(data) {
console.log(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 // Obviously, you *will* want to use Streams directly if you're interested in
Expand Down
5 changes: 3 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{ {
"name": "noisyo", "name": "noisyo",
"version": "0.1.0", "version": "0.1.1",
"description": "Drinks and vomits Stream contents. Promised to be really noisy.", "description": "Drinks and vomits Stream contents. Promised to be really noisy.",
"main": "./lib/index.js", "main": "./lib/index.js",
"devDependencies": { "devDependencies": {
Expand Down Expand Up @@ -40,6 +40,7 @@
"node": ">=0.10.0" "node": ">=0.10.0"
}, },
"dependencies": { "dependencies": {
"pinky": "~0.1.2" "pinky": "~0.1.2",
"pinky-combinators": "~0.1.1"
} }
} }
41 changes: 24 additions & 17 deletions src/index.ls
Expand Up @@ -26,6 +26,7 @@


### -- Dependencies ---------------------------------------------------- ### -- Dependencies ----------------------------------------------------
pinky = require 'pinky' pinky = require 'pinky'
{all} = require 'pinky-combinators'
stream = require 'stream' stream = require 'stream'




Expand Down Expand Up @@ -100,29 +101,29 @@ write-to = (dest, source, encoding) ->
#### λ slurp #### λ slurp
# Drains the contents of a Stream and returns a promise for it. # Drains the contents of a Stream and returns a promise for it.
# #
# :: Stream -> Promise String Error # :: Promise Stream a -> Promise String Error
slurp = (source) -> slurp = (source-p) ->
promise = pinky! promise = pinky!
data = (source.read! || '').to-string! data = ''


source.on 'readable' grab-chunk (pinky source-p).then (source) ->
source.on 'error' handle-failure data := ((source.read 0) or '').to-string!
source.on 'end' fulfill
source.on 'end' clean-up source.on 'readable' (grab-chunk source)
source.on 'end' fulfill
source.on 'end' (clean-up source)


return promise return promise


function grab-chunk()
function grab-chunk(source) => ->
chunk = source.read! chunk = source.read!
if chunk isnt null => data += chunk.to-string! if chunk isnt null => data += chunk.to-string!


function handle-failure(err)
promise.reject err

function fulfill() function fulfill()
promise.fulfill data promise.fulfill data


function clean-up() function clean-up(source) => ->
source.remove-listener 'readable' grab-chunk source.remove-listener 'readable' grab-chunk
source.remove-listener 'end' fulfill source.remove-listener 'end' fulfill
source.remove-listener 'end' clean-up source.remove-listener 'end' clean-up
Expand All @@ -131,13 +132,19 @@ slurp = (source) ->
#### λ spit #### λ spit
# Vomits some contents (String or Stream) into a Stream. # Vomits some contents (String or Stream) into a Stream.
# #
# :: Stream -> String -> Promise Stream Error # :: Promise Stream a -> Promise String a -> Promise Stream Error
# :: Stream -> Stream -> Promise Stream Error # :: Promise Stream a -> Promise Stream a -> Promise Stream Error
spit = (dest, source) --> spit = (dest-p, source-p) -->
| is-stream source => pipe-from source, dest promise = pinky!
| otherwise => write-to dest, source, arguments[2]


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 --------------------------------------------------------- ### -- Exports ---------------------------------------------------------
Expand Down
34 changes: 13 additions & 21 deletions test/suite.ls
Expand Up @@ -11,29 +11,25 @@ x = xit


duplex = -> duplex = ->
d = new stream.Duplex d = new stream.Duplex
d.name = '<no>'
data = '' data = ''
ready = false d._write = (buf, enc, f) ->
d.write = (buf, enc, f) -> data := buf.to-string!
data := buf d.push data
process.next-tick -> do process.next-tick -> do
ready := true d.push null
if f => f! if f => f!
d.emit 'readable'



d._read = (n) -> do

if d.haz-error => d.emit 'error', (new Error 'no u')
d.read = -> data
if ready
process.next-tick -> d.emit 'end'
ready := false
return data
else
return null


return d return d


d = null d = null
before-each -> d := duplex! before-each ->
d := duplex!
d.name = '<d>'


text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.' text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'


Expand All @@ -43,24 +39,20 @@ describe 'λ slurp' ->
d.write text d.write text
expect (slurp d) .to.become 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 'λ spit' ->
describe 'with Stream' -> describe 'with Stream' ->
o 'Should fulfill after contents have been piped.' (done) -> o 'Should fulfill after contents have been piped.' (done) ->
t = duplex! t = duplex!
d.write text 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.' -> o 'Should fulfill after source is read if it\'s a standard output stream.' ->
d.write text d.write text
expect (spit process.stdout, d) .to.be.fulfilled expect (spit process.stdout, d) .to.be.fulfilled


describe 'with Strings' -> describe 'with Strings' ->
o 'Should fulfill after writing to the source.' -> 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




0 comments on commit 4ca9c13

Please sign in to comment.