diff --git a/lib/utils.js b/lib/utils.js index 3792cbafb2..abe773eec8 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -107,7 +107,7 @@ var indexOf = exports.indexOf = function(arr, obj, start) { * @param {Object} val Initial value. * @return {*} */ -exports.reduce = function(arr, fn, val) { +var reduce = exports.reduce = function(arr, fn, val) { var rval = val; for (var i = 0, l = arr.length; i < l; i++) { @@ -313,7 +313,7 @@ exports.trim = function(str) { * @return {Object} */ exports.parseQuery = function(qs) { - return exports.reduce(qs.replace('?', '').split('&'), function(obj, pair) { + return reduce(qs.replace('?', '').split('&'), function(obj, pair) { var i = pair.indexOf('='); var key = pair.slice(0, i); var val = pair.slice(++i); @@ -449,9 +449,19 @@ exports.stringify = function(value) { .replace(/,(\n|$)/g, '$1'); } - for (var prop in value) { - if (Object.prototype.hasOwnProperty.call(value, prop)) { - return jsonStringify(exports.canonicalize(value, null, typeHint), 2).replace(/,(\n|$)/g, '$1'); + // IE7/IE8 has a bizarre String constructor; needs to be coerced + // into an array and back again. + var obj = value; + if (typeof value === 'string' && typeHint === 'object') { + obj = Array.prototype.slice.apply(value); + } + for (var prop in obj) { + if (Object.prototype.hasOwnProperty.call(obj, prop)) { + var result = jsonStringify(exports.canonicalize(obj, null, typeHint), 2).replace(/,(\n|$)/g, '$1'); + return obj === value ? result : reduce(result, function(acc, char, idx) { + acc[idx] = char; + return acc; + }, {}); } } @@ -750,7 +760,7 @@ exports.stackTraceFilter = function() { return function(stack) { stack = stack.split('\n'); - stack = exports.reduce(stack, function(list, line) { + stack = reduce(stack, function(list, line) { if (isMochaInternal(line)) { return list; }