forked from sindresorhus/query-string
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringify.js
More file actions
124 lines (106 loc) · 2.72 KB
/
Copy pathstringify.js
File metadata and controls
124 lines (106 loc) · 2.72 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
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
import test from 'ava';
import fn from '../';
test('stringify', t => {
t.is(fn.stringify({foo: 'bar'}), 'foo=bar');
t.is(fn.stringify({
foo: 'bar',
bar: 'baz'
}), 'bar=baz&foo=bar');
});
test('different types', t => {
t.is(fn.stringify(), '');
t.is(fn.stringify(0), '');
});
test('URI encode', t => {
t.is(fn.stringify({'foo bar': 'baz faz'}), 'foo%20bar=baz%20faz');
t.is(fn.stringify({'foo bar': 'baz\'faz'}), 'foo%20bar=baz%27faz');
});
test('no encoding', t => {
t.is(fn.stringify({'foo:bar': 'baz:faz'}, {encode: false}), 'foo:bar=baz:faz');
});
test('handle array value', t => {
t.is(fn.stringify({
abc: 'abc',
foo: ['bar', 'baz']
}), 'abc=abc&foo=bar&foo=baz');
});
test('array order', t => {
t.is(fn.stringify({
abc: 'abc',
foo: ['baz', 'bar']
}), 'abc=abc&foo=baz&foo=bar');
});
test('handle empty array value', t => {
t.is(fn.stringify({
abc: 'abc',
foo: []
}), 'abc=abc');
});
test('should not encode undefined values', t => {
t.is(fn.stringify({
abc: undefined,
foo: 'baz'
}), 'foo=baz');
});
test('should encode null values as just a key', t => {
t.is(fn.stringify({
'x y z': null,
'abc': null,
'foo': 'baz'
}), 'abc&foo=baz&x%20y%20z');
});
test('handle null values in array', t => {
t.is(fn.stringify({
foo: null,
bar: [null, 'baz']
}), 'bar&bar=baz&foo');
});
test('handle undefined values in array', t => {
t.is(fn.stringify({
foo: null,
bar: [undefined, 'baz']
}), 'bar=baz&foo');
});
test('handle undefined and null values in array', t => {
t.is(fn.stringify({
foo: null,
bar: [undefined, null, 'baz']
}), 'bar&bar=baz&foo');
});
test('strict encoding', t => {
t.is(fn.stringify({foo: '\'bar\''}), 'foo=%27bar%27');
t.is(fn.stringify({foo: ['\'bar\'', '!baz']}), 'foo=%27bar%27&foo=%21baz');
});
test('loose encoding', t => {
t.is(fn.stringify({foo: '\'bar\''}, {strict: false}), 'foo=\'bar\'');
t.is(fn.stringify({foo: ['\'bar\'', '!baz']}, {strict: false}), 'foo=\'bar\'&foo=!baz');
});
test('array stringify representation with array indexes', t => {
t.is(fn.stringify({
foo: null,
bar: ['one', 'two']
}, {
arrayFormat: 'index'
}), 'bar[0]=one&bar[1]=two&foo');
});
test('array stringify representation with array brackets', t => {
t.is(fn.stringify({
foo: null,
bar: ['one', 'two']
}, {
arrayFormat: 'bracket'
}), 'bar[]=one&bar[]=two&foo');
});
test('array stringify representation with a bad array format', t => {
t.is(fn.stringify({
foo: null,
bar: ['one', 'two']
}, {
arrayFormat: 'badinput'
}), 'bar=one&bar=two&foo');
});
test('array stringify representation with array indexes and sparse array', t => {
const a = ['one', 'two'];
a[10] = 'three';
t.is(fn.stringify({bar: a}, {arrayFormat: 'index'}), 'bar[0]=one&bar[1]=two&bar[2]=three');
});