blakef / jsonifier Goto Github PK
View Code? Open in Web Editor NEWDynamically generates JSON output from templates
License: MIT License
Dynamically generates JSON output from templates
License: MIT License
Nesting isn't working, tests don't cover correctly. This is the expected behaviour:
let a = new JSONifier({namespace: 'a'}).add({1:2});
> a.build({nest: true}).next().value
{'a': {'1': '2'}}
> a.build({nest: false}).next().value
{'1': '2'}
> a.build('a').next().value
{'1': '2'}
> a.build().next().value
{'a': {'1': '2'}}
Used with namespaces
> a.build({namespace: 'a', nest: true}).next().value
{'1': '2'}
> a.build({namespace: 'a', nest: true}).next().value
{'a': {'1': '2'}}
This is useful because it allows us to use namespaces as a filter (see #4).
Add test which represent these use cases.
Fixes for #4, meant I had to pull the tests for this feature. The idea was that you could extend a class with JSONifier and have some control over how it internally stored objects before build.
This hasn't really every worked, and is a feature which would be useful.
Currently builds allow namespaces to be built by referencing the namespace as a string argument:
new JSONifier({namespace: 'a'}).add({'b': 'c'}).build('a')
> {'b': 'c'}
We need more options to allow output to be customised. Namespaces should be used for 2 cases:
.build('a')
.build({namespace:'a'})
.build({namespace:['a', 'b', 'c']})
let a = new JSONifier({namespace: 'a'})
.add({'a': '1'})
.add({'b': '2'});
let b = new JSONifier({namespace: 'b'})
.add({'b': 'woot'});
b.build('a').next().value
> {'a': '1', 'b': '2'}
b.build({namespace: 'a'}).next().value
> {'a': '1', 'b': '2'}
b.build('b').next().value
> {'b': 'woot'}
b.build().next().value
> {'a': {'a': '1', 'b': '2'}, 'b': {'b': 'woot'}}
b.build({nest: false}).next().value
> {'a': '1', 'b': 'woot'}
The following doesn't work:
let test = new JSONifier().add({a:[{b: () => 'failure'}])
> test.build().next().value
> { 'a': { '0': 'failure' } }
When we expect:
> {'a': ['failure']}
The tests are using containDeep
instead of eql
, which means we're possibly passing tests incorrectly.
When inheriting multiple jsonifier objects with namespaces, you only have the child's states. That is to say all the inherited states are lost.
This means that if we call:
let a = new JSONifier().add('a', function* () { yield* [1,2] });
let b = a.build();
b.next(); b.next();
let c = a.build();
c.next(); // incorrectly is 'undefined'
So it needs to convert generators to iterators in JSONify#build instead of JSONify#add
let test = new jsonifier().add('test', function* test() { yield 1 });
for (let t of test.build()) {
console.log(t);
}
> { 'test': 1 }
> { 'test': undefined }
> { 'test': undefined }
> { 'test': undefined }
...
The current solution is to use the limit
parameter. This isn't very elegant.
Limit now represents these 3 options:
undefined
for generators which have completedA declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.