Giter VIP home page Giter VIP logo

json-chewer's Introduction

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

json-chewer's People

Contributors

vdyalex avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.