Initial commit
[yaffs-website] / node_modules / body-parser / node_modules / qs / lib / utils.js
1 // Load modules
2
3
4 // Declare internals
5
6 var internals = {};
7 internals.hexTable = new Array(256);
8 for (var h = 0; h < 256; ++h) {
9     internals.hexTable[h] = '%' + ((h < 16 ? '0' : '') + h.toString(16)).toUpperCase();
10 }
11
12
13 exports.arrayToObject = function (source, options) {
14
15     var obj = options.plainObjects ? Object.create(null) : {};
16     for (var i = 0, il = source.length; i < il; ++i) {
17         if (typeof source[i] !== 'undefined') {
18
19             obj[i] = source[i];
20         }
21     }
22
23     return obj;
24 };
25
26
27 exports.merge = function (target, source, options) {
28
29     if (!source) {
30         return target;
31     }
32
33     if (typeof source !== 'object') {
34         if (Array.isArray(target)) {
35             target.push(source);
36         }
37         else if (typeof target === 'object') {
38             target[source] = true;
39         }
40         else {
41             target = [target, source];
42         }
43
44         return target;
45     }
46
47     if (typeof target !== 'object') {
48         target = [target].concat(source);
49         return target;
50     }
51
52     if (Array.isArray(target) &&
53         !Array.isArray(source)) {
54
55         target = exports.arrayToObject(target, options);
56     }
57
58     var keys = Object.keys(source);
59     for (var k = 0, kl = keys.length; k < kl; ++k) {
60         var key = keys[k];
61         var value = source[key];
62
63         if (!Object.prototype.hasOwnProperty.call(target, key)) {
64             target[key] = value;
65         }
66         else {
67             target[key] = exports.merge(target[key], value, options);
68         }
69     }
70
71     return target;
72 };
73
74
75 exports.decode = function (str) {
76
77     try {
78         return decodeURIComponent(str.replace(/\+/g, ' '));
79     } catch (e) {
80         return str;
81     }
82 };
83
84 exports.encode = function (str) {
85
86     // This code was originally written by Brian White (mscdex) for the io.js core querystring library.
87     // It has been adapted here for stricter adherence to RFC 3986
88     if (str.length === 0) {
89         return str;
90     }
91
92     if (typeof str !== 'string') {
93         str = '' + str;
94     }
95
96     var out = '';
97     for (var i = 0, il = str.length; i < il; ++i) {
98         var c = str.charCodeAt(i);
99
100         if (c === 0x2D || // -
101             c === 0x2E || // .
102             c === 0x5F || // _
103             c === 0x7E || // ~
104             (c >= 0x30 && c <= 0x39) || // 0-9
105             (c >= 0x41 && c <= 0x5A) || // a-z
106             (c >= 0x61 && c <= 0x7A)) { // A-Z
107
108             out += str[i];
109             continue;
110         }
111
112         if (c < 0x80) {
113             out += internals.hexTable[c];
114             continue;
115         }
116
117         if (c < 0x800) {
118             out += internals.hexTable[0xC0 | (c >> 6)] + internals.hexTable[0x80 | (c & 0x3F)];
119             continue;
120         }
121
122         if (c < 0xD800 || c >= 0xE000) {
123             out += internals.hexTable[0xE0 | (c >> 12)] + internals.hexTable[0x80 | ((c >> 6) & 0x3F)] + internals.hexTable[0x80 | (c & 0x3F)];
124             continue;
125         }
126
127         ++i;
128         c = 0x10000 + (((c & 0x3FF) << 10) | (str.charCodeAt(i) & 0x3FF));
129         out += internals.hexTable[0xF0 | (c >> 18)] + internals.hexTable[0x80 | ((c >> 12) & 0x3F)] + internals.hexTable[0x80 | ((c >> 6) & 0x3F)] + internals.hexTable[0x80 | (c & 0x3F)];
130     }
131
132     return out;
133 };
134
135 exports.compact = function (obj, refs) {
136
137     if (typeof obj !== 'object' ||
138         obj === null) {
139
140         return obj;
141     }
142
143     refs = refs || [];
144     var lookup = refs.indexOf(obj);
145     if (lookup !== -1) {
146         return refs[lookup];
147     }
148
149     refs.push(obj);
150
151     if (Array.isArray(obj)) {
152         var compacted = [];
153
154         for (var i = 0, il = obj.length; i < il; ++i) {
155             if (typeof obj[i] !== 'undefined') {
156                 compacted.push(obj[i]);
157             }
158         }
159
160         return compacted;
161     }
162
163     var keys = Object.keys(obj);
164     for (i = 0, il = keys.length; i < il; ++i) {
165         var key = keys[i];
166         obj[key] = exports.compact(obj[key], refs);
167     }
168
169     return obj;
170 };
171
172
173 exports.isRegExp = function (obj) {
174
175     return Object.prototype.toString.call(obj) === '[object RegExp]';
176 };
177
178
179 exports.isBuffer = function (obj) {
180
181     if (obj === null ||
182         typeof obj === 'undefined') {
183
184         return false;
185     }
186
187     return !!(obj.constructor &&
188               obj.constructor.isBuffer &&
189               obj.constructor.isBuffer(obj));
190 };