forked from sindresorhus/map-obj
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
47 lines (39 loc) · 1.37 KB
/
test.js
File metadata and controls
47 lines (39 loc) · 1.37 KB
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
import test from 'ava';
import m from '.';
test('main', t => {
t.is(m({foo: 'bar'}, key => [key, 'unicorn']).foo, 'unicorn');
t.is(m({foo: 'bar'}, (key, val) => ['unicorn', val]).unicorn, 'bar');
t.is(m({foo: 'bar'}, (key, val) => [val, key]).bar, 'foo');
});
test('target option', t => {
const target = {};
t.is(m({foo: 'bar'}, (key, val) => [val, key], {target}), target);
t.is(target.bar, 'foo');
});
test('deep option', t => {
const obj = {one: 1, obj: {two: 2, three: 3}, arr: [{four: 4}, 5]};
const expected = {one: 2, obj: {two: 4, three: 6}, arr: [{four: 8}, 5]};
const fn = (key, val) => [key, typeof val === 'number' ? val * 2 : val];
const actual = m(obj, fn, {deep: true});
t.deepEqual(actual, expected);
});
test('nested arrays', t => {
const obj = {arr: [[0, 1, 2, {a: 3}]]};
const expected = {arr: [[0, 1, 2, {a: 6}]]};
const fn = (key, val) => [key, typeof val === 'number' ? val * 2 : val];
const actual = m(obj, fn, {deep: true});
t.deepEqual(actual, expected);
});
test('handles circular references', t => {
const obj = {one: 1, arr: [2]};
obj.circular = obj;
obj.arr2 = obj.arr;
obj.arr.push(obj);
const fn = (key, val) => [key.toUpperCase(), val];
const actual = m(obj, fn, {deep: true});
const expected = {ONE: 1, ARR: [2]};
expected.CIRCULAR = expected;
expected.ARR2 = expected.ARR;
expected.ARR.push(expected);
t.deepEqual(actual, expected);
});