-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
142 lines (120 loc) · 4.75 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const test = require('ava');
const decamelcaseKeys = require('.');
test('main', t => {
t.true(decamelcaseKeys({fooBar: true}).foo_bar);
t.true(decamelcaseKeys({FooBar: true}).foo_bar);
t.true(decamelcaseKeys({'foo Bar': true})['foo bar']);
t.true(decamelcaseKeys({'foo-Bar': true})['foo-bar']);
t.true(decamelcaseKeys({thisIsATest: true}).this_is_a_test);
});
test('handles acronyms', t => {
t.true(decamelcaseKeys({myURLString: true}).my_url_string);
t.true(decamelcaseKeys({URLString: true}).url_string);
t.true(decamelcaseKeys({StringURL: true}).string_url);
t.true(decamelcaseKeys({testGUILabel: true}).test_gui_label);
t.true(decamelcaseKeys({CAPLOCKED1: true}).caplocked1);
t.true(decamelcaseKeys({UPPERCASE: true}).uppercase);
t.true(decamelcaseKeys({'UPPER CASE': true})['upper case']);
});
test('without changes', t => {
t.true(decamelcaseKeys({'foo-bar': true})['foo-bar']);
// eslint-disable-next-line camelcase
t.true(decamelcaseKeys({foo_bar: true}).foo_bar);
t.true(decamelcaseKeys({_id: true})._id);
t.deepEqual(
decamelcaseKeys({foo: true, obj: {deepOption: 'not set'}}),
{foo: true, obj: {deepOption: 'not set'}}
);
});
test('exclude option', t => {
t.deepEqual(decamelcaseKeys({fooBar: true}, {exclude: [/^f/]}), {fooBar: true});
t.deepEqual(
decamelcaseKeys({fooBar: true, barFoo: false}, {exclude: ['barFoo']}),
// eslint-disable-next-line camelcase
{foo_bar: true, barFoo: false}
);
});
test('deep option', t => {
t.deepEqual(
decamelcaseKeys({fooBar: true, obj: {oneTwo: false, arr: [{threeFour: true}]}}, {deep: true}),
// eslint-disable-next-line camelcase
{foo_bar: true, obj: {one_two: false, arr: [{three_four: true}]}}
);
});
test('stopPaths option', t => {
t.deepEqual(
decamelcaseKeys({fooBar: true, obj: {oneTwo: false, arr: [{threeFour: true}]}}, {deep: true, stopPaths: ['obj']}),
// eslint-disable-next-line camelcase
{foo_bar: true, obj: {oneTwo: false, arr: [{threeFour: true}]}}
);
t.deepEqual(
decamelcaseKeys({fooBar: true, obj: {oneTwo: false, arr: [{threeFour: true}]}}, {deep: true, stopPaths: ['obj.arr']}),
// eslint-disable-next-line camelcase
{foo_bar: true, obj: {one_two: false, arr: [{threeFour: true}]}}
);
t.deepEqual(
decamelcaseKeys({qWE: [[{fooBar: 1}, {oneTwo: 2}, {fooBar: 3, oneTwo: 4}]]}, {deep: true, stopPaths: ['qWE.fooBar']}),
// eslint-disable-next-line camelcase
{q_we: [[{foo_bar: 1}, {one_two: 2}, {foo_bar: 3, one_two: 4}]]}
);
t.deepEqual(
decamelcaseKeys({aB: 1, aC: {cD: 1, cE: {eF: 1}}}, {deep: true, stopPaths: ['aC.cE']}),
// eslint-disable-next-line camelcase
{a_b: 1, a_c: {c_d: 1, c_e: {eF: 1}}}
);
});
test('separator option only', t => {
t.true(decamelcaseKeys({NewFooBar: true}, {separator: '-'})['new-foo-bar']);
t.true(decamelcaseKeys({thisIsATest: true}, {separator: ' '})['this is a test']);
t.true(decamelcaseKeys({thisIsATest: true}, {separator: ''}).thisisatest);
t.true(decamelcaseKeys({unicornRainbow: true}, {separator: '|'})['unicorn|rainbow']);
t.true(decamelcaseKeys({thisHasSpecialCharactersLikeČandŠ: true}, {separator: ' '})['this has special characters like čand š']);
});
test('separator and deep options', t => {
t.deepEqual(
decamelcaseKeys({PFooBar: true, PObj: {PTwo: false, PArr: [{PThreeFour: true}]}}, {deep: true, separator: '-'}),
{'p-foo-bar': true, 'p-obj': {'p-two': false, 'p-arr': [{'p-three-four': true}]}}
);
});
test('handles nested arrays', t => {
t.deepEqual(
decamelcaseKeys({qWE: [['a', 'b']]}, {deep: true}),
// eslint-disable-next-line camelcase
{q_we: [['a', 'b']]}
);
});
test('accepts an array of objects', t => {
t.deepEqual(
decamelcaseKeys([{fooBar: true}, {barFoo: false}]),
// eslint-disable-next-line camelcase
[{foo_bar: true}, {bar_foo: false}]
);
});
test('different separator option values', t => {
t.true(decamelcaseKeys({fooBarUPPERCASE: true}).foo_bar_uppercase);
t.true(decamelcaseKeys({fooBarUPPERCASE: true}, {separator: '-'})['foo-bar-uppercase']);
t.true(decamelcaseKeys({fooBarUPPERCASE: true}, {separator: '.'})['foo.bar.uppercase']);
t.deepEqual(
decamelcaseKeys({PFooBar: true, PObj: {PTwo: false, PArr: [{PThreeFour: true}]}}, {deep: true, separator: '-'}),
{'p-foo-bar': true, 'p-obj': {'p-two': false, 'p-arr': [{'p-three-four': true}]}}
);
t.deepEqual(
decamelcaseKeys({PFooBar: true, PObj: {PTwo: false, PArr: [{PThreeFour: true}]}}, {deep: true}),
// eslint-disable-next-line camelcase
{p_foo_bar: true, p_obj: {p_two: false, p_arr: [{p_three_four: true}]}}
);
});
test('handle array of non-objects', t => {
const input = ['fooBar', 'barFoo'];
t.deepEqual(
decamelcaseKeys(input),
input
);
});
test('handle array of non-objects with `deep` option', t => {
const input = ['fooBar', 'barFoo'];
t.deepEqual(
decamelcaseKeys(input, {deep: true}),
input
);
});