-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.js
56 lines (46 loc) · 1.64 KB
/
test.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict';
require('mocha');
var assert = require('assert');
var rename = require('./');
describe('rename keys', function() {
it('should throw an error if an object is not passed', function() {
assert.throws(function() {
rename('foo');
});
});
it('should return the original object if no function is passed.', function() {
assert.deepEqual(rename({name: 'rename-keys'}), {name: 'rename-keys'});
});
it('should return original object because nothing is renamed', function() {
assert.deepEqual(rename({a: 1}, function() {}), {a: 1});
});
it('should rename keys.', function() {
var actual = rename({name: 'rename-keys', description: 'foo'}, function(str) {
return '--' + str;
});
assert.deepEqual(Object.keys(actual)[1], '--description');
});
it('should only rename keys that are updated:', function() {
assert.deepEqual(rename({a: 1, b: 1}, function(key) {
if (key === 'b') return 'c';
}), {a: 1, c: 1});
});
it('should rename keys without conflicts', function() {
assert.deepEqual(rename({a: 1, b: 2, c: 3, d: 4}, function(key) {
var renameMap = {a: 'd', b: 'c', c: 'b', d: 'a'};
return renameMap[key];
}), {a: 4, b: 3, c: 2, d: 1});
});
it('should not throw an error when input object does not inherit from Object.prototype', function() {
var input = Object.create(null);
input.a = 0;
rename(input, function(key) {
return key;
});
});
it('should rename key based on value', function() {
assert.deepEqual(rename({a: 1, b: 2, c: 3}, function(key, value) {
if (value > 1) return key + 'x';
}), {a: 1, bx: 2, cx: 3});
});
});