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 var has = Object.prototype.hasOwnProperty;
14 exports.arrayToObject = function (source, options) {
15 var obj = options.plainObjects ? Object.create(null) : {};
16 for (var i = 0; i < source.length; ++i) {
17 if (typeof source[i] !== 'undefined') {
25 exports.merge = function (target, source, options) {
30 if (typeof source !== 'object') {
31 if (Array.isArray(target)) {
33 } else if (typeof target === 'object') {
34 if (options.plainObjects || options.allowPrototypes || !has.call(Object.prototype, source)) {
35 target[source] = true;
38 return [target, source];
44 if (typeof target !== 'object') {
45 return [target].concat(source);
48 var mergeTarget = target;
49 if (Array.isArray(target) && !Array.isArray(source)) {
50 mergeTarget = exports.arrayToObject(target, options);
53 return Object.keys(source).reduce(function (acc, key) {
54 var value = source[key];
56 if (has.call(acc, key)) {
57 acc[key] = exports.merge(acc[key], value, options);
65 exports.decode = function (str) {
67 return decodeURIComponent(str.replace(/\+/g, ' '));
73 exports.encode = function (str) {
74 // This code was originally written by Brian White (mscdex) for the io.js core querystring library.
75 // It has been adapted here for stricter adherence to RFC 3986
76 if (str.length === 0) {
80 var string = typeof str === 'string' ? str : String(str);
83 for (var i = 0; i < string.length; ++i) {
84 var c = string.charCodeAt(i);
91 (c >= 0x30 && c <= 0x39) || // 0-9
92 (c >= 0x41 && c <= 0x5A) || // a-z
93 (c >= 0x61 && c <= 0x7A) // A-Z
95 out += string.charAt(i);
100 out = out + hexTable[c];
105 out = out + (hexTable[0xC0 | (c >> 6)] + hexTable[0x80 | (c & 0x3F)]);
109 if (c < 0xD800 || c >= 0xE000) {
110 out = out + (hexTable[0xE0 | (c >> 12)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)]);
115 c = 0x10000 + (((c & 0x3FF) << 10) | (string.charCodeAt(i) & 0x3FF));
116 out += (hexTable[0xF0 | (c >> 18)] + hexTable[0x80 | ((c >> 12) & 0x3F)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)]);
122 exports.compact = function (obj, references) {
123 if (typeof obj !== 'object' || obj === null) {
127 var refs = references || [];
128 var lookup = refs.indexOf(obj);
135 if (Array.isArray(obj)) {
138 for (var i = 0; i < obj.length; ++i) {
139 if (typeof obj[i] !== 'undefined') {
140 compacted.push(obj[i]);
147 var keys = Object.keys(obj);
148 for (var j = 0; j < keys.length; ++j) {
150 obj[key] = exports.compact(obj[key], refs);
156 exports.isRegExp = function (obj) {
157 return Object.prototype.toString.call(obj) === '[object RegExp]';
160 exports.isBuffer = function (obj) {
161 if (obj === null || typeof obj === 'undefined') {
165 return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));