forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvertObj.js
38 lines (30 loc) · 1.18 KB
/
invertObj.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
describe('invertObj', function() {
it('takes a list or object and returns an object', function() {
eq(typeof R.invertObj([]), 'object');
eq(typeof R.invertObj({}), 'object');
});
it('returns an empty object when applied to a primitive', function() {
eq(R.invertObj(42), {});
eq(R.invertObj('abc'), {});
});
it('returns an empty object when applied to null/undefined', function() {
eq(R.invertObj(null), {});
eq(R.invertObj(undefined), {});
});
it('returns the input\'s values as keys, and keys as values', function() {
eq(R.invertObj(['a', 'b', 'c']), {a:'0', b:'1', c:'2'});
eq(R.invertObj({x:'a', y:'b', z:'c'}), {a:'x', b:'y', c:'z'});
});
it('prefers the last key found when handling keys with the same value', function() {
eq(R.invertObj(['a', 'b', 'a']), {a:'2', b:'1'});
eq(R.invertObj({x:'a', y:'b', z:'a', _id:'a'}), {a: '_id', b: 'y'});
});
// this one is more of a sanity check
it('is not destructive', function() {
var input = {x:'a', y:'b', z:'a', _id:'a'};
R.invertObj(input);
eq(input, {x:'a', y:'b', z:'a', _id:'a'});
});
});