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

Can't handle sparse array correctly #4

Open
CrescentSine opened this issue Apr 16, 2021 · 1 comment
Open

Can't handle sparse array correctly #4

CrescentSine opened this issue Apr 16, 2021 · 1 comment

Comments

@CrescentSine
Copy link

case:
format([,3.14,6,,98])
result:
"return {\n\n 3.14,\n 6,\n\n 98,\n}"
should be:
"return {\n nil,\n 3.14,\n 6,\n nil,\n 98,\n}"

@m-imperfect
Copy link

m-imperfect commented May 12, 2023

JSON syntax doesn't really support sparse array.
I think this package is designed to format JSON arrays not JS,
but anyway, it's easy to replace the empty slots in a sparse array, for example:

let sparseArray = [,3.14,6,,98]
console.log([...sparseArray]) // [undefined, 3.14, 6, undefined, 98]

also, it's possible to do:

let sparseArray = [,3.14,6,,98]
let array = ([...sparseArray]).map(value => value === undefined ? null : value)
console.log(array) // [null, 3.14, 6, null, 98]

in order to avoid undefined values.

I think this should be left for the user to replace those empty slots, as JSON won't have them in anyway.

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

No branches or pull requests

2 participants