Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support symbol literal #80

Open
GHolk opened this issue Aug 12, 2023 · 0 comments · May be fixed by #81
Open

Support symbol literal #80

GHolk opened this issue Aug 12, 2023 · 0 comments · May be fixed by #81

Comments

@GHolk
Copy link

GHolk commented Aug 12, 2023

We can support global registried symbol and well-known symbol.
this means stringifyObject(Symbol.for('sym')) can output Symbol.for('sym'),
and stringifyObject(Symbol.iterator) output Symbol.iterator.

I am not sure whether to support other symbols, because their stringified literal values will never equal,
but they may be same symbols before stringified.

Currently we can support symbol as object value or array item with a transform callback, but not the object key.
following is my transform code:

import stro from 'stringify-object'
const symbolGlobalList = Object.getOwnPropertyNames(Symbol)
    .map(k => Symbol[k]).filter(s => typeof s == 'symbol')
const symbolToLiteral = s => {
    if (~symbolGlobalList.indexOf(s)) return s.description
    const t = stro(s.description)
    if (Symbol.keyFor(s) == undefined) return `Symbol(${t})`
    else return `Symbol.for(${t})`
}
const strf = x => stro(x, {
    indent: '  ', inlineCharacterLimit: 40,
    transform(d, k, o) {
        const v = d[k]
        if (typeof v == 'symbol') o = symbolToLiteral(v)
        return o
    }
})
@GHolk GHolk linked a pull request Aug 13, 2023 that will close this issue
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 a pull request may close this issue.

1 participant