Skip to content

Commit

Permalink
Merge pull request web-platform-tests#19 from jgraham/format_value_re…
Browse files Browse the repository at this point in the history
…cursion

Prevent infinite recursion formatting cyclicly nested arrays
  • Loading branch information
jgraham committed Mar 27, 2013
2 parents 8164906 + 70324d2 commit d9d62c8
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions testharness.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,11 +493,22 @@ policies and contribution forms [3].
/*
* Convert a value to a nice, human-readable string
*/
function format_value(val)
function format_value(val, seen)
{
if (!seen) {
seen = [];
}
if (typeof val === "object" && val !== null)
{
if (seen.indexOf(val) >= 0)
{
return "[...]";
}
seen.push(val);
}
if (Array.isArray(val))
{
return "[" + val.map(format_value).join(", ") + "]";
return "[" + val.map(function(x) {return format_value(x, seen)}).join(", ") + "]";
}

switch (typeof val)
Expand Down

0 comments on commit d9d62c8

Please sign in to comment.