123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- // Load modules
- // Declare internals
- var internals = {};
- internals.hexTable = new Array(256);
- for (var h = 0; h < 256; ++h) {
- internals.hexTable[h] = '%' + ((h < 16 ? '0' : '') + h.toString(16)).toUpperCase();
- }
- exports.arrayToObject = function (source, options) {
- var obj = options.plainObjects ? Object.create(null) : {};
- for (var i = 0, il = source.length; i < il; ++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') {
- target[source] = true;
- }
- else {
- target = [target, source];
- }
- return target;
- }
- if (typeof target !== 'object') {
- target = [target].concat(source);
- return target;
- }
- if (Array.isArray(target) &&
- !Array.isArray(source)) {
- target = exports.arrayToObject(target, options);
- }
- var keys = Object.keys(source);
- for (var k = 0, kl = keys.length; k < kl; ++k) {
- var key = keys[k];
- var value = source[key];
- if (!Object.prototype.hasOwnProperty.call(target, key)) {
- target[key] = value;
- }
- else {
- target[key] = exports.merge(target[key], value, options);
- }
- }
- return target;
- };
- 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;
- }
- if (typeof str !== 'string') {
- str = '' + str;
- }
- var out = '';
- for (var i = 0, il = str.length; i < il; ++i) {
- var c = str.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 += str[i];
- continue;
- }
- if (c < 0x80) {
- out += internals.hexTable[c];
- continue;
- }
- if (c < 0x800) {
- out += internals.hexTable[0xC0 | (c >> 6)] + internals.hexTable[0x80 | (c & 0x3F)];
- continue;
- }
- if (c < 0xD800 || c >= 0xE000) {
- out += internals.hexTable[0xE0 | (c >> 12)] + internals.hexTable[0x80 | ((c >> 6) & 0x3F)] + internals.hexTable[0x80 | (c & 0x3F)];
- continue;
- }
- ++i;
- c = 0x10000 + (((c & 0x3FF) << 10) | (str.charCodeAt(i) & 0x3FF));
- out += internals.hexTable[0xF0 | (c >> 18)] + internals.hexTable[0x80 | ((c >> 12) & 0x3F)] + internals.hexTable[0x80 | ((c >> 6) & 0x3F)] + internals.hexTable[0x80 | (c & 0x3F)];
- }
- return out;
- };
- exports.compact = function (obj, refs) {
- if (typeof obj !== 'object' ||
- obj === null) {
- return obj;
- }
- refs = refs || [];
- var lookup = refs.indexOf(obj);
- if (lookup !== -1) {
- return refs[lookup];
- }
- refs.push(obj);
- if (Array.isArray(obj)) {
- var compacted = [];
- for (var i = 0, il = obj.length; i < il; ++i) {
- if (typeof obj[i] !== 'undefined') {
- compacted.push(obj[i]);
- }
- }
- return compacted;
- }
- var keys = Object.keys(obj);
- for (i = 0, il = keys.length; i < il; ++i) {
- var key = keys[i];
- 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));
- };
|