Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

refactor: remove `repeat()` polyfill (#5837)

Pelle Wessmanf988a8054044
diff --git a/lib/utils.js b/lib/utils.js
index 9cfbb6fd..c2129fec 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -217,8 +217,7 @@ exports.stringify = function (value) {
).replace(/,(\n|$)/g, "$1");
}
- // IE7/IE8 has a bizarre String constructor; needs to be coerced
- // into an array and back to obj.
+ // Boxed String objects (new String("foo")): expand into character-indexed object.
if (typeHint === "string" && typeof value === "object") {
value = value.split("").reduce(function (acc, char, idx) {
acc[idx] = char;
@@ -265,11 +264,6 @@ function jsonStringify(object, spaces, depth) {
typeof object.length === "number"
? object.length
: Object.keys(object).length;
- // `.repeat()` polyfill
- function repeat(s, n) {
- return new Array(n).join(s);
- }
-
function _stringify(val) {
switch (canonicalType(val)) {
case "null":
@@ -318,7 +312,7 @@ function jsonStringify(object, spaces, depth) {
--length;
str +=
"\n " +
- repeat(" ", space) +
+ " ".repeat(space - 1) +
(Array.isArray(object) ? "" : '"' + i + '": ') + // key
_stringify(object[i]) + // value
(length ? "," : ""); // comma
@@ -327,7 +321,7 @@ function jsonStringify(object, spaces, depth) {
return (
str +
// [], {}
- (str.length !== 1 ? "\n" + repeat(" ", space - 1) + end : end)
+ (str.length !== 1 ? "\n" + " ".repeat(Math.max(0, space - 2)) + end : end)
);
}