diff --git a/.eslintrc.yaml b/.eslintrc.yaml index bf36fc9..8e3e593 100644 --- a/.eslintrc.yaml +++ b/.eslintrc.yaml @@ -6,6 +6,7 @@ env: globals: Set: false + Symbol: false plugins: - ie11 diff --git a/AUTHORS b/AUTHORS index 77b324b..c011256 100644 --- a/AUTHORS +++ b/AUTHORS @@ -8,3 +8,4 @@ Dominykas Blyžė Edward Betts Dave Geddes Stein Magnus Jodal +Brandon Evans diff --git a/lib/formatio.js b/lib/formatio.js index 0371899..09abd29 100644 --- a/lib/formatio.js +++ b/lib/formatio.js @@ -58,6 +58,10 @@ function ascii(f, object, processed, indent) { return processed || quote ? "\"" + object + "\"" : object; } + if (typeof object === "symbol") { + return object.toString(); + } + if (typeof object === "function" && !(object instanceof RegExp)) { return ascii.func(object); } @@ -130,7 +134,9 @@ ascii.object = function (object, processed, indent) { processed.push(object); indent = indent || 0; var pieces = []; - var properties = Object.keys(object).sort(); + var properties = Object.keys(object).sort().concat( + Object.getOwnPropertySymbols(object) + ); var length = 3; var prop, str, obj, i, k, l; l = (this.limitChildrenCount > 0) ? @@ -146,7 +152,10 @@ ascii.object = function (object, processed, indent) { str = ascii(this, obj, processed, indent + 2); } - str = (/\s/.test(prop) ? "\"" + prop + "\"" : prop) + ": " + str; + str = ( + typeof prop === "string" && /\s/.test(prop) ? + "\"" + prop + "\"" : prop.toString() + ) + ": " + str; length += str.length; pieces.push(str); } diff --git a/lib/formatio.test.js b/lib/formatio.test.js index 29aa887..84f4733 100644 --- a/lib/formatio.test.js +++ b/lib/formatio.test.js @@ -198,6 +198,7 @@ describe("formatio.ascii", function () { "oh hi": 42, seriously: "many properties" }; + object[Symbol("key")] = Symbol("value"); var expectedFunctionString = namesAnonymousFunctions @@ -207,7 +208,8 @@ describe("formatio.ascii", function () { var expected = "{\n " + expectedFunctionString + ",\n id: 42,\n " + "more: \"properties\",\n \"oh hi\": 42,\n please: " + "\"Gimme some more\",\n prop: \"Some\"," + - "\n seriously: \"many properties\"\n}"; + "\n seriously: \"many properties\"," + + "\n Symbol(key): Symbol(value)\n}"; assert.equals(formatio.ascii(object), expected); }); @@ -377,6 +379,10 @@ describe("formatio.ascii", function () { }); }); + it("formats symbol", function () { + assert.equals(formatio.ascii(Symbol("value")), "Symbol(value)"); + }); + describe("sets", function () { it("formats sets", function () { var set = new Set();