3 var has = Object.prototype.hasOwnProperty;
5 var hexTable = (function () {
7 for (var i = 0; i < 256; ++i) {
8 array.push('%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase());
14 exports.arrayToObject = function (source, options) {
15 var obj = options && 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 if (Array.isArray(target) && Array.isArray(source)) {
54 source.forEach(function (item, i) {
55 if (has.call(target, i)) {
56 if (target[i] && typeof target[i] === 'object') {
57 target[i] = exports.merge(target[i], item, options);
68 return Object.keys(source).reduce(function (acc, key) {
69 var value = source[key];
71 if (Object.prototype.hasOwnProperty.call(acc, key)) {
72 acc[key] = exports.merge(acc[key], value, options);
80 exports.decode = function (str) {
82 return decodeURIComponent(str.replace(/\+/g, ' '));
88 exports.encode = function (str) {
89 // This code was originally written by Brian White (mscdex) for the io.js core querystring library.
90 // It has been adapted here for stricter adherence to RFC 3986
91 if (str.length === 0) {
95 var string = typeof str === 'string' ? str : String(str);
98 for (var i = 0; i < string.length; ++i) {
99 var c = string.charCodeAt(i);
106 (c >= 0x30 && c <= 0x39) || // 0-9
107 (c >= 0x41 && c <= 0x5A) || // a-z
108 (c >= 0x61 && c <= 0x7A) // A-Z
110 out += string.charAt(i);
115 out = out + hexTable[c];
120 out = out + (hexTable[0xC0 | (c >> 6)] + hexTable[0x80 | (c & 0x3F)]);
124 if (c < 0xD800 || c >= 0xE000) {
125 out = out + (hexTable[0xE0 | (c >> 12)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)]);
130 c = 0x10000 + (((c & 0x3FF) << 10) | (string.charCodeAt(i) & 0x3FF));
131 out += hexTable[0xF0 | (c >> 18)] + hexTable[0x80 | ((c >> 12) & 0x3F)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)]; // eslint-disable-line max-len
137 exports.compact = function (obj, references) {
138 if (typeof obj !== 'object' || obj === null) {
142 var refs = references || [];
143 var lookup = refs.indexOf(obj);
150 if (Array.isArray(obj)) {
153 for (var i = 0; i < obj.length; ++i) {
154 if (obj[i] && typeof obj[i] === 'object') {
155 compacted.push(exports.compact(obj[i], refs));
156 } else if (typeof obj[i] !== 'undefined') {
157 compacted.push(obj[i]);
164 var keys = Object.keys(obj);
165 keys.forEach(function (key) {
166 obj[key] = exports.compact(obj[key], refs);
172 exports.isRegExp = function (obj) {
173 return Object.prototype.toString.call(obj) === '[object RegExp]';
176 exports.isBuffer = function (obj) {
177 if (obj === null || typeof obj === 'undefined') {
181 return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));