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

utils.js: significant modification of diff behavior #1357

Merged
merged 1 commit into from
Dec 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 159 additions & 33 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,18 +292,100 @@ exports.highlightTags = function(name) {
}
};

/**
* If a value could have properties, and has none, this function is called, which returns
* a string representation of the empty value.
*
* Functions w/ no properties return `'[Function]'`
* Arrays w/ length === 0 return `'[]'`
* Objects w/ no properties return `'{}'`
* All else: return result of `value.toString()`
*
* @param {*} value Value to inspect
* @param {string} [type] The type of the value, if known.
* @returns {string}
*/
var emptyRepresentation = function emptyRepresentation(value, type) {
type = type || exports.type(value);

switch(type) {
case 'function':
return '[Function]';
case 'object':
return '{}';
case 'array':
return '[]';
default:
return value.toString();
}
};

/**
* Takes some variable and asks `{}.toString()` what it thinks it is.
* @param {*} value Anything
* @example
* type({}) // 'object'
* type([]) // 'array'
* type(1) // 'number'
* type(false) // 'boolean'
* type(Infinity) // 'number'
* type(null) // 'null'
* type(new Date()) // 'date'
* type(/foo/) // 'regexp'
* type('type') // 'string'
* type(global) // 'global'
* @api private
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString
* @returns {string}
*/
exports.type = function type(value) {
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) {
return 'buffer';
}
return Object.prototype.toString.call(value)
.replace(/^\[.+\s(.+?)\]$/, '$1')
.toLowerCase();
};

/**
* Stringify `obj`.
* @summary Stringify `value`.
* @description Different behavior depending on type of value.
* - If `value` is undefined or null, return `'[undefined]'` or `'[null]'`, respectively.
* - If `value` is not an object, function or array, return result of `value.toString()` wrapped in double-quotes.
* - If `value` is an *empty* object, function, or array, return result of function
* {@link emptyRepresentation}.
* - If `value` has properties, call {@link exports.canonicalize} on it, then return result of
* JSON.stringify().
*
* @param {Object} obj
* @return {String}
* @see exports.type
* @param {*} value
* @return {string}
* @api private
*/

exports.stringify = function(obj) {
if (obj instanceof RegExp) return obj.toString();
return JSON.stringify(exports.canonicalize(obj), null, 2).replace(/,(\n|$)/g, '$1');
exports.stringify = function(value) {
var prop,
type = exports.type(value);

if (type === 'null' || type === 'undefined') {
return '[' + type + ']';
}

if (type === 'date') {
return '[Date: ' + value.toISOString() + ']';
}

if (!~exports.indexOf(['object', 'array', 'function'], type)) {
return value.toString();
}

for (prop in value) {
if (value.hasOwnProperty(prop)) {
return JSON.stringify(exports.canonicalize(value), null, 2).replace(/,(\n|$)/g, '$1');
}
}

return emptyRepresentation(value, type);
};

/**
Expand All @@ -313,45 +395,89 @@ exports.stringify = function(obj) {
* @api private
*/
exports.isBuffer = function (arg) {
return typeof Buffer !== 'undefined' && arg instanceof Buffer;
return typeof Buffer !== 'undefined' && Buffer.isBuffer(arg);
};

/**
* Return a new object that has the keys in sorted order.
* @param {Object} obj
* @param {Array} [stack]
* @return {Object}
* @summary Return a new Thing that has the keys in sorted order. Recursive.
* @description If the Thing...
* - has already been seen, return string `'[Circular]'`
* - is `undefined`, return string `'[undefined]'`
* - is `null`, return value `null`
* - is some other primitive, return the value
* - is not a primitive or an `Array`, `Object`, or `Function`, return the value of the Thing's `toString()` method
* - is a non-empty `Array`, `Object`, or `Function`, return the result of calling this function again.
* - is an empty `Array`, `Object`, or `Function`, return the result of calling `emptyRepresentation()`
*
* @param {*} value Thing to inspect. May or may not have properties.
* @param {Array} [stack=[]] Stack of seen values
* @return {(Object|Array|Function|string|undefined)}
* @see {@link exports.stringify}
* @api private
*/

exports.canonicalize = function(obj, stack) {
stack = stack || [];
exports.canonicalize = function(value, stack) {
var canonicalizedObj,
type = exports.type(value),
prop,
withStack = function withStack(value, fn) {
stack.push(value);
fn();
stack.pop();
};

if (exports.indexOf(stack, obj) !== -1) return '[Circular]';
stack = stack || [];

var canonicalizedObj;
if (exports.indexOf(stack, value) !== -1) {
return '[Circular]';
}

if(exports.isBuffer(obj)) {
return obj;
} else if ({}.toString.call(obj) === '[object Array]') {
stack.push(obj);
canonicalizedObj = exports.map(obj, function (item) {
return exports.canonicalize(item, stack);
});
stack.pop();
} else if (typeof obj === 'object' && obj !== null) {
stack.push(obj);
canonicalizedObj = {};
exports.forEach(exports.keys(obj).sort(), function (key) {
canonicalizedObj[key] = exports.canonicalize(obj[key], stack);
});
stack.pop();
} else {
canonicalizedObj = obj;
switch(type) {
case 'undefined':
canonicalizedObj = '[undefined]';
break;
case 'buffer':
case 'null':
canonicalizedObj = value;
break;
case 'array':
withStack(value, function () {
canonicalizedObj = exports.map(value, function (item) {
return exports.canonicalize(item, stack);
});
});
break;
case 'date':
canonicalizedObj = '[Date: ' + value.toISOString() + ']';
break;
case 'function':
for (prop in value) {
canonicalizedObj = {};
break;
}
if (!canonicalizedObj) {
canonicalizedObj = emptyRepresentation(value, type);
break;
}
/* falls through */
case 'object':
canonicalizedObj = canonicalizedObj || {};
withStack(value, function () {
exports.forEach(exports.keys(value).sort(), function (key) {
canonicalizedObj[key] = exports.canonicalize(value[key], stack);
});
});
break;
case 'number':
case 'boolean':
canonicalizedObj = value;
break;
default:
canonicalizedObj = value.toString();
}

return canonicalizedObj;
};
};

/**
* Lookup file names at the given `path`.
Expand Down
Loading