3 var hexTable = (function () {
4 var array = new Array(256);
5 for (var i = 0; i < 256; ++i) {
6 array[i] = '%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase();
12 exports.arrayToObject = function (source, options) {
13 var obj = options.plainObjects ? Object.create(null) : {};
14 for (var i = 0; i < source.length; ++i) {
15 if (typeof source[i] !== 'undefined') {
23 exports.merge = function (target, source, options) {
28 if (typeof source !== 'object') {
29 if (Array.isArray(target)) {
31 } else if (typeof target === 'object') {
32 target[source] = true;
34 return [target, source];
40 if (typeof target !== 'object') {
41 return [target].concat(source);
44 var mergeTarget = target;
45 if (Array.isArray(target) && !Array.isArray(source)) {
46 mergeTarget = exports.arrayToObject(target, options);
49 return Object.keys(source).reduce(function (acc, key) {
50 var value = source[key];
52 if (Object.prototype.hasOwnProperty.call(acc, key)) {
53 acc[key] = exports.merge(acc[key], value, options);
61 exports.decode = function (str) {
63 return decodeURIComponent(str.replace(/\+/g, ' '));
69 exports.encode = function (str) {
70 // This code was originally written by Brian White (mscdex) for the io.js core querystring library.
71 // It has been adapted here for stricter adherence to RFC 3986
72 if (str.length === 0) {
76 var string = typeof str === 'string' ? str : String(str);
79 for (var i = 0; i < string.length; ++i) {
80 var c = string.charCodeAt(i);
87 (c >= 0x30 && c <= 0x39) || // 0-9
88 (c >= 0x41 && c <= 0x5A) || // a-z
89 (c >= 0x61 && c <= 0x7A) // A-Z
91 out += string.charAt(i);
96 out = out + hexTable[c];
101 out = out + (hexTable[0xC0 | (c >> 6)] + hexTable[0x80 | (c & 0x3F)]);
105 if (c < 0xD800 || c >= 0xE000) {
106 out = out + (hexTable[0xE0 | (c >> 12)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)]);
111 c = 0x10000 + (((c & 0x3FF) << 10) | (string.charCodeAt(i) & 0x3FF));
112 out += hexTable[0xF0 | (c >> 18)] + hexTable[0x80 | ((c >> 12) & 0x3F)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)];
118 exports.compact = function (obj, references) {
119 if (typeof obj !== 'object' || obj === null) {
123 var refs = references || [];
124 var lookup = refs.indexOf(obj);
131 if (Array.isArray(obj)) {
134 for (var i = 0; i < obj.length; ++i) {
135 if (obj[i] && typeof obj[i] === 'object') {
136 compacted.push(exports.compact(obj[i], refs));
137 } else if (typeof obj[i] !== 'undefined') {
138 compacted.push(obj[i]);
145 var keys = Object.keys(obj);
146 for (var j = 0; j < keys.length; ++j) {
148 obj[key] = exports.compact(obj[key], refs);
154 exports.isRegExp = function (obj) {
155 return Object.prototype.toString.call(obj) === '[object RegExp]';
158 exports.isBuffer = function (obj) {
159 if (obj === null || typeof obj === 'undefined') {
163 return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));