Support shorthand object initializer syntax (fixes #9) #10
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This allows usage of ES6 Object Initializer Shorthand Syntax in the template. (JSON should continue working)
Examples:
fony -t '{ name, age, address }' will expand the template to
{ "name": "name", "age": "age", "address": "address" } and then generate fony data as per usual.
fony -t '{ name, age, location: address }' will expand the template to
{ "name": "name", "age": "age", "location": "address" }
Nested / array support continues to work so
fony -t '{ names: { name1: name, name2: name }, location: address, phones: [ "phone", 3 ] }'
will work just fine.The template is transformed via a trivial babel plugin (src/transform.js).
Apart from less typing (always a good thing) it also makes working on Windows much easier, since quotes are no longer mandatory for keynames or values!
While this works, I realized we could support much more complex syntax and be more efficient if createData were to accept an AST instead of an object instance. We could for example allow syntax like this: (emmet flavoured?)
{name, age, phone, known_locations: address*3 }
If there is interest I will work on a PR for emmet like syntax.