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
195 lines (169 loc) · 4.49 KB
/
Copy pathstringify.js
File metadata and controls
195 lines (169 loc) · 4.49 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import test from 'ava';
import queryString from '..';
test('stringify', t => {
t.is(queryString.stringify({foo: 'bar'}), 'foo=bar');
t.is(queryString.stringify({
foo: 'bar',
bar: 'baz'
}), 'bar=baz&foo=bar');
});
test('different types', t => {
t.is(queryString.stringify(), '');
t.is(queryString.stringify(0), '');
});
test('URI encode', t => {
t.is(queryString.stringify({'foo bar': 'baz faz'}), 'foo%20bar=baz%20faz');
t.is(queryString.stringify({'foo bar': 'baz\'faz'}), 'foo%20bar=baz%27faz');
});
test('no encoding', t => {
t.is(queryString.stringify({'foo:bar': 'baz:faz'}, {encode: false}), 'foo:bar=baz:faz');
});
test('handle array value', t => {
t.is(queryString.stringify({
abc: 'abc',
foo: ['bar', 'baz']
}), 'abc=abc&foo=bar&foo=baz');
});
test('array order', t => {
t.is(queryString.stringify({
abc: 'abc',
foo: ['baz', 'bar']
}), 'abc=abc&foo=baz&foo=bar');
});
test('handle empty array value', t => {
t.is(queryString.stringify({
abc: 'abc',
foo: []
}), 'abc=abc');
});
test('should not encode undefined values', t => {
t.is(queryString.stringify({
abc: undefined,
foo: 'baz'
}), 'foo=baz');
});
test('should encode null values as just a key', t => {
t.is(queryString.stringify({
'x y z': null,
abc: null,
foo: 'baz'
}), 'abc&foo=baz&x%20y%20z');
});
test('handle null values in array', t => {
t.is(queryString.stringify({
foo: null,
bar: [null, 'baz']
}), 'bar&bar=baz&foo');
});
test('handle undefined values in array', t => {
t.is(queryString.stringify({
foo: null,
bar: [undefined, 'baz']
}), 'bar=baz&foo');
});
test('handle undefined and null values in array', t => {
t.is(queryString.stringify({
foo: null,
bar: [undefined, null, 'baz']
}), 'bar&bar=baz&foo');
});
test('strict encoding', t => {
t.is(queryString.stringify({foo: '\'bar\''}), 'foo=%27bar%27');
t.is(queryString.stringify({foo: ['\'bar\'', '!baz']}), 'foo=%27bar%27&foo=%21baz');
});
test('loose encoding', t => {
t.is(queryString.stringify({foo: '\'bar\''}, {strict: false}), 'foo=\'bar\'');
t.is(queryString.stringify({foo: ['\'bar\'', '!baz']}, {strict: false}), 'foo=\'bar\'&foo=!baz');
});
test('array stringify representation with array indexes', t => {
t.is(queryString.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(queryString.stringify({
foo: null,
bar: ['one', 'two']
}, {
arrayFormat: 'bracket'
}), 'bar[]=one&bar[]=two&foo');
});
test('array stringify representation with array brackets and null value', t => {
t.is(queryString.stringify({
foo: ['a', null, ''],
bar: [null]
}, {
arrayFormat: 'bracket'
}), 'bar[]&foo[]=a&foo[]&foo[]=');
});
test('array stringify representation with array commas', t => {
t.is(queryString.stringify({
foo: null,
bar: ['one', 'two']
}, {
arrayFormat: 'comma'
}), 'bar=one,two&foo');
});
test('array stringify representation with array commas and null value', t => {
t.is(queryString.stringify({
foo: ['a', null, ''],
bar: [null]
}, {
arrayFormat: 'comma'
}), 'foo=a');
});
test('array stringify representation with array commas and 0 value', t => {
t.is(queryString.stringify({
foo: ['a', null, 0],
bar: [null]
}, {
arrayFormat: 'comma'
}), 'foo=a,0');
});
test('array stringify representation with a bad array format', t => {
t.is(queryString.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 fixture = ['one', 'two'];
fixture[10] = 'three';
t.is(queryString.stringify({bar: fixture}, {arrayFormat: 'index'}), 'bar[0]=one&bar[1]=two&bar[2]=three');
});
test('should sort keys in given order', t => {
const fixture = ['c', 'a', 'b'];
const sort = (key1, key2) => fixture.indexOf(key1) - fixture.indexOf(key2);
t.is(queryString.stringify({a: 'foo', b: 'bar', c: 'baz'}, {sort}), 'c=baz&a=foo&b=bar');
});
test('should not sort when sort is false', t => {
const fixture = {
story: 'a',
patch: 'b',
deployment: 'c',
lat: 10,
lng: 20,
sb: 'd',
sc: 'e',
mn: 'f',
ln: 'g',
nf: 'h',
srs: 'i',
destination: 'g'
};
t.is(queryString.stringify(fixture, {sort: false}), 'story=a&patch=b&deployment=c&lat=10&lng=20&sb=d&sc=e&mn=f&ln=g&nf=h&srs=i&destination=g');
});
test('should disable sorting', t => {
t.is(queryString.stringify({
c: 'foo',
b: 'bar',
a: 'baz'
}, {
sort: false
}), 'c=foo&b=bar&a=baz');
});