Skip to content

Attempt at partially supporting symbols#34

Merged
SBoudrias merged 3 commits into
sindresorhus:masterfrom
mightyiam:symbols
Nov 23, 2016
Merged

Attempt at partially supporting symbols#34
SBoudrias merged 3 commits into
sindresorhus:masterfrom
mightyiam:symbols

Conversation

@mightyiam

Copy link
Copy Markdown
Contributor

No description provided.

Comment thread fixture.js
newlines: "foo\nbar\r\nbaz",
circular: "[Circular]"
circular: "[Circular]",
Symbol(): Symbol(),

@mightyiam mightyiam Nov 20, 2016

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally

[Symbol()]: Symbol()

.

Comment thread fixture.js
circular: "[Circular]"
circular: "[Circular]",
Symbol(): Symbol(),
Symbol(foo): Symbol(foo),

@mightyiam mightyiam Nov 20, 2016

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally

[Symbol('foo')]: Symbol('foo')

with quotes according to options.

Comment thread fixture.js
circular: "[Circular]",
Symbol(): Symbol(),
Symbol(foo): Symbol(foo),
Symbol(foo): Symbol(foo)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally

[Symbol.for('foo')]: Symbol.for('foo')

with quotes according to options.

Comment thread index.js
typeof val === 'number' ||
typeof val === 'boolean' ||
typeof val === 'function' ||
typeof val === 'symbol' ||

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle it!

Comment thread index.js Outdated

if (isObj(val)) {
const objKeys = Object.keys(val);
const objKeys = Object.keys(val).concat(Object.getOwnPropertySymbols(val));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread index.js Outdated
const eol = objKeys.length - 1 === i ? tokens.newLine : ',' + tokens.newLineOrSpace;
const key = /^[a-z$_][a-z$_0-9]*$/i.test(el) ? el : stringify(el, opts);
return tokens.indent + key + ': ' + stringify(val[el], opts, pad + opts.indent) + eol;
const key = typeof el === 'symbol' || /^[a-z$_][a-z$_0-9]*$/i.test(el) ? el : stringify(el, opts);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it correct to bypass the regular expression test when el is a symbol?

Comment thread index.js
const key = /^[a-z$_][a-z$_0-9]*$/i.test(el) ? el : stringify(el, opts);
return tokens.indent + key + ': ' + stringify(val[el], opts, pad + opts.indent) + eol;
const key = typeof el === 'symbol' || /^[a-z$_][a-z$_0-9]*$/i.test(el) ? el : stringify(el, opts);
return tokens.indent + String(key) + ': ' + stringify(val[el], opts, pad + opts.indent) + eol;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the executions where key is a symbol. Otherwise 💥.

Comment thread test.js
newlines: "foo\nbar\r\nbaz",
[Symbol()]: Symbol(), // eslint-disable-line symbol-description
[Symbol('foo')]: Symbol('foo'),
[Symbol.for('foo')]: Symbol.for('foo')

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take a look please and compare with the fixture above.

@mightyiam mightyiam left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pareto improvement?

@SBoudrias SBoudrias left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks pretty good to me. Thanks for sending this out!

Comment thread index.js Outdated
const eol = objKeys.length - 1 === i ? tokens.newLine : ',' + tokens.newLineOrSpace;
const key = /^[a-z$_][a-z$_0-9]*$/i.test(el) ? el : stringify(el, opts);
return tokens.indent + key + ': ' + stringify(val[el], opts, pad + opts.indent) + eol;
const key = typeof el === 'symbol' || /^[a-z$_][a-z$_0-9]*$/i.test(el) ? el : stringify(el, opts);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make that a if statement. It's getting quite hairy all on a single line.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SBoudrias how do you like it now?
See last commit.

@mightyiam

Copy link
Copy Markdown
Contributor Author

@SBoudrias please re- 🤓

@SBoudrias

Copy link
Copy Markdown
Contributor

Just so I understand this properly, this is stripping any symbol in the object?

@mightyiam

Copy link
Copy Markdown
Contributor Author

@SBoudrias "stripping"? What do you mean by that?

On a different topic, as the PR branch is, non-enumerable symbol-key properties are included. I'm guessing we do not wish for that, correct?

@mightyiam

Copy link
Copy Markdown
Contributor Author

Now it excludes non-enumerable symbol-key properties.

Comment thread index.js
'use strict';
const isRegexp = require('is-regexp');
const isObj = require('is-obj');
const getOwnEnumPropSymbols = require('get-own-enumerable-property-symbols');

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread index.js

if (isObj(val)) {
const objKeys = Object.keys(val);
const objKeys = Object.keys(val).concat(getOwnEnumPropSymbols(val));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like simple.

Comment thread package.json
"json"
],
"dependencies": {
"get-own-enumerable-property-symbols": "^1.0.1",

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mightyiam mightyiam left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@SBoudrias

Copy link
Copy Markdown
Contributor

Cool thanks, the latest diff makes more sense to me now.

1 similar comment
@SBoudrias

Copy link
Copy Markdown
Contributor

Cool thanks, the latest diff makes more sense to me now.

@SBoudrias
SBoudrias merged commit b3225b4 into sindresorhus:master Nov 23, 2016
@mightyiam
mightyiam deleted the symbols branch November 23, 2016 22:02
@mightyiam

Copy link
Copy Markdown
Contributor Author

🎆

I think this is semver-major.

@SBoudrias

Copy link
Copy Markdown
Contributor

I'm going to bump a release tonight.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants