mathiasbynens / jsesc

Given some data, jsesc returns the shortest possible stringified & ASCII-safe representation of that data.
https://mths.be/jsesc
MIT License
716 stars 53 forks source link

Skip undefined properties for options.json = true; #52

Open ziir opened 5 years ago

ziir commented 5 years ago

Hello, many thanks for this awesome package.

Similarily to JSON.stringify, jsesc(obj, { json:true }) should skip undefined properties within obj.

Side-note: Following discussions on Twitter, I was experimenting moving away from serialize-javascript to jsesc to serialize Redux SSR's initial state for GANDI apps such as https://shop.gandi.net when I encountered jsesc's behavior regarding undefined object properties, which differs from serialize-javascript's behavior.

Awaiting feedback before making final changes.

Fixes #17 Continuation of #18

Thank you for any response.

mathiasbynens commented 5 years ago

Please make the changes in /src/jsesc.js. /jsesc.js is generated from it when you run the build script.

ziir commented 5 years ago

@mathiasbynens I have made the changes in the source, ran the build script and updated the README. I'm not sure why the Travis build is failing though. Please advise.

ziir commented 5 years ago

I just came across another case of different behavior between jsesc(..., { json: true }) (this PR's version) and JSON.stringify():

> require('jsesc')({ foo: () => {} }, { json: true })
'{"foo":null}'
> JSON.stringify({ foo: () => {} })
'{}'
> require('serialize-javascript')({ foo: () => {} }, { isJSON: true })
'{}'

I believe this falls in the scope of #17.

I will address it in the fork of jsesc I'm currently using for my project, and suggest a patch here.