Skip to content

Commit

Permalink
fix(util): handle sparse arrays and options.maxArrayLength for util.i…
Browse files Browse the repository at this point in the history
…nspect
  • Loading branch information
sgtcoolguy committed Mar 7, 2019
1 parent ee4a93f commit 2f7c44c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
45 changes: 41 additions & 4 deletions common/Resources/ti.internal/extensions/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,46 @@ const defaultInspectOptions = {
getters: false
};

function formatArray(array, options) {
const maxLength = Math.max(0, options.maxArrayLength);
const arrayLength = array.length;
const values = [];
let consecutiveEmpties = 0;
let i = 0;
// for sparse arrays, consecutive empties count as a "single item" in terms of maxArrayLength
for (; i < arrayLength; i++) { // don't go past end of array...
const value = array[i];
if (value === undefined) { // sparse array!
consecutiveEmpties++;
continue;
}

// non-empty index currently...
if (consecutiveEmpties > 0) { // were we collecting consecutive empty indices as a single gap?
values.push(`<${consecutiveEmpties} empty item${consecutiveEmpties > 1 ? 's' : ''}>`);
consecutiveEmpties = 0; // reset our count
if (values.length >= maxLength) { // don't show more than options.maxArrayLength "values"
break;
}
}
// push the current index value
values.push(util.inspect(value, options));
if (values.length >= maxLength) { // don't show more than options.maxArrayLength "values"
i++; // so our "remaining" count is correct
break;
}
}

const remaining = arrayLength - i;
if (remaining > 0) { // did we stop before the end of the array (due to options.maxArrayLength)?
values.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
} else if (consecutiveEmpties > 0) { // did the sparse array gaps run to the end of the array?
values.push(`<${consecutiveEmpties} empty item${consecutiveEmpties > 1 ? 's' : ''}>`);
}

return values;
}

/**
* @param {*} obj JS value or object to inspect
* @param {object} [options] options for output
Expand Down Expand Up @@ -156,10 +196,7 @@ util.inspect = (obj, options = {}) => {
prefix = ''; // wipe "normal" Array prefixes
}
[ open, close ] = [ '[', ']' ]; // use array braces
if (obj.length > 0) {
// TODO: handle sparse arrays
values.push(...obj.map(o => util.inspect(o, mergedOptions)));
}
values.push(...formatArray(obj, mergedOptions));
} else if (util.types.isMap(obj)) {
if (obj.size > 0) {
values.push(...Array.from(obj).map(entry => `${util.inspect(entry[0], mergedOptions)} => ${util.inspect(entry[1], mergedOptions)}`));
Expand Down
33 changes: 31 additions & 2 deletions tests/Resources/util.addontest.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,12 +450,41 @@ describe.only('util', () => {
util.inspect([ 'a', 2 ]).should.eql('[ \'a\', 2 ]');
});

// TODO: Handle sparse arrays
it.skip('handles sparse array', () => {
it('handles sparse array', () => {
// eslint-disable-next-line no-sparse-arrays
util.inspect([ 1, , 3 ]).should.eql('[ 1, <1 empty item>, 3 ]');
});

it('handles sparse array with multiple items missing in a row', () => {
// eslint-disable-next-line no-sparse-arrays
util.inspect([ 1,,,, 3 ]).should.eql('[ 1, <3 empty items>, 3 ]');
});

it('handles sparse array with multiple separate gaps', () => {
// eslint-disable-next-line no-sparse-arrays
util.inspect([ 1,,,, 3, ,, 4 ]).should.eql('[ 1, <3 empty items>, 3, <2 empty items>, 4 ]');
});

it('handles array with length > options.maxArrayLength', () => {
util.inspect([ 1, 2, 3 ], { maxArrayLength: 1 }).should.eql('[ 1, ... 2 more items ]');
});

it('handles array with length > options.maxArrayLength and is sparse', () => {
// eslint-disable-next-line no-sparse-arrays
util.inspect([ 1,,,, 3, ,, 4 ], { maxArrayLength: 1 }).should.eql('[ 1, ... 7 more items ]');
});

it('handles sparse array with length > options.maxArrayLength counting gaps as one item for length', () => {
// eslint-disable-next-line no-sparse-arrays
util.inspect([ 1,,,, ], { maxArrayLength: 2 }).should.eql('[ 1, <3 empty items> ]');
// eslint-disable-next-line no-sparse-arrays
util.inspect([ 1,,,, 3, ,, 4 ], { maxArrayLength: 2 }).should.eql('[ 1, <3 empty items>, ... 4 more items ]');
// eslint-disable-next-line no-sparse-arrays
util.inspect([ 1,,,, 3, ,, 4 ], { maxArrayLength: 3 }).should.eql('[ 1, <3 empty items>, 3, ... 3 more items ]');
// eslint-disable-next-line no-sparse-arrays
util.inspect([ 1,,,, 3, ,, 4 ], { maxArrayLength: 4 }).should.eql('[ 1, <3 empty items>, 3, <2 empty items>, ... 1 more item ]');
});

it('handles Regexp literal', () => {
util.inspect(/123/).should.eql('/123/');
});
Expand Down

0 comments on commit 2f7c44c

Please sign in to comment.