Skip to content

Commit

Permalink
add S.keys, S.values and S.pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
svozza committed May 18, 2016
1 parent 4aa1b02 commit 4bc304d
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 0 deletions.
50 changes: 50 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
//.
//. R.map(S.toUpper, S.head(words))
//.
//. Sanctuary is designed to work in Node.js and in ES5-compatible browsers.
//.
//. ## Types
//.
//. Sanctuary uses Haskell-like type signatures to describe the types of
Expand Down Expand Up @@ -2746,6 +2748,54 @@
return filter(is(type), Just(x));
});

//# keys :: StrMap a -> Array String
//.
//. Returns the keys of the given string map, in arbitrary order.
//.
//. ```javascript
//. > S.keys({a: 1, b: 2, c: 3})
//. ['a', 'b', 'c']
//. ```
S.keys =
def('keys',
{},
[$.StrMap(a), $.Array($.String)],
Object.keys);

//# values :: StrMap a -> Array a
//.
//. Returns the values of the given string map, in arbitrary order.
//.
//. ```javascript
//. > S.values({a: 1, b: 2, c: 3})
//. [1, 2, 3]
//. ```
S.values =
def('values',
{},
[$.StrMap(a), $.Array(a)],
function(strMap) {
var xs = Object.keys(strMap);
for (var idx = 0; idx < xs.length; idx += 1) xs[idx] = strMap[xs[idx]];
return xs;
});

//# toPairs :: StrMap a -> Array (Pair String a)
//.
//. Returns the key–value pairs of the given string map, in arbitrary order.
//.
//. ```javascript
//. > S.pairs({a: 1, b: 2, c: 3})
//. [['a', 1], ['b', 2], ['c', 3]]
//. ```
S.pairs =
def('pairs',
{},
[$.StrMap(a), $.Array($.Pair($.String, a))],
function(strMap) {
return Object.keys(strMap).map(function(k) { return [k, strMap[k]]; });
});

//. ### Number

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

var throws = require('assert').throws;

var eq = require('./utils').eq;
var errorEq = require('./utils').errorEq;
var S = require('..');


describe('keys', function() {

it('is a unary function', function() {
eq(typeof S.keys, 'function');
eq(S.keys.length, 1);
});

it('type checks its arguments', function() {
throws(function() { S.keys('xxx'); },
errorEq(TypeError,
'Invalid value\n' +
'\n' +
'keys :: StrMap a -> Array String\n' +
' ^^^^^^^^\n' +
' 1\n' +
'\n' +
'1) "xxx" :: String\n' +
'\n' +
'The value at position 1 is not a member of ‘StrMap a’.\n'));

throws(function() { S.keys({a: '1', b: 2, c: '3'}); },
errorEq(TypeError,
'Type-variable constraint violation\n' +
'\n' +
'keys :: StrMap a -> Array String\n' +
' ^\n' +
' 1\n' +
'\n' +
'1) "1" :: String\n' +
' 2 :: Number, FiniteNumber, NonZeroFiniteNumber, Integer, ValidNumber\n' +
' "3" :: String\n' +
'\n' +
'Since there is no type of which all the above values are members, the type-variable constraint has been violated.\n'));
});

it("returns an array of the given object's own keys", function() {
eq(S.keys({}), []);
eq(S.keys({a: 1, b: 2, c: 3}).sort(), ['a', 'b', 'c']);
});

it('does not include prototype properties', function() {
var proto = {a: 1, b: 2};
var obj = Object.create(proto);
obj.c = 3;
obj.d = 4;

eq(S.keys(obj).sort(), ['c', 'd']);
});

});
63 changes: 63 additions & 0 deletions test/pairs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict';

var throws = require('assert').throws;

var eq = require('./utils').eq;
var errorEq = require('./utils').errorEq;
var S = require('..');


describe('toPairs', function() {

var comparePairsAsc = function(a, b) {
return a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0;
};

it('is a unary function', function() {
eq(typeof S.pairs, 'function');
eq(S.pairs.length, 1);
});

it('type checks its arguments', function() {
throws(function() { S.pairs('xxx'); },
errorEq(TypeError,
'Invalid value\n' +
'\n' +
'pairs :: StrMap a -> Array (Pair String a)\n' +
' ^^^^^^^^\n' +
' 1\n' +
'\n' +
'1) "xxx" :: String\n' +
'\n' +
'The value at position 1 is not a member of ‘StrMap a’.\n'));

throws(function() { S.pairs({a: '1', b: 2, c: '3'}); },
errorEq(TypeError,
'Type-variable constraint violation\n' +
'\n' +
'pairs :: StrMap a -> Array (Pair String a)\n' +
' ^\n' +
' 1\n' +
'\n' +
'1) "1" :: String\n' +
' 2 :: Number, FiniteNumber, NonZeroFiniteNumber, Integer, ValidNumber\n' +
' "3" :: String\n' +
'\n' +
'Since there is no type of which all the above values are members, the type-variable constraint has been violated.\n'));
});

it('returns an array with the key value pairs of each property of the object', function() {
eq(S.pairs({}), []);
eq(S.pairs({a: 1, b: 2, c: 3}).sort(comparePairsAsc), [['a', 1], ['b', 2], ['c', 3]]);
});

it('does not include prototype properties', function() {
var proto = {a: 1, b: 2};
var obj = Object.create(proto);
obj.c = 3;
obj.d = 4;

eq(S.pairs(obj).sort(comparePairsAsc), [['c', 3], ['d', 4]]);
});

});
59 changes: 59 additions & 0 deletions test/values.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';

var throws = require('assert').throws;

var eq = require('./utils').eq;
var errorEq = require('./utils').errorEq;
var S = require('..');


describe('values', function() {

it('is a unary function', function() {
eq(typeof S.values, 'function');
eq(S.values.length, 1);
});

it('type checks its arguments', function() {
throws(function() { S.values('xxx'); },
errorEq(TypeError,
'Invalid value\n' +
'\n' +
'values :: StrMap a -> Array a\n' +
' ^^^^^^^^\n' +
' 1\n' +
'\n' +
'1) "xxx" :: String\n' +
'\n' +
'The value at position 1 is not a member of ‘StrMap a’.\n'));

throws(function() { S.values({a: '1', b: 2, c: '3'}); },
errorEq(TypeError,
'Type-variable constraint violation\n' +
'\n' +
'values :: StrMap a -> Array a\n' +
' ^\n' +
' 1\n' +
'\n' +
'1) "1" :: String\n' +
' 2 :: Number, FiniteNumber, NonZeroFiniteNumber, Integer, ValidNumber\n' +
' "3" :: String\n' +
'\n' +
'Since there is no type of which all the above values are members, the type-variable constraint has been violated.\n'));
});

it("returns an array of the given object's own values", function() {
eq(S.values({}), []);
eq(S.values({a: 1, b: 2, c: 3}).sort(), [1, 2, 3]);
});

it('does not include prototype values', function() {
var proto = {a: 1, b: 2};
var obj = Object.create(proto);
obj.c = 3;
obj.d = 4;

eq(S.values(obj).sort(), [3, 4]);
});

});

0 comments on commit 4bc304d

Please sign in to comment.