-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathtest.js
140 lines (124 loc) · 3.67 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
'use strict'
const test = require('tape')
const Snake = require('./')
test(function (t) {
t.deepEqual(Snake({ fooBar: 'baz', nested: { fooBar: 'baz' } }), { foo_bar: 'baz', nested: { foo_bar: 'baz' } })
t.end()
})
test('repeated capital letters', function (t) {
t.deepEqual(Snake({ fooID: 1 }), { foo_id: 1 })
t.end()
})
test('shallow conversion with {deep: false}', function (t) {
t.deepEqual(
Snake({ fooBar: { barBaz: 'qux' } }, { deep: false }),
{ foo_bar: { barBaz: 'qux' } }
)
t.end()
})
test('array of objects', function (t) {
const result = Snake([{ fooBar: 'baz' }])
t.deepEqual(result, [{ foo_bar: 'baz' }])
t.ok(Array.isArray(result))
t.end()
})
test('nested arrays', function (t) {
const result = Snake({ foo: [0, 1, 2] })
t.deepEqual(result, { foo: [0, 1, 2] })
t.ok(Array.isArray(result.foo))
t.end()
})
test('snakecase objects in arrays', function (t) {
const result = Snake({ foo: [0, { fooBar: 'baz', nested: { fooBar: 'baz' } }, 2] })
t.deepEqual(result, { foo: [0, { foo_bar: 'baz', nested: { foo_bar: 'baz' } }, 2] })
t.ok(Array.isArray(result.foo))
t.end()
})
test('exclude', function (t) {
t.deepEqual(
Snake({ fooBar: 'baz', barBaz: 'qux' }, { exclude: ['fooBar'] }),
{ fooBar: 'baz', bar_baz: 'qux' }
)
t.deepEqual(
Snake({ fooBar: 'baz', barBaz: 'qux' }, { exclude: [/^foo/, /^bar/] }),
{ fooBar: 'baz', barBaz: 'qux' }
)
t.end()
})
test('parsing options', function (t) {
const camelCaseRegex = /([a-z])([A-Z])/g
t.deepEqual(
Snake({ 'fooBar.baz': 'qux', 'bar.bazQux': 'foo' }, { parsingOptions: { stripRegexp: camelCaseRegex } }),
{ 'foo_bar.baz': 'qux', 'bar.baz_qux': 'foo' }
)
t.end()
})
test('shouldRecurse option', function (t) {
t.deepEqual(
Snake(
{ fooBar: { barBaz: 'qux' }, nested: { barBaz: 'qux' } },
{ deep: true, shouldRecurse: (key, val) => key !== 'nested' }
),
{ foo_bar: { bar_baz: 'qux' }, nested: { barBaz: 'qux' } }
)
const date = new Date()
t.deepEqual(
Snake(
{ fooBar: { barBaz: 'qux' }, fooDate: date },
{ deep: true, shouldRecurse: (key, val) => !(val instanceof Date) }
),
{
foo_bar: { bar_baz: 'qux' },
foo_date: date
}
)
t.end()
})
test('not a plain object(primitive value)', function (t) {
t.throws(
() => Snake(1),
{ message: 'obj must be an plain object' },
'Should throw an error when input is not a plain object'
)
t.end()
})
test('not a plain object(function value)', function (t) {
t.throws(
() => Snake(() => { return 1 }),
{ message: 'obj must be an plain object' },
'Should throw an error when input is not a plain object'
)
t.end()
})
test('not a plain object(instance value)', function (t) {
t.throws(
() => Snake(new Date()),
{ message: 'obj must be an plain object' },
'Should throw an error when input is not a plain object'
)
t.end()
})
test('not array of plain objects(primitive value)', function (t) {
t.throws(
() => Snake([1, { fooBar: 'baz' }]),
{ message: 'obj must be array of plain objects' },
'Should throw an error when input is not an array of plain objects'
)
t.end()
})
test('not array of plain objects(function value)', function (t) {
t.throws(
() => Snake([() => { return 1 }, { fooBar: 'baz' }]),
{ message: 'obj must be array of plain objects' },
'Should throw an error when input is not an array of plain objects'
)
t.end()
})
test('not array of plain objects(instance value)', function (t) {
t.throws(
() => Snake([new Date(), { fooBar: 'baz' }]),
{ message: 'obj must be array of plain objects' },
'Should throw an error when input is not an array of plain objects'
)
t.end()
})