Skip to content
This repository has been archived by the owner on Jan 6, 2021. It is now read-only.

Commit

Permalink
Format symbols (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonE authored and mantoni committed Mar 1, 2019
1 parent 2999f53 commit ff37cfd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions .eslintrc.yaml
Expand Up @@ -6,6 +6,7 @@ env:

globals:
Set: false
Symbol: false

plugins:
- ie11
Expand Down
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -8,3 +8,4 @@ Dominykas Blyžė <hello@dominykas.com>
Edward Betts <edward@4angle.com>
Dave Geddes <davidcgeddes@gmail.com>
Stein Magnus Jodal <stein.magnus@jodal.no>
Brandon Evans <contact@brandonmevans.com>
13 changes: 11 additions & 2 deletions lib/formatio.js
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) ?
Expand All @@ -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);
}
Expand Down
8 changes: 7 additions & 1 deletion lib/formatio.test.js
Expand Up @@ -198,6 +198,7 @@ describe("formatio.ascii", function () {
"oh hi": 42,
seriously: "many properties"
};
object[Symbol("key")] = Symbol("value");

var expectedFunctionString =
namesAnonymousFunctions
Expand All @@ -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);
});
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit ff37cfd

Please sign in to comment.