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();
13 exports.arrayToObject = function (source, options) {
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') {
27 exports.merge = function (target, source, options) {
33 if (typeof source !== 'object') {
34 if (Array.isArray(target)) {
37 else if (typeof target === 'object') {
38 target[source] = true;
41 target = [target, source];
47 if (typeof target !== 'object') {
48 target = [target].concat(source);
52 if (Array.isArray(target) &&
53 !Array.isArray(source)) {
55 target = exports.arrayToObject(target, options);
58 var keys = Object.keys(source);
59 for (var k = 0, kl = keys.length; k < kl; ++k) {
61 var value = source[key];
63 if (!Object.prototype.hasOwnProperty.call(target, key)) {
67 target[key] = exports.merge(target[key], value, options);
75 exports.decode = function (str) {
78 return decodeURIComponent(str.replace(/\+/g, ' '));
84 exports.encode = function (str) {
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) {
92 if (typeof str !== 'string') {
97 for (var i = 0, il = str.length; i < il; ++i) {
98 var c = str.charCodeAt(i);
100 if (c === 0x2D || // -
104 (c >= 0x30 && c <= 0x39) || // 0-9
105 (c >= 0x41 && c <= 0x5A) || // a-z
106 (c >= 0x61 && c <= 0x7A)) { // A-Z
113 out += internals.hexTable[c];
118 out += internals.hexTable[0xC0 | (c >> 6)] + internals.hexTable[0x80 | (c & 0x3F)];
122 if (c < 0xD800 || c >= 0xE000) {
123 out += internals.hexTable[0xE0 | (c >> 12)] + internals.hexTable[0x80 | ((c >> 6) & 0x3F)] + internals.hexTable[0x80 | (c & 0x3F)];
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)];
135 exports.compact = function (obj, refs) {
137 if (typeof obj !== 'object' ||
144 var lookup = refs.indexOf(obj);
151 if (Array.isArray(obj)) {
154 for (var i = 0, il = obj.length; i < il; ++i) {
155 if (typeof obj[i] !== 'undefined') {
156 compacted.push(obj[i]);
163 var keys = Object.keys(obj);
164 for (i = 0, il = keys.length; i < il; ++i) {
166 obj[key] = exports.compact(obj[key], refs);
173 exports.isRegExp = function (obj) {
175 return Object.prototype.toString.call(obj) === '[object RegExp]';
179 exports.isBuffer = function (obj) {
182 typeof obj === 'undefined') {
187 return !!(obj.constructor &&
188 obj.constructor.isBuffer &&
189 obj.constructor.isBuffer(obj));