Mock-data generation – is quite trivial task, while talking about creating several objects with different values in the some fields (e. g. json-generator, mockaroo and npm-modules – ?q=json+generator)
I’ve found out these tools work fine only for generation a determined number of combinations only. Although my task is to generate all possible JSON-combinations according from the data provided. Let’s have a look at small example: there is a template {p1: '{{a}}', p2: '{{b}}'}
and for a
two options are available – 1,2
and for b
there are two options – 3,4
. I expect to get 4 objects as a result:
[ {a: 1, b: 3}, {a: 1, b: 4}, {a: 2, b: 3}, {a: 2, b: 4} ] |
The generation algorithm is quite simple and has three nested loops:
loop through templates loop through keys in the every template where some replace should be done loop through data |
There are few critical points:
- The replace could be required for the deep nested keys:
{a: {b: '{{replace_me}}'}}
or{a: [{b: '{{replace_me}}'}]}
- Multiple replace could be required in the one key
How to figure this out? JSONium is a “class” created for generation all possible object-combinations. It’s source code is available on github – link. Install:
npm i jsonium |
Small use-case:
var Jsonium = require('jsonium'); var j = new Jsonium(); var operators = [ {OPERATOR: "==="}, {OPERATOR: "!=="} ]; var types = [ {TYPE: "string"}, {TYPE: "object"} ]; var templates = [ { code: "var val = typeof foo {{OPERATOR}} '{{TYPE}}';" }, { code: "var val = '{{TYPE}}' {{OPERATOR}} typeof foo;" } ]; var tests = j .setTemplates(templates) .createCombos(["code"], operators) .useCombosAsTemplates() .createCombos(["code"], types) .uniqueCombos() .getCombos(); |
Output is:
[ { "code": "var val = typeof foo === 'string';" }, { "code": "var val = typeof foo === 'object';" }, { "code": "var val = typeof foo !== 'string';" }, { "code": "var val = typeof foo !== 'object';" }, { "code": "var val = 'string' === typeof foo;" }, { "code": "var val = 'object' === typeof foo;" }, { "code": "var val = 'string' !== typeof foo;" }, { "code": "var val = 'object' !== typeof foo;" } ] |
Even such a small example shows how easy is to add new options to the existing combinations. Combinations with new operators? No problem – just add two lines to the operators
(and there will be 16 combinations):
var operators = [ {OPERATOR: "==="}, {OPERATOR: "!=="}, {OPERATOR: "!="}, {OPERATOR: "=="} ]; |
If I add undefined
, symbol
, function
, number
, boolean
to the types
, I’ll get 56 combinations. A manual prescription is obviously a bad idea.
Let’s talk more about the methods to use. setTemplates
– gets a list of the templates and stores them inside the generator. createCombos
is basically the method-generator. It takes two arguments – a list with keys where the replace should be done and an object with data to replace. useCombosAsTemplates
– sets created combinations to the generator’s templates. It could be helpful, when generation is done in series. uniqueCombos
– filters out the duplicates from combinations. getCombos
– returns generated combinations.
JSONium got its actual use while working on eslint-plugins mocha-cleanup and ember-cleanup or rather when tested them. When the number of test-cases written manually was very large, this process was decided to be automated.
jsonium
- JSONium - еще один генератор коллекций json'ов
- JSONium as another json's collection generator
Add comment