123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- 'use strict';
- var has = Object.prototype.hasOwnProperty;
- var hexTable = (function () {
- var array = [];
- for (var i = 0; i < 256; ++i) {
- array.push('%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase());
- }
- return array;
- }());
- exports.arrayToObject = function (source, options) {
- var obj = options && options.plainObjects ? Object.create(null) : {};
- for (var i = 0; i < source.length; ++i) {
- if (typeof source[i] !== 'undefined') {
- obj[i] = source[i];
- }
- }
- return obj;
- };
- exports.merge = function (target, source, options) {
- if (!source) {
- return target;
- }
- if (typeof source !== 'object') {
- if (Array.isArray(target)) {
- target.push(source);
- } else if (typeof target === 'object') {
- if (options.plainObjects || options.allowPrototypes || !has.call(Object.prototype, source)) {
- target[source] = true;
- }
- } else {
- return [target, source];
- }
- return target;
- }
- if (typeof target !== 'object') {
- return [target].concat(source);
- }
- var mergeTarget = target;
- if (Array.isArray(target) && !Array.isArray(source)) {
- mergeTarget = exports.arrayToObject(target, options);
- }
- if (Array.isArray(target) && Array.isArray(source)) {
- source.forEach(function (item, i) {
- if (has.call(target, i)) {
- if (target[i] && typeof target[i] === 'object') {
- target[i] = exports.merge(target[i], item, options);
- } else {
- target.push(item);
- }
- } else {
- target[i] = item;
- }
- });
- return target;
- }
- return Object.keys(source).reduce(function (acc, key) {
- var value = source[key];
- if (Object.prototype.hasOwnProperty.call(acc, key)) {
- acc[key] = exports.merge(acc[key], value, options);
- } else {
- acc[key] = value;
- }
- return acc;
- }, mergeTarget);
- };
- exports.decode = function (str) {
- try {
- return decodeURIComponent(str.replace(/\+/g, ' '));
- } catch (e) {
- return str;
- }
- };
- exports.encode = function (str) {
- // This code was originally written by Brian White (mscdex) for the io.js core querystring library.
- // It has been adapted here for stricter adherence to RFC 3986
- if (str.length === 0) {
- return str;
- }
- var string = typeof str === 'string' ? str : String(str);
- var out = '';
- for (var i = 0; i < string.length; ++i) {
- var c = string.charCodeAt(i);
- if (
- c === 0x2D || // -
- c === 0x2E || // .
- c === 0x5F || // _
- c === 0x7E || // ~
- (c >= 0x30 && c <= 0x39) || // 0-9
- (c >= 0x41 && c <= 0x5A) || // a-z
- (c >= 0x61 && c <= 0x7A) // A-Z
- ) {
- out += string.charAt(i);
- continue;
- }
- if (c < 0x80) {
- out = out + hexTable[c];
- continue;
- }
- if (c < 0x800) {
- out = out + (hexTable[0xC0 | (c >> 6)] + hexTable[0x80 | (c & 0x3F)]);
- continue;
- }
- if (c < 0xD800 || c >= 0xE000) {
- out = out + (hexTable[0xE0 | (c >> 12)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)]);
- continue;
- }
- i += 1;
- c = 0x10000 + (((c & 0x3FF) << 10) | (string.charCodeAt(i) & 0x3FF));
- out += hexTable[0xF0 | (c >> 18)] + hexTable[0x80 | ((c >> 12) & 0x3F)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)]; // eslint-disable-line max-len
- }
- return out;
- };
- exports.compact = function (obj, references) {
- if (typeof obj !== 'object' || obj === null) {
- return obj;
- }
- var refs = references || [];
- var lookup = refs.indexOf(obj);
- if (lookup !== -1) {
- return refs[lookup];
- }
- refs.push(obj);
- if (Array.isArray(obj)) {
- var compacted = [];
- for (var i = 0; i < obj.length; ++i) {
- if (obj[i] && typeof obj[i] === 'object') {
- compacted.push(exports.compact(obj[i], refs));
- } else if (typeof obj[i] !== 'undefined') {
- compacted.push(obj[i]);
- }
- }
- return compacted;
- }
- var keys = Object.keys(obj);
- keys.forEach(function (key) {
- obj[key] = exports.compact(obj[key], refs);
- });
- return obj;
- };
- exports.isRegExp = function (obj) {
- return Object.prototype.toString.call(obj) === '[object RegExp]';
- };
- exports.isBuffer = function (obj) {
- if (obj === null || typeof obj === 'undefined') {
- return false;
- }
- return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));
- };
|