Lorem JSON

Stack: Node, Typescript

Lorem JSON is a mock data generator that outputs lorem ipsum as a JSON object to the console.

Crafting mock data by hand is tedious and time-consuming. Lorem JSON produces customizable valid JSON defined by a single argument.

Example input:

$ lorem-json '{ productId: number/5, productName: string, productDescription: string/s5, productReviews: [string/p1*4] }'

Example output:

{
  "productId": "13074",
  "productName": "commodo",
  "productDescription": "Reprehenderit nulla tempor exercitation. Commodo in fugiat id incididunt Lorem esse consectetur do incididunt exercitation voluptate tempor. Ut occaecat dolore Lorem qui Lorem enim proident nostrud sunt fugiat veniam laborum. Ullamco nulla cillum proident sint. Quis velit cupidatat officia in magna est sunt dolore Lorem aliquip dolor excepteur excepteur duis ea.",
  "productReviews": [
    "Duis voluptate quis est dolor irure ex laboris adipisicing cillum do. Ex proident occaecat incididunt dolor sunt ipsum minim deserunt velit cillum id officia elit. Excepteur dolore consectetur elit consectetur adipisicing cillum aliquip pariatur quis voluptate consectetur magna nisi sint. Laboris esse cupidatat commodo velit nostrud amet ut laborum labore qui aute amet. Irure mollit duis amet ut ullamco occaecat quis sunt. Cupidatat 
laboris irure esse labore aliqua qui consectetur irure ipsum.",
    "Pariatur elit ipsum elit velit magna et cupidatat esse proident adipisicing ullamco mollit. Reprehenderit cupidatat aliqua esse laboris non qui officia ullamco sunt incididunt et. Lorem sunt mollit ea irure non eu tempor fugiat tempor et. Labore eiusmod ad reprehenderit culpa ex aliquip do nisi consequat culpa Lorem eu occaecat incididunt.",
    "Cupidatat incididunt quis cupidatat minim nisi est quis aliqua incididunt. Eu eiusmod do dolore do. Id incididunt occaecat ea ut cupidatat commodo nostrud ad nisi aliquip dolor. Cillum deserunt in aliquip aliquip do 
do reprehenderit deserunt eiusmod."
  ]
}

The syntax for the input is identical to JSON (curly braces enclosing a comma-separated list of key: value pairs) with the exception that the quoting of keys and values is optional, for ease of typing.

Keys are assigned whatever names should appear in the generated JSON. Values are assigned a type (e.g. string or number), followed by a / character and a whole integer which represents how many words should be generated if the type is string, or how many digits should be generated if the type is number. So an input of '{ name: string/2 }' will produce an output like { name: "cupidatat commodo" }, and an input of '{ id: number/5 }' will produce an output like { id: "40175" }.

Values can also be arrays, which are declared by surrounding a primitive value or list of primitive values in square brackets. A value can be repeated within an array with the character *, followed by a whole integer specifying how many times that value should be repeated. An input of '{ ids: [number/5*3] }' will produce an output like { “ids”: [“23131”, “28467”, “69827”] }. Nested objects are also supported.

To produce a JSON object from a string input, Lorem JSON first transforms the input string into a Javascript object. That object is then traversed by a recursive function (ensuring support for nested objects) that applies a callback to each property value. The callback will invoke an appropriate data generator function based on the value type.

This architecture makes the tool highly extensible. Currently the only supported types are string and number, but highly customized types like timestamps or email addresses can be added simply by testing for that type in the callback and then implementing the appropriate data generator function.

Repo: https://github.com/Alamansky/lorem-json