Skip to content

Instantly share code, notes, and snippets.

@joneshf
Forked from dypsilon/reader-t.js
Last active August 10, 2016 01:53
Show Gist options
  • Save joneshf/12d4f9831020a9fa8e7b7a91d56c9c7d to your computer and use it in GitHub Desktop.
Save joneshf/12d4f9831020a9fa8e7b7a91d56c9c7d to your computer and use it in GitHub Desktop.
node_modules
{
"name": "readert",
"version": "1.0.0",
"description": "",
"main": "reader_future.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://gist.github.com/7c638c0dc6289b21af413d6abe3aee9d.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://gist.github.com/7c638c0dc6289b21af413d6abe3aee9d"
},
"homepage": "https://gist.github.com/7c638c0dc6289b21af413d6abe3aee9d",
"dependencies": {
"crypto": "0.0.3",
"fantasy-readers": "0.0.1",
"fluture": "^1.2.1",
"ramda": "^0.22.1"
}
}
/**
* This short program will encrypt the user password
* and insert a new record into a mock database.
*/
const Reader = require('fantasy-readers');
const Future = require('fluture');
const R = require('ramda');
const crypto = require('crypto');
// our mock database
const database = [
{ email: 'user@example.org', password: 'e0538fd8f022bb3b139d72cf12766cb0e31690ff' },
{ email: 'admin@example.org', password: '42c4fbf6fec201c66b82c97833b08d936d2cd526' }
]
// creates a statefull database connection
const connectTo = (db) => {
return {
insert: (doc) => Future.of(db.push(doc)),
get: (i) => Future.of(db[i]),
delete: (i) => Future.of(db.splice(i, 1)),
list: () => Future.of(db)
}
}
// some utility functions
const encrypt = (i) => crypto.createHash('sha1').update(i).digest('hex');
const encPassword = R.evolve({password: encrypt})
// utility function which makes it easier
// to ask for some dependency
// dependencies should reside in an object { dep: obj }
const inject = R.curry((dep, f, reader) => {
return reader.chain((v) => {
return Reader.ask.map((env) => {
return f(env[dep], v);
});
});
});
// this is how you access the cb connection inside the reader
const save = (db, user) => {
db.insert(user);
return db.list();
}
// the body of the program
const handler = R.pipe(
R.map(encPassword), // Reader Object
inject('db', save), // Reader Future Error Object
R.map(R.map(R.map(R.dissoc('password')))) // Reader Future Error Object
);
// this is our db connection now
const dbCon = connectTo(database);
const request = { email: 'new@example.org', password: 'secret' };
const result = handler(Reader.of(request))
.run({ 'db': dbCon }) // this is how you pass the db connection in
.fork(console.error, console.log); // now fork the future
/**
* This short program will encrypt the user password
* and insert a new record into a mock database.
*/
const {ReaderT} = require('fantasy-readers');
const Future = require('fluture');
const R = require('ramda');
const crypto = require('crypto');
const FutureReader = ReaderT(Future);
// our mock database
const database = [
{ email: 'user@example.org', password: 'e0538fd8f022bb3b139d72cf12766cb0e31690ff' },
{ email: 'admin@example.org', password: '42c4fbf6fec201c66b82c97833b08d936d2cd526' }
]
// creates a statefull database connection
const connectTo = (db) => {
return {
insert: (doc) => FutureReader.of(db.push(doc)),
get: (i) => FutureReader.of(db[i]),
delete: (i) => FutureReader.of(db.splice(i, 1)),
list: () => FutureReader.of(db)
}
}
// some utility functions
const encrypt = (i) => crypto.createHash('sha1').update(i).digest('hex');
const encPassword = R.evolve({password: encrypt})
// utility function which makes it easier
// to ask for some dependency
// dependencies should reside in an object { dep: obj }
const inject = R.curry((dep, f, reader) => {
return reader.chain((v) => {
return FutureReader.ask.chain((env) => {
return f(env[dep], v);
});
});
});
// this is how you access the cb connection inside the reader
const save = (db, user) => {
db.insert(user);
return db.list();
}
// the body of the program
const handler = R.pipe(
R.chain(R.compose(FutureReader.of, encPassword)), // Reader Object
inject('db', save), // Reader Future Error Object
R.chain(R.compose(FutureReader.of, R.map(R.dissoc('password')))) // Reader Future Error Object
);
// this is our db connection now
const dbCon = connectTo(database);
const request = { email: 'new@example.org', password: 'secret' };
const result = handler(FutureReader.of(request))
.run({ 'db': dbCon }) // this is how you pass the db connection in
.fork(console.error, console.log); // now fork the future
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment