Skip to content

Commit

Permalink
Add S.fromPairs
Browse files Browse the repository at this point in the history
  • Loading branch information
futpib committed Nov 1, 2017
1 parent 0a8fdfc commit 48c13f6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
25 changes: 25 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3778,6 +3778,31 @@
S.pairs =
def('pairs', {}, [$.StrMap(a), $.Array($.Pair($.String, a))], pairs);

//# fromPairs :: Foldable f => f (Pair String a) -> StrMap a
//.
//. Returns a string map containing the key–value pairs specified by the
//. given [Foldable][]. If a key appears in multiple pairs, the rightmost
//. pair takes precedence.
//.
//. ```javascript
//. > S.fromPairs([['a', 1], ['b', 2], ['c', 3]])
//. {a: 1, b: 2, c: 3}
//.
//. > S.fromPairs([['x', 1], ['x', 2]])
//. {x: 2}
//. ```
function fromPairs(pairs) {
return Z.reduce(function(strMap, pair) {
strMap[pair[0]] = pair[1];
return strMap;
}, {}, pairs);
}
S.fromPairs =
def('fromPairs',
{f: [Z.Foldable]},
[f($.Pair($.String, a)), $.StrMap(a)],
fromPairs);

//. ### Number

//# negate :: ValidNumber -> ValidNumber
Expand Down
19 changes: 19 additions & 0 deletions test/fromPairs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

var S = require('..');

var eq = require('./internal/eq');


test('fromPairs', function() {

eq(typeof S.fromPairs, 'function');
eq(S.fromPairs.length, 1);
eq(S.fromPairs.toString(), 'fromPairs :: Foldable f => f (Pair String a) -> StrMap a');

eq(S.fromPairs([]), {});
eq(S.fromPairs([['a', 1], ['b', 2], ['c', 3]]), {a: 1, b: 2, c: 3});
eq(S.fromPairs({x: ['a', 1], y: ['b', 2], z: ['c', 3]}), {a: 1, b: 2, c: 3});
eq(S.fromPairs([['x', 1], ['x', 2]]), {x: 2});

});

0 comments on commit 48c13f6

Please sign in to comment.