-
-
Notifications
You must be signed in to change notification settings - Fork 717
/
stringify.js
executable file
·157 lines (120 loc) · 3.84 KB
/
stringify.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'use strict';
// Load modules
const Utils = require('./utils');
// Declare internals
const internals = {
delimiter: '&',
arrayPrefixGenerators: {
brackets: function (prefix, key) {
return prefix + '[]';
},
indices: function (prefix, key) {
return prefix + '[' + key + ']';
},
repeat: function (prefix, key) {
return prefix;
}
},
strictNullHandling: false,
skipNulls: false,
encode: true
};
internals.stringify = function (obj, prefix, generateArrayPrefix, strictNullHandling, skipNulls, encode, filter, sort) {
if (typeof filter === 'function') {
obj = filter(prefix, obj);
}
else if (Utils.isBuffer(obj)) {
obj = obj.toString();
}
else if (obj instanceof Date) {
obj = obj.toISOString();
}
else if (obj === null) {
if (strictNullHandling) {
return encode ? Utils.encode(prefix) : prefix;
}
obj = '';
}
if (typeof obj === 'string' ||
typeof obj === 'number' ||
typeof obj === 'boolean') {
if (encode) {
return [Utils.encode(prefix) + '=' + Utils.encode(obj)];
}
return [prefix + '=' + obj];
}
let values = [];
if (typeof obj === 'undefined') {
return values;
}
let objKeys;
if (Array.isArray(filter)) {
objKeys = filter;
}
else {
const keys = Object.keys(obj);
objKeys = sort ? keys.sort(sort) : keys;
}
for (let i = 0; i < objKeys.length; ++i) {
const key = objKeys[i];
if (skipNulls &&
obj[key] === null) {
continue;
}
if (Array.isArray(obj)) {
values = values.concat(internals.stringify(obj[key], generateArrayPrefix(prefix, key), generateArrayPrefix, strictNullHandling, skipNulls, encode, filter));
}
else {
values = values.concat(internals.stringify(obj[key], prefix + '[' + key + ']', generateArrayPrefix, strictNullHandling, skipNulls, encode, filter));
}
}
return values;
};
module.exports = function (obj, options) {
options = options || {};
const delimiter = typeof options.delimiter === 'undefined' ? internals.delimiter : options.delimiter;
const strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : internals.strictNullHandling;
const skipNulls = typeof options.skipNulls === 'boolean' ? options.skipNulls : internals.skipNulls;
const encode = typeof options.encode === 'boolean' ? options.encode : internals.encode;
const sort = typeof options.sort === 'function' ? options.sort : null;
let objKeys;
let filter;
if (typeof options.filter === 'function') {
filter = options.filter;
obj = filter('', obj);
}
else if (Array.isArray(options.filter)) {
objKeys = filter = options.filter;
}
let keys = [];
if (typeof obj !== 'object' ||
obj === null) {
return '';
}
let arrayFormat;
if (options.arrayFormat in internals.arrayPrefixGenerators) {
arrayFormat = options.arrayFormat;
}
else if ('indices' in options) {
arrayFormat = options.indices ? 'indices' : 'repeat';
}
else {
arrayFormat = 'indices';
}
const generateArrayPrefix = internals.arrayPrefixGenerators[arrayFormat];
if (!objKeys) {
objKeys = Object.keys(obj);
}
if (sort) {
objKeys.sort(sort);
}
for (let i = 0; i < objKeys.length; ++i) {
const key = objKeys[i];
if (skipNulls &&
obj[key] === null) {
continue;
}
keys = keys.concat(internals.stringify(obj[key], key, generateArrayPrefix, strictNullHandling, skipNulls, encode, filter, sort));
}
return keys.join(delimiter);
};