Skip to content

Latest commit

 

History

History
242 lines (185 loc) · 5.04 KB

README.md

File metadata and controls

242 lines (185 loc) · 5.04 KB

JSON Chewer

TL;DR:

Generate random JSON file using pure javascript. It removes the complexity and inconsistences from creating string-based methods such as in madoka.


Benefits

  1. Write reliable code and avoid mistakes with real javascript code.
  2. Write complex algorithms in your own way.
  3. Use the libraries you need for the seed file.
  4. Use highlight from your favorite code editor.
  5. Use all Javascript (ES5 and ES6) features.
  6. Get rid of RegEx and eval methods to evaluate your own code.
  7. Use both API and CLI for generating files.

Instead of using string notations, you can write full javascript code:

// Madoka style
{
  company: '{{ company().toUpperCase() }}'
}

// JSON Chewer style
{
  company: () => faker.company.companyName().toUpperCase()
}

Installation

Install it locally or globally as a npm package:

# Local
$ npm install --save json-chewer

# Global
$ npm install --global json-chewer

Getting started

  • Create a Node.js file with the seed:
const { faker, repeat } = require('json-chewer');

module.exports = {
  users: repeat(5, () => faker.name.firstName())
};
  • Run the command line:
$ json-chewer my-seed.js
  • Get the output:
{
  "users": ["Mariela", "Shanny", "Misael", "Tyrell", "Elian"]
}

IMPORTANT: There are differences between creating repeated values and creating repeated values into functions:

module.exports = {
  users: repeat(5, faker.name.firstName())
};
// Output example
{
  "user": [ "Mariela", "Mariela", "Mariela", "Mariela", "Mariela" ]
}

Scope inheritance

There is a scope management on each parsing step. The generator automatically modifies the children properties' functions to bind their parent scopes.

module.exports = {
  username: () => faker.internet.userName(),
  profile: function () {
    // 'this' inherited from parent object
    return `http://example.com/${this.username}`;
  }
}

// Output example
{
  "username": "Verdie30",
  "profile": "http://example.com/Verdie30"
}

In case of arrays, it also binds the parent scope. However, it refers to the immediate parent object, not to the array:

module.exports = {
  name: () => faker.commerce.product(),
  slogans: repeat(1, 5, function () {
    // 'this' inherited from immediate parent object, skipping its array
    const product = this.name;

    const adjective = faker.commerce.productAdjective();

    return `${adjective} ${product}`;
  })
}

// Output example
{
  "name": "Chair",
  "slogans": [
    "Ergonomic Chair",
    "Fantastic Chair",
    "Intelligent Chair"
  ]
}

IMPORTANT: Due to arrow function specifications, it's only possible to use this from lexical environment if it's enclosed into a regular function:

module.exports = {
  username: () => faker.internet.userName(),
  link: function() {
    return {
      profile: () => this.username  // 'this' inherited from regular function
    }
  }
}

// Output example
{
  "username": "Alaina_Klocko70",
  "link": {
    "profile": "Alaina_Klocko70"
  }
}

Built-in features

Faker

It includes Faker as a library for generating fake data samples.

Check the Faker.js documentation before using it: https://github.com/Marak/faker.js/wiki.

It might be imported directly from the JSON Chewer:

const { faker } = require('json-chewer');

Repeat

There is a built-in function for generating arrays with specific values.

Repeated values

const { repeat } = require('json-chewer');

module.exports = {
  foo: repeat(3, 'bar')
}

// Output example
{
  "foo": [ "bar","bar","bar" ]
}

Random values

const { repeat } = require('json-chewer');

module.exports = {
  values: repeat(3, () => Math.random())
}

// Output example
{
  "values": [ 0.6866788951130716, 0.1538252618213385, 0.4480162893196198 ]
}

Random range of items

const { repeat } = require('json-chewer');

module.exports = {
  hello: repeat(1, 5, 'world')
}

// Output example
{
  "hello": [ "world", "world" ]
}

Random range of items with random values

const { repeat } = require('json-chewer');

module.exports = {
  values: repeat(1, 10, () => Math.random())
}

// Output example
{
  "values": [ 0.4480162893196198, 0.1538252618213385 ]
}

CLI

$ json-chewer

  Usage: json-chewer [options] <file>

  Generate random JSON file using pure javascript


  Options:

    -O, --output <file>  Define the output file.
    -p, --pretty         Use pretty formatting to output file.
    -v, --verbose        Display additional information about the generation processing.
    -V, --version        output the version number
    -h, --help           output usage information

To-dos

  • Add built-in support for moment
  • Add built-in support for lodash