fast-stringify icon indicating copy to clipboard operation
fast-stringify copied to clipboard

Stable keys option?

Open yang opened this issue 4 years ago • 1 comments

Thanks for the wonderful library!

Would you consider adding stable stringify as an option? This would then entirely displace json-stable-stringify and pretty much all other safe/stable stringify libraries (which besides being slower also doesn't encode circular locations). I understand this probably comes with a perf hit.

yang avatar Apr 28 '21 22:04 yang

I have no objections to this option, however the problem lies in the idea of options themselves. The library currently has no concept of "options" ... perhaps naively it leverages the same contract as JSON.stringify with the additional fourth parameter of circularReplacer. This decision was due both to the fact there were no other options at the time, and that it also makes this a drop-in replacement for json-stringify-safe. The introduction of options would be a conversion of that fourth param to instead be an object including both circularReplacer and this new isStable option.

That said, as breaking changes go this is pretty easy of a migration (a custom circularReplacer is a rare use-case), so definitely something I can explore. I'll need to play around a bit to make sure I can still leverage JSON.stringify internally (json-stable-stringify has a custom stringifier, which is slower than native but offers more control), and need to account for other data types (Map, Set, etc., which json-stable-stringify does not account for), but I think it is doable.

planttheidea avatar Apr 28 '21 22:04 planttheidea