forked from jazzychad/querystring.node.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquerystring-stringify.js
75 lines (64 loc) · 2.2 KB
/
querystring-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
/*
* querystring-stringify.js
* - node.js module providing 'stringify' method for converting objects to query strings.
*
* Chad Etzel
*
* Based on YUI "querystring-stringify.js" module
* http://github.com/isaacs/yui3/tree/master/src/querystring/js
*
* Copyright (c) 2009, Yahoo! Inc. and Chad Etzel
* BSD License (see LICENSE.md for info)
*
*/
var util = require("./util");
exports.stringify = querystring_stringify;
var stack = [];
/**
* <p>Converts an arbitrary value to a Query String representation.</p>
*
* <p>Objects with cyclical references will trigger an exception.</p>
*
* @method stringify
* @param obj {Variant} any arbitrary value to convert to query string
* @param sep {String} (optional) Character that should join param k=v pairs together. Default: "&"
* @param eq {String} (optional) Character that should join keys to their values. Default: "="
* @param name {String} (optional) Name of the current key, for handling children recursively.
* @param escape {Function} (optional) Function for escaping. Default: encodeURIComponent
*/
function querystring_stringify (obj, sep, eq, name, escape) {
sep = sep || "&";
eq = eq || "=";
escape = escape || encodeURIComponent;
if (util.isNull(obj) || util.isUndefined(obj) || typeof(obj) === 'function') {
return name ? escape(name) + eq : '';
}
if (util.isBoolean(obj)) obj = +obj;
if (util.isNumber(obj) || util.isString(obj)) {
return escape(name) + eq + escape(obj);
}
if (util.isArray(obj)) {
var s = [];
name = name+'[]';
for (var i = 0, l = obj.length; i < l; i ++) {
s.push( querystring_stringify(obj[i], sep, eq, name, escape) );
}
return s.join(sep);
}
// Check for cyclical references in nested objects
for (var i = stack.length - 1; i >= 0; --i) if (stack[i] === obj) {
throw new Error("querystring_stringify. Cyclical reference");
}
stack.push(obj);
var s = [];
var begin = name ? name + '[' : '';
var end = name ? ']' : '';
for (var i in obj) if (obj.hasOwnProperty(i)) {
var n = begin + i + end;
s.push(querystring_stringify(obj[i], sep, eq, n, escape));
}
stack.pop();
s = s.join(sep);
if (!s && name) return name + "=";
return s;
};