From 74396dc634cc63199d46c3641fa65b94ee11c6c9 Mon Sep 17 00:00:00 2001 From: Brandon Evans Date: Wed, 30 Jan 2019 23:35:17 -0600 Subject: [PATCH 1/5] Format symbolic values and keys. --- lib/formatio.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/formatio.js b/lib/formatio.js index ea22546..2b97958 100644 --- a/lib/formatio.js +++ b/lib/formatio.js @@ -63,6 +63,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); } @@ -134,7 +138,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) ? @@ -144,6 +150,10 @@ ascii.object = function (object, processed, indent) { prop = properties[i]; obj = object[prop]; + if (typeof object === "symbol") { + return object.toString(); + } + if (isCircular(obj, processed)) { str = "[Circular]"; } else { From 1226a91dfcda728d4dc7c50da834be26231edea8 Mon Sep 17 00:00:00 2001 From: Brandon Evans Date: Sun, 3 Feb 2019 15:32:46 -0600 Subject: [PATCH 2/5] Fixed symbolic key formatting. --- lib/formatio.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/formatio.js b/lib/formatio.js index 2b97958..a3417c0 100644 --- a/lib/formatio.js +++ b/lib/formatio.js @@ -150,17 +150,16 @@ ascii.object = function (object, processed, indent) { prop = properties[i]; obj = object[prop]; - if (typeof object === "symbol") { - return object.toString(); - } - if (isCircular(obj, processed)) { str = "[Circular]"; } else { 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); } From 136a81cb55d46c520f2f9ba284387ccb295f44ad Mon Sep 17 00:00:00 2001 From: Brandon Evans Date: Thu, 28 Feb 2019 23:03:10 -0600 Subject: [PATCH 3/5] -Updated unit test. -Fixed linting errors. -Added myself to the authors list. --- .eslintrc.yaml | 1 + AUTHORS | 1 + lib/formatio.js | 2 +- lib/formatio.test.js | 4 +++- 4 files changed, 6 insertions(+), 2 deletions(-) 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 fb16be6..09abd29 100644 --- a/lib/formatio.js +++ b/lib/formatio.js @@ -153,7 +153,7 @@ ascii.object = function (object, processed, indent) { } str = ( - typeof prop === 'string' && /\s/.test(prop) ? + typeof prop === "string" && /\s/.test(prop) ? "\"" + prop + "\"" : prop.toString() ) + ": " + str; length += str.length; diff --git a/lib/formatio.test.js b/lib/formatio.test.js index 29aa887..215f120 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("symbolic")] = "symbolic property"; 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(symbolic): \"symbolic property\"\n}"; assert.equals(formatio.ascii(object), expected); }); From 62ade8e05eb7f4f6ab96094600700d83b9981bf7 Mon Sep 17 00:00:00 2001 From: Brandon Evans Date: Fri, 1 Mar 2019 01:17:42 -0600 Subject: [PATCH 4/5] Test symbolic value. --- lib/formatio.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/formatio.test.js b/lib/formatio.test.js index 215f120..f1d5a74 100644 --- a/lib/formatio.test.js +++ b/lib/formatio.test.js @@ -198,7 +198,7 @@ describe("formatio.ascii", function () { "oh hi": 42, seriously: "many properties" }; - object[Symbol("symbolic")] = "symbolic property"; + object[Symbol("key")] = Symbol("value"); var expectedFunctionString = namesAnonymousFunctions @@ -209,7 +209,7 @@ describe("formatio.ascii", function () { "more: \"properties\",\n \"oh hi\": 42,\n please: " + "\"Gimme some more\",\n prop: \"Some\"," + "\n seriously: \"many properties\"," + - "\n Symbol(symbolic): \"symbolic property\"\n}"; + "\n Symbol(key): Symbol(value)\n}"; assert.equals(formatio.ascii(object), expected); }); From 56ee5a028bb7b141ef91ca5c8d92c310cedbd136 Mon Sep 17 00:00:00 2001 From: Brandon Evans Date: Fri, 1 Mar 2019 01:28:07 -0600 Subject: [PATCH 5/5] Add separate test for formatting a symbol. --- lib/formatio.test.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/formatio.test.js b/lib/formatio.test.js index f1d5a74..84f4733 100644 --- a/lib/formatio.test.js +++ b/lib/formatio.test.js @@ -379,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();