Skip to content

Commit

Permalink
fix: Handle the formatting of array arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
dentuzhik committed Apr 24, 2024
1 parent 31db200 commit 6dde40f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
14 changes: 14 additions & 0 deletions src/utility.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var merge = require('./merge');

var RollbarJSON = {};

function setupJSON(polyfillJSON) {
if (isFunction(RollbarJSON.stringify) && isFunction(RollbarJSON.parse)) {
return;
Expand Down Expand Up @@ -450,6 +451,7 @@ function nonCircularClone(obj) {
}
return result;
}

return clone(obj, seen);
}

Expand Down Expand Up @@ -689,6 +691,18 @@ function formatArgsAsString(args) {
for (i = 0, len = args.length; i < len; ++i) {
arg = args[i];
switch (typeName(arg)) {
case 'array':
var trimLimit = 10;
var trimmedArgs = arg.slice(0, trimLimit);
var argsToFormat =
trimmedArgs.length === trimLimit
? trimmedArgs.concat(
'...output trimmed to ' + trimLimit + ' items...',
)
: trimmedArgs;

arg = '[' + formatArgsAsString(argsToFormat) + ']';
break;
case 'object':
arg = stringify(arg);
arg = arg.error || arg.value;
Expand Down
31 changes: 24 additions & 7 deletions test/utility.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,23 @@ describe('formatArgsAsString', function () {

expect(result).to.eql('1 {"a":42}');
});

it('should handle arrays gracefully', function () {
var args = [1, [{ a: 42 }], [{ b: 43 }, null, 'foo', [1, 2]]];
var result = _.formatArgsAsString(args);

expect(result).to.eql('1 [{"a":42}] [{"b":43} null foo [1 2]]');
});

it('should trim large arrays to the list of 10 items', function () {
var args = [1, [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]];
var result = _.formatArgsAsString(args);

expect(result).to.eql(
'1 [2 3 4 5 6 7 8 9 10 11 ...output trimmed to 10 items...]',
);
});

it('should handle strings', function () {
var args = [1, 'foo'];
var result = _.formatArgsAsString(args);
Expand All @@ -915,12 +932,12 @@ describe('formatArgsAsString', function () {
expect(result).to.eql('');
});
/*
* PhantomJS does not support Symbol yet
it('should handle symbols', function() {
var args = [1, Symbol('hello')];
var result = _.formatArgsAsString(args);
* PhantomJS does not support Symbol yet
it('should handle symbols', function() {
var args = [1, Symbol('hello')];
var result = _.formatArgsAsString(args);
expect(result).to.eql('1 symbol(\'hello\')');
});
*/
expect(result).to.eql('1 symbol(\'hello\')');
});
*/
});

0 comments on commit 6dde40f

Please sign in to comment.