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

Added stringify method for custom object serialization #136

Closed
wants to merge 1 commit into from

Conversation

LeaVerou
Copy link
Owner

It allows custom separators as well as functions to pass keys and values through for maximal flexibility.
For example, URL param serialization would be:

$.stringify(data, {pair: "=", line: "&", value: encodeURIComponent});

With a different value function, one can even have nested URL serialization, as has been requested in #128.
Adds about 100 bytes to the minified & gzipped result.

Thoughts?
Alternatively, it could be a plugin if you guys don't think it's that useful.

@dzek69
Copy link

dzek69 commented Dec 15, 2015

Adds about 100 bytes to the minified & gzipped result.

Thoughts?
Alternatively, it could be a plugin if you guys don't think it's that useful.

My thought is even it is nice to have small library size, we shouldn't review the need of something to exists from lib size perspective. It doesn't matter that much if this expands the size of the lib by 100 or 200 bytes :) We all know that one image may be exact size as all libraries added to the page.

Don't get me wrong, but really, some bytes isn't a difference that would make any sane person to choose one library over another ;)

@LeaVerou
Copy link
Owner Author

It’s a slippery slope though, and library size tends to increase over time, so we do need to be conservative, otherwise Bliss will end up just as bloated as the libraries it's trying to replace after a few years.

@LeaVerou LeaVerou closed this Dec 15, 2015
@LeaVerou LeaVerou reopened this Dec 15, 2015
@zdfs
Copy link
Collaborator

zdfs commented Dec 15, 2015

I think if we feel like this is a solution that will address 80% of the use cases, sure. This isn't a use case that I have a lot, so I may not fit the target audience. How does everyone else feel about it?

@MaxArt2501
Copy link

I get this function just works for simple key/value pairs. For example, I can't use it to pass arrays in a query string.
Should it work with more complex structures?

It would also be nice if $.fetch could use it to create the query string out of the data property (which, currently, also doesn't support arrays). It would also justify its existence as a separate method.

@LeaVerou
Copy link
Owner Author

@MaxArt2501 That’s why it supports the key and value parameters, which are functions, so they could do more complex things.

@MaxArt2501
Copy link

@LeaVerou Yes, but from what I see they're intended as transform functions of the keys or the values only, but not both, which is necessary to create query strings like a[]=foo&a[]=bar out of objects like { a: [ "foo", "bar" ] }.

@LeaVerou
Copy link
Owner Author

Oh, right! There could be a custom callback too then, which would default to the current one.

But more and more, I’m starting to think what we really need is a plugin system, with hooks, just like PrismJS.

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.

None yet

4 participants