Skip to content

Commit

Permalink
Separate inspect logic and display logic when formatting typedArrays
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfcosta committed Jan 5, 2016
1 parent 7f6b49c commit fb4585b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 30 deletions.
31 changes: 12 additions & 19 deletions lib/chai/utils/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ function formatValue(ctx, value, recurseTimes) {
if (array) {
output = formatArray(ctx, value, recurseTimes, visibleKeys, keys);
} else if (typedArray) {
output = formatTypedArray(ctx, value, recurseTimes, visibleKeys, keys);
return formatTypedArray(value);
} else {
output = keys.map(function(key) {
return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array);
Expand Down Expand Up @@ -242,28 +242,21 @@ function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
return output;
}

function formatTypedArray(ctx, value, recurseTimes, visibleKeys, keys) {
var output = [];

// Truncating TypedArray if it has more elements than the configured threshold
var t = config.truncateThreshold;
var l = value.length > t ? t : value.length;

for (var i = 0; i < l; ++i) {
if (Object.prototype.hasOwnProperty.call(value, String(i))) {
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
String(i), true));
} else {
output.push('');
function formatTypedArray(value) {
str = '[ ';

for (var i = 0; i < value.length; i++) {
if (str.length >= config.truncateThreshold - 7) {
return Object.create(Object.getPrototypeOf(value),
{length: {value: value.length}});
}
str += value[i] + ', ';
}

// If TypedArray has been truncated we add '...' to its end
if (l !== value.length) {
output.push('...');
}
// Removing trailing `, `
str = str.slice(0, -2) + ' ]';

return output;
return str;
}

function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
Expand Down
13 changes: 7 additions & 6 deletions lib/chai/utils/objDisplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,20 @@ var config = require('../config');
*/

module.exports = function (obj) {
var str = inspect(obj)
, type = Object.prototype.toString.call(obj);
var str = inspect(obj);
var type = Object.prototype.toString.call(obj);
var arrayMatch = / (.*Array)]$/.exec(type);

if (config.truncateThreshold && str.length >= config.truncateThreshold) {
if (type === '[object Function]') {
return !obj.name || obj.name === ''
? '[Function]'
: '[Function: ' + obj.name + ']';
} else if (type === '[object Array]') {
return '[ Array(' + obj.length + ') ]';
} else if (arrayMatch !== null) {
return '[ ' + arrayMatch[1] + '(' + obj.length + ') ]';
} else if (type === '[object Object]') {
var keys = Object.keys(obj)
, kstr = keys.length > 2
var keys = Object.keys(obj);
var kstr = keys.length > 2
? keys.splice(0, 2).join(', ') + ', ...'
: keys.join(', ');
return '{ Object (' + kstr + ') }';
Expand Down
10 changes: 5 additions & 5 deletions test/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,12 +432,12 @@ describe('utilities', function () {

_chai.config.truncateThreshold = 5;

var arr = []
, exp = '[ 1, 2, 3, 4, 5, ... ]'
, isNode = true;
var arr = [];
var exp = '[ Int8Array(10000) ]';
var isNode = true;

// Filling arr with lots of elements
for (var i = 1; i <= 1000; i++) {
for (var i = 1; i <= 10000; i++) {
arr.push(i);
}

Expand All @@ -447,7 +447,7 @@ describe('utilities', function () {

if ((!isNode && 'Int8Array' in window) ||
isNode && typeof 'Int8Array' !== undefined) {
expect(_.inspect(new Int8Array(arr))).to.equal(exp);
expect(_.objDisplay(new Int8Array(arr))).to.equal(exp);
}
});
});
Expand Down

0 comments on commit fb4585b

Please sign in to comment.