Skip to content

Commit

Permalink
0.0.3 - add some mapP methods. Specs and descriptions pushing soon
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanCannon committed Aug 6, 2015
1 parent b4b555c commit e95ffe4
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -78,3 +78,6 @@ otherwise resolve `deferred` with `data`.
});
});
```

## TODO
- Need specs and descriptions for new mapP methods
22 changes: 22 additions & 0 deletions lib/methods/mapObjP.js
@@ -0,0 +1,22 @@
'use strict';

var R = require('ramda'),
Q = require('q');

var mapPairs = require('./mapPairs.js');

// (a -> Promise b) -> [(c, a)] -> Promise [(c, b)]
var mapPairsP = R.curry(function(f, pairs) {
var pairsWithPromiseAsSecondValue = mapPairs(f, pairs),
pairPromises = pairsWithPromiseAsSecondValue,
promiseOfPairs = Q.all(pairPromises);
return promiseOfPairs;
});

// (a -> Promise b) -> {c: a} -> Promise {c: b}
var mapObjP = R.curry(function(f, obj) {
var pairs = R.toPairs(obj);
return mapPairsP(f, pairs).then(R.fromPairs);
});

module.exports = mapObjP;
12 changes: 12 additions & 0 deletions lib/methods/mapP.js
@@ -0,0 +1,12 @@
'use strict';

var R = require('ramda'),
Q = require('q');


// (a -> Promise b) -> [a] -> Promise [b]
var mapP = R.curry(function(f, vs) {
return Q.all(R.map(f, vs));
});

module.exports = mapP;
16 changes: 16 additions & 0 deletions lib/methods/mapPairs.js
@@ -0,0 +1,16 @@
'use strict';

var R = require('ramda'),
Q = require('q');

// (a -> b) -> (c, a) -> (c, b)
var callOnPair = R.curry(function(f, pair) {
return [pair[0], f(pair[1])];
});

// (a -> b) -> [(c, a)] -> [(c, b)]
var mapPairs = R.curry(function(f, pairs) {
return R.map(callOnPair(f), pairs);
});

module.exports = mapPairs;
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name" : "alien-node-q-utils",
"version" : "0.0.2",
"version" : "0.0.3",
"description" : "Helper functions for promises with Q on NodeJS",
"main" : "lib/Promises.js",
"dependencies" : {
Expand Down

0 comments on commit e95ffe4

Please sign in to comment.