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
168 lines (145 loc) · 3.63 KB
/
stringify.js
File metadata and controls
168 lines (145 loc) · 3.63 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
import test from 'ava';
import m from '..';
test('stringify', t => {
t.is(m.stringify({foo: 'bar'}), 'foo=bar');
t.is(m.stringify({
foo: 'bar',
bar: 'baz'
}), 'bar=baz&foo=bar');
});
test('different types', t => {
t.is(m.stringify(), '');
t.is(m.stringify(0), '');
});
test('URI encode', t => {
t.is(m.stringify({'foo bar': 'baz faz'}), 'foo%20bar=baz%20faz');
t.is(m.stringify({'foo bar': 'baz\'faz'}), 'foo%20bar=baz%27faz');
});
test('no encoding', t => {
t.is(m.stringify({'foo:bar': 'baz:faz'}, {encode: false}), 'foo:bar=baz:faz');
});
test('handle array value', t => {
t.is(m.stringify({
abc: 'abc',
foo: ['bar', 'baz']
}), 'abc=abc&foo=bar&foo=baz');
});
test('array order', t => {
t.is(m.stringify({
abc: 'abc',
foo: ['baz', 'bar']
}), 'abc=abc&foo=baz&foo=bar');
});
test('handle empty array value', t => {
t.is(m.stringify({
abc: 'abc',
foo: []
}), 'abc=abc');
});
test('should not encode undefined values', t => {
t.is(m.stringify({
abc: undefined,
foo: 'baz'
}), 'foo=baz');
});
test('should encode null values as just a key', t => {
t.is(m.stringify({
'x y z': null,
abc: null,
foo: 'baz'
}), 'abc&foo=baz&x%20y%20z');
});
test('handle null values in array', t => {
t.is(m.stringify({
foo: null,
bar: [null, 'baz']
}), 'bar&bar=baz&foo');
});
test('handle undefined values in array', t => {
t.is(m.stringify({
foo: null,
bar: [undefined, 'baz']
}), 'bar=baz&foo');
});
test('handle undefined and null values in array', t => {
t.is(m.stringify({
foo: null,
bar: [undefined, null, 'baz']
}), 'bar&bar=baz&foo');
});
test('strict encoding', t => {
t.is(m.stringify({foo: '\'bar\''}), 'foo=%27bar%27');
t.is(m.stringify({foo: ['\'bar\'', '!baz']}), 'foo=%27bar%27&foo=%21baz');
});
test('loose encoding', t => {
t.is(m.stringify({foo: '\'bar\''}, {strict: false}), 'foo=\'bar\'');
t.is(m.stringify({foo: ['\'bar\'', '!baz']}, {strict: false}), 'foo=\'bar\'&foo=!baz');
});
test('array stringify representation with array indexes', t => {
t.is(m.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(m.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(m.stringify({
foo: ['a', null, ''],
bar: [null]
}, {
arrayFormat: 'bracket'
}), 'bar[]&foo[]=a&foo[]&foo[]=');
});
test('array stringify representation with a bad array format', t => {
t.is(m.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(m.stringify({bar: a}, {arrayFormat: 'index'}), 'bar[0]=one&bar[1]=two&bar[2]=three');
});
test('should sort keys in given order', t => {
const order = ['c', 'a', 'b'];
const sort = (key1, key2) => order.indexOf(key1) - order.indexOf(key2);
t.is(m.stringify({a: 'foo', b: 'bar', c: 'baz'}, {sort}), 'c=baz&a=foo&b=bar');
});
test('should not sort when sort is false', t => {
const object = {
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(m.stringify(object, {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(m.stringify({
c: 'foo',
b: 'bar',
a: 'baz'
}, {
sort: false
}), 'c=foo&b=bar&a=baz');
});