Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change exports usage #230

Merged
merged 1 commit into from Oct 14, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 22 additions & 11 deletions lib/utils.js
Expand Up @@ -34,7 +34,7 @@ var compactQueue = function compactQueue(queue) {
return obj;
};

exports.arrayToObject = function arrayToObject(source, options) {
var arrayToObject = function arrayToObject(source, options) {
var obj = options && options.plainObjects ? Object.create(null) : {};
for (var i = 0; i < source.length; ++i) {
if (typeof source[i] !== 'undefined') {
Expand All @@ -45,7 +45,7 @@ exports.arrayToObject = function arrayToObject(source, options) {
return obj;
};

exports.merge = function merge(target, source, options) {
var merge = function merge(target, source, options) {
if (!source) {
return target;
}
Expand All @@ -70,14 +70,14 @@ exports.merge = function merge(target, source, options) {

var mergeTarget = target;
if (Array.isArray(target) && !Array.isArray(source)) {
mergeTarget = exports.arrayToObject(target, options);
mergeTarget = 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);
target[i] = merge(target[i], item, options);
} else {
target.push(item);
}
Expand All @@ -92,30 +92,30 @@ exports.merge = function merge(target, source, options) {
var value = source[key];

if (has.call(acc, key)) {
acc[key] = exports.merge(acc[key], value, options);
acc[key] = merge(acc[key], value, options);
} else {
acc[key] = value;
}
return acc;
}, mergeTarget);
};

exports.assign = function assignSingleSource(target, source) {
var assign = function assignSingleSource(target, source) {
return Object.keys(source).reduce(function (acc, key) {
acc[key] = source[key];
return acc;
}, target);
};

exports.decode = function (str) {
var decode = function (str) {
try {
return decodeURIComponent(str.replace(/\+/g, ' '));
} catch (e) {
return str;
}
};

exports.encode = function encode(str) {
var encode = function encode(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) {
Expand Down Expand Up @@ -167,7 +167,7 @@ exports.encode = function encode(str) {
return out;
};

exports.compact = function compact(value) {
var compact = function compact(value) {
var queue = [{ obj: { o: value }, prop: 'o' }];
var refs = [];

Expand All @@ -189,14 +189,25 @@ exports.compact = function compact(value) {
return compactQueue(queue);
};

exports.isRegExp = function isRegExp(obj) {
var isRegExp = function isRegExp(obj) {
return Object.prototype.toString.call(obj) === '[object RegExp]';
};

exports.isBuffer = function isBuffer(obj) {
var isBuffer = function isBuffer(obj) {
if (obj === null || typeof obj === 'undefined') {
return false;
}

return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));
};

module.exports = {
arrayToObject: arrayToObject,
assign: assign,
compact: compact,
decode: decode,
encode: encode,
isBuffer: isBuffer,
isRegExp: isRegExp,
merge: merge
};