Skip to content

Commit

Permalink
Merge pull request #576 from lucasfcosta/truncate-typed-arrays
Browse files Browse the repository at this point in the history
[WIP] Truncate Typed Arrays (#441)
  • Loading branch information
keithamus committed Jan 5, 2016
2 parents 9271294 + 07b0a8e commit ec8d99f
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 2 deletions.
41 changes: 39 additions & 2 deletions lib/chai/utils/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
var getName = require('./getName');
var getProperties = require('./getProperties');
var getEnumerableProperties = require('./getEnumerableProperties');
var config = require('../config');

module.exports = inspect;

/**
* Echos the value of a value. Trys to print the value out
* Echos the value of a value. Tries to print the value out
* in the best way possible given the different types.
*
* @param {Object} obj The object to print out.
Expand Down Expand Up @@ -120,7 +121,15 @@ function formatValue(ctx, value, recurseTimes) {
}
}

var base = '', array = false, braces = ['{', '}'];
var base = ''
, array = false
, typedArray = false
, braces = ['{', '}'];

if (isTypedArray(value)) {
typedArray = true;
braces = ['[', ']'];
}

// Make Array say that they are Array
if (isArray(value)) {
Expand Down Expand Up @@ -167,6 +176,8 @@ function formatValue(ctx, value, recurseTimes) {
var output;
if (array) {
output = formatArray(ctx, value, recurseTimes, visibleKeys, keys);
} else if (typedArray) {
return formatTypedArray(value);
} else {
output = keys.map(function(key) {
return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array);
Expand Down Expand Up @@ -221,6 +232,7 @@ function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
output.push('');
}
}

keys.forEach(function(key) {
if (!key.match(/^\d+$/)) {
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
Expand All @@ -230,6 +242,25 @@ function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
return output;
}

function formatTypedArray(value) {
var str = '[ ';

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

// Removing trailing `, ` if the array was not truncated
if (str.indexOf(', ]') !== -1) {
str = str.replace(', ]', ' ]');
}

return str;
}

function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
var name;
Expand Down Expand Up @@ -314,6 +345,12 @@ function reduceToSingleString(output, base, braces) {
return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
}

function isTypedArray(ar) {
// Unfortunately there's no way to check if an object is a TypedArray
// We have to check if it's one of these types
return (typeof ar === 'object' && /\w+Array]$/.test(objectToString(ar)));
}

function isArray(ar) {
return Array.isArray(ar) ||
(typeof ar === 'object' && objectToString(ar) === '[object Array]');
Expand Down
59 changes: 59 additions & 0 deletions test/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,65 @@ describe('utilities', function () {
});
});

it('inspect every kind of available TypedArray', function () {
chai.use(function (_chai, _) {
var arr = [1, 2, 3]
, exp = '[ 1, 2, 3 ]'
, isNode = true;

if (typeof window !== 'undefined') {
isNode = false;
}

// Checks if engine supports common TypedArrays
if ((!isNode && 'Int8Array' in window) ||
isNode && typeof 'Int8Array' !== undefined) {
// Typed array inspections should work as array inspections do
expect(_.inspect(new Int8Array(arr))).to.equal(exp);
expect(_.inspect(new Uint8Array(arr))).to.equal(exp);
expect(_.inspect(new Int16Array(arr))).to.equal(exp);
expect(_.inspect(new Uint16Array(arr))).to.equal(exp);
expect(_.inspect(new Int32Array(arr))).to.equal(exp);
expect(_.inspect(new Uint32Array(arr))).to.equal(exp);
expect(_.inspect(new Float32Array(arr))).to.equal(exp);
}

// These ones may not be available alongside the others above
if ((!isNode && 'Uint8ClampedArray' in window) ||
isNode && typeof 'Uint8ClampedArray' !== undefined) {
expect(_.inspect(new Uint8ClampedArray(arr))).to.equal(exp);
}

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

it('truncate long TypedArray', function () {
chai.use(function (_chai, _) {

var arr = []
, exp = '[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ... ]'
, isNode = true;

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

if (typeof window !== 'undefined') {
isNode = false;
}

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

it('addChainableMethod', function () {
chai.use(function (_chai, _) {
_chai.Assertion.addChainableMethod('x',
Expand Down

0 comments on commit ec8d99f

Please sign in to comment.