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

feat: add genObjectFromValues #9

Merged
merged 6 commits into from
Mar 30, 2024

Conversation

ricardogobbosouza
Copy link
Contributor

@ricardogobbosouza ricardogobbosouza commented Apr 26, 2022

Sometimes it is necessary to generate an object preserving types

// in this case 'bar' is undefined
genObjectFromRaw({ foo: 'bar' }) = '{ foo: bar }';

// in this case 'bar' is a string
genObjectFromValues({ foo: 'bar' }) = '{ foo: "bar" }';

@ricardogobbosouza
Copy link
Contributor Author

ricardogobbosouza commented Apr 26, 2022

wdyt @danielroe, @pi0 ?

@pi0
Copy link
Member

pi0 commented Apr 26, 2022

Thanks for the PR @ricardogobbosouza. I believe this had to be default behavior preserving types for serialization. /cc @danielroe Any reason why not to make default?

@ricardogobbosouza
Copy link
Contributor Author

@pi0 I didn't put it as default because this is a breaking change

@ricardogobbosouza
Copy link
Contributor Author

Maybe rename to preservingTypes

@pi0
Copy link
Member

pi0 commented Apr 26, 2022

I'm fine with breaking change if necessary. We do it quite often for unjs packages. Only want to ensure we have the same vision about utility behavior.

@danielroe
Copy link
Member

The major purpose of this utility is in order to render strings as 'raw', allowing functions, and other non serialisable values, to be serialised.

I would suggest JSON.stringify or, if that doesn't work for your use, a different utility (genObject) with more information on why it's needed.

@pi0
Copy link
Member

pi0 commented Apr 26, 2022

The main purpose of this package was to render valid JS code. But I'm fine if by convention left values are considered to be JS code we could simply use genObjectFromRaw({ foo: '"bar"' }). While an option could be nice, I also prefer each function have almost same purpose and behavior.

Having genObjectFromValue that stringifies the right values seems a nice idea 👍🏼 (since looking at current utils, most of them consider Rval as js code not value. genObject would make it different)

@ricardogobbosouza
Copy link
Contributor Author

ricardogobbosouza commented Apr 26, 2022

It makes more sense to have another function like genObjectFromValue.
I will refactor this PR

@ricardogobbosouza ricardogobbosouza changed the title feat: add option to preserveString values feat: add genObjectFromValues Apr 28, 2022
@ricardogobbosouza
Copy link
Contributor Author

@pi0, @danielroe
added getObjectFromValues

@ricardogobbosouza
Copy link
Contributor Author

@pi0, @danielroe Any idea when it will be merged?

Copy link

codecov bot commented Mar 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.75%. Comparing base (9d75498) to head (9767e7e).
Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main       #9      +/-   ##
==========================================
+ Coverage   97.36%   98.75%   +1.38%     
==========================================
  Files           7        7              
  Lines         380      402      +22     
  Branches       72       77       +5     
==========================================
+ Hits          370      397      +27     
+ Misses         10        5       -5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@pi0 pi0 merged commit 6c03a07 into unjs:main Mar 30, 2024
4 checks passed
pi0 added a commit that referenced this pull request Mar 30, 2024
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.

3 participants