Created
January 26, 2016 14:12
-
-
Save kwijibo/39f99671572bf9b2f0b5 to your computer and use it in GitHub Desktop.
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
// httpGET :: url -> HttpGET | |
const httpGET = (url) => { | |
let transform = x => x | |
const GET = { | |
url: url, | |
map: (f) => { | |
transform = x => f(transform(x)) | |
return GET | |
}, | |
fork: (reject,resolve) => { | |
request(url, (err,data) => err? reject(err) : resolve(transform(data))) | |
}, | |
resolve: (val) => transform(val) | |
} | |
return GET | |
} | |
const getCatPictures = () => httpGET('http://example.com/cats/pics.json') | |
.map(JSON.parse) | |
.map(imagesToHtml) | |
//app usage | |
getCatPictures() | |
.map(renderInEl('#gallery')) | |
.fork(console.log, console.error) | |
//test usage | |
const testGET = getCatPictures() | |
expect(testGET.url).to.be('http://example.com/cats/pics.json') | |
const fakeResponse = JSON.stringify({image1: 'cat1.jpg', image2: 'cat2.jpg'}) | |
const result = testGET.resolve(fakeResponse) | |
expect(result).to.be('<ul><li><img src=...') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment