Initial commit for OpenECOMP SDN-C OA&M
[sdnc/oam.git] / dgbuilder / dgeflows / node_modules / body-parser / node_modules / qs / lib / stringify.js
1 // Load modules
2
3 var Utils = require('./utils');
4
5
6 // Declare internals
7
8 var internals = {
9     delimiter: '&',
10     indices: true
11 };
12
13
14 internals.stringify = function (obj, prefix, options) {
15
16     if (Utils.isBuffer(obj)) {
17         obj = obj.toString();
18     }
19     else if (obj instanceof Date) {
20         obj = obj.toISOString();
21     }
22     else if (obj === null) {
23         obj = '';
24     }
25
26     if (typeof obj === 'string' ||
27         typeof obj === 'number' ||
28         typeof obj === 'boolean') {
29
30         return [encodeURIComponent(prefix) + '=' + encodeURIComponent(obj)];
31     }
32
33     var values = [];
34
35     if (typeof obj === 'undefined') {
36         return values;
37     }
38
39     var objKeys = Object.keys(obj);
40     for (var i = 0, il = objKeys.length; i < il; ++i) {
41         var key = objKeys[i];
42         if (!options.indices &&
43             Array.isArray(obj)) {
44
45             values = values.concat(internals.stringify(obj[key], prefix, options));
46         }
47         else {
48             values = values.concat(internals.stringify(obj[key], prefix + '[' + key + ']', options));
49         }
50     }
51
52     return values;
53 };
54
55
56 module.exports = function (obj, options) {
57
58     options = options || {};
59     var delimiter = typeof options.delimiter === 'undefined' ? internals.delimiter : options.delimiter;
60     options.indices = typeof options.indices === 'boolean' ? options.indices : internals.indices;
61
62     var keys = [];
63
64     if (typeof obj !== 'object' ||
65         obj === null) {
66
67         return '';
68     }
69
70     var objKeys = Object.keys(obj);
71     for (var i = 0, il = objKeys.length; i < il; ++i) {
72         var key = objKeys[i];
73         keys = keys.concat(internals.stringify(obj[key], key, options));
74     }
75
76     return keys.join(delimiter);
77 };