npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-chewer

v1.1.4

Published

Generate random JSON file using pure javascript

Downloads

20

Readme

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