Skip to content

Commit bc77ea6

Browse files
committedMay 18, 2016
Merge pull request #214 from svozza/keys-values-fn
Add S.keys, S.values and S.pairs
2 parents 4aa1b02 + 7d636b4 commit bc77ea6

File tree

4 files changed

+229
-0
lines changed

4 files changed

+229
-0
lines changed
 

‎index.js

+48
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
//.
2828
//. R.map(S.toUpper, S.head(words))
2929
//.
30+
//. Sanctuary is designed to work in Node.js and in ES5-compatible browsers.
31+
//.
3032
//. ## Types
3133
//.
3234
//. Sanctuary uses Haskell-like type signatures to describe the types of
@@ -2746,6 +2748,52 @@
27462748
return filter(is(type), Just(x));
27472749
});
27482750

2751+
//# keys :: StrMap a -> Array String
2752+
//.
2753+
//. Returns the keys of the given string map, in arbitrary order.
2754+
//.
2755+
//. ```javascript
2756+
//. > S.keys({b: 2, c: 3, a: 1}).sort()
2757+
//. ['a', 'b', 'c']
2758+
//. ```
2759+
S.keys =
2760+
def('keys',
2761+
{},
2762+
[$.StrMap(a), $.Array($.String)],
2763+
Object.keys);
2764+
2765+
//# values :: StrMap a -> Array a
2766+
//.
2767+
//. Returns the values of the given string map, in arbitrary order.
2768+
//.
2769+
//. ```javascript
2770+
//. > S.values({a: 1, c: 3, b: 2}).sort()
2771+
//. [1, 2, 3]
2772+
//. ```
2773+
S.values =
2774+
def('values',
2775+
{},
2776+
[$.StrMap(a), $.Array(a)],
2777+
function(strMap) {
2778+
return Object.keys(strMap).map(function(key) { return strMap[key]; });
2779+
});
2780+
2781+
//# toPairs :: StrMap a -> Array (Pair String a)
2782+
//.
2783+
//. Returns the key–value pairs of the given string map, in arbitrary order.
2784+
//.
2785+
//. ```javascript
2786+
//. > S.pairs({b: 2, a: 1, c: 3}).sort()
2787+
//. [['a', 1], ['b', 2], ['c', 3]]
2788+
//. ```
2789+
S.pairs =
2790+
def('pairs',
2791+
{},
2792+
[$.StrMap(a), $.Array($.Pair($.String, a))],
2793+
function(strMap) {
2794+
return Object.keys(strMap).map(function(k) { return [k, strMap[k]]; });
2795+
});
2796+
27492797
//. ### Number
27502798

27512799
//# negate :: ValidNumber -> ValidNumber

‎test/keys.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'use strict';
2+
3+
var throws = require('assert').throws;
4+
5+
var eq = require('./utils').eq;
6+
var errorEq = require('./utils').errorEq;
7+
var S = require('..');
8+
9+
10+
describe('keys', function() {
11+
12+
it('is a unary function', function() {
13+
eq(typeof S.keys, 'function');
14+
eq(S.keys.length, 1);
15+
});
16+
17+
it('type checks its arguments', function() {
18+
throws(function() { S.keys('xxx'); },
19+
errorEq(TypeError,
20+
'Invalid value\n' +
21+
'\n' +
22+
'keys :: StrMap a -> Array String\n' +
23+
' ^^^^^^^^\n' +
24+
' 1\n' +
25+
'\n' +
26+
'1) "xxx" :: String\n' +
27+
'\n' +
28+
'The value at position 1 is not a member of ‘StrMap a’.\n'));
29+
30+
throws(function() { S.keys({a: '1', b: 2, c: '3'}); },
31+
errorEq(TypeError,
32+
'Type-variable constraint violation\n' +
33+
'\n' +
34+
'keys :: StrMap a -> Array String\n' +
35+
' ^\n' +
36+
' 1\n' +
37+
'\n' +
38+
'1) "1" :: String\n' +
39+
' 2 :: Number, FiniteNumber, NonZeroFiniteNumber, Integer, ValidNumber\n' +
40+
' "3" :: String\n' +
41+
'\n' +
42+
'Since there is no type of which all the above values are members, the type-variable constraint has been violated.\n'));
43+
});
44+
45+
it("returns an array of the given object's own keys", function() {
46+
eq(S.keys({}), []);
47+
eq(S.keys({a: 1, b: 2, c: 3}).sort(), ['a', 'b', 'c']);
48+
});
49+
50+
it('does not include prototype properties', function() {
51+
var proto = {a: 1, b: 2};
52+
var obj = Object.create(proto);
53+
obj.c = 3;
54+
obj.d = 4;
55+
56+
eq(S.keys(obj).sort(), ['c', 'd']);
57+
});
58+
59+
});

‎test/pairs.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict';
2+
3+
var throws = require('assert').throws;
4+
5+
var eq = require('./utils').eq;
6+
var errorEq = require('./utils').errorEq;
7+
var S = require('..');
8+
9+
10+
describe('toPairs', function() {
11+
12+
var comparePairsAsc = function(a, b) {
13+
return a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0;
14+
};
15+
16+
it('is a unary function', function() {
17+
eq(typeof S.pairs, 'function');
18+
eq(S.pairs.length, 1);
19+
});
20+
21+
it('type checks its arguments', function() {
22+
throws(function() { S.pairs('xxx'); },
23+
errorEq(TypeError,
24+
'Invalid value\n' +
25+
'\n' +
26+
'pairs :: StrMap a -> Array (Pair String a)\n' +
27+
' ^^^^^^^^\n' +
28+
' 1\n' +
29+
'\n' +
30+
'1) "xxx" :: String\n' +
31+
'\n' +
32+
'The value at position 1 is not a member of ‘StrMap a’.\n'));
33+
34+
throws(function() { S.pairs({a: '1', b: 2, c: '3'}); },
35+
errorEq(TypeError,
36+
'Type-variable constraint violation\n' +
37+
'\n' +
38+
'pairs :: StrMap a -> Array (Pair String a)\n' +
39+
' ^\n' +
40+
' 1\n' +
41+
'\n' +
42+
'1) "1" :: String\n' +
43+
' 2 :: Number, FiniteNumber, NonZeroFiniteNumber, Integer, ValidNumber\n' +
44+
' "3" :: String\n' +
45+
'\n' +
46+
'Since there is no type of which all the above values are members, the type-variable constraint has been violated.\n'));
47+
});
48+
49+
it('returns an array with the key value pairs of each property of the object', function() {
50+
eq(S.pairs({}), []);
51+
eq(S.pairs({a: 1, b: 2, c: 3}).sort(comparePairsAsc), [['a', 1], ['b', 2], ['c', 3]]);
52+
});
53+
54+
it('does not include prototype properties', function() {
55+
var proto = {a: 1, b: 2};
56+
var obj = Object.create(proto);
57+
obj.c = 3;
58+
obj.d = 4;
59+
60+
eq(S.pairs(obj).sort(comparePairsAsc), [['c', 3], ['d', 4]]);
61+
});
62+
63+
});

‎test/values.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'use strict';
2+
3+
var throws = require('assert').throws;
4+
5+
var eq = require('./utils').eq;
6+
var errorEq = require('./utils').errorEq;
7+
var S = require('..');
8+
9+
10+
describe('values', function() {
11+
12+
it('is a unary function', function() {
13+
eq(typeof S.values, 'function');
14+
eq(S.values.length, 1);
15+
});
16+
17+
it('type checks its arguments', function() {
18+
throws(function() { S.values('xxx'); },
19+
errorEq(TypeError,
20+
'Invalid value\n' +
21+
'\n' +
22+
'values :: StrMap a -> Array a\n' +
23+
' ^^^^^^^^\n' +
24+
' 1\n' +
25+
'\n' +
26+
'1) "xxx" :: String\n' +
27+
'\n' +
28+
'The value at position 1 is not a member of ‘StrMap a’.\n'));
29+
30+
throws(function() { S.values({a: '1', b: 2, c: '3'}); },
31+
errorEq(TypeError,
32+
'Type-variable constraint violation\n' +
33+
'\n' +
34+
'values :: StrMap a -> Array a\n' +
35+
' ^\n' +
36+
' 1\n' +
37+
'\n' +
38+
'1) "1" :: String\n' +
39+
' 2 :: Number, FiniteNumber, NonZeroFiniteNumber, Integer, ValidNumber\n' +
40+
' "3" :: String\n' +
41+
'\n' +
42+
'Since there is no type of which all the above values are members, the type-variable constraint has been violated.\n'));
43+
});
44+
45+
it("returns an array of the given object's own values", function() {
46+
eq(S.values({}), []);
47+
eq(S.values({a: 1, b: 2, c: 3}).sort(), [1, 2, 3]);
48+
});
49+
50+
it('does not include prototype values', function() {
51+
var proto = {a: 1, b: 2};
52+
var obj = Object.create(proto);
53+
obj.c = 3;
54+
obj.d = 4;
55+
56+
eq(S.values(obj).sort(), [3, 4]);
57+
});
58+
59+
});

0 commit comments

Comments
 (0)
Please sign in to comment.