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-creator

v0.0.5

Published

Create json data from a template object with ES6 Template Literals.

Downloads

37

Readme

json-creator

Generate JSON data from a JSON template. Thanks to ES6 Template Literals, we dynamicly evaluate the string parts using the notation ${}.

This is a useful library for dummy data generating or generally data processing.

Usage

npm install json-creator
import jsonCreator from 'json-creator';
// or using CommonJS
// const jsonCreator = require('json-creator').default;

const template = [
    {
        '3..5': {
            name: '${FAKE("{{commerce.productName}}")} ${RANDINT(1000,10000)}'
        }
    }
];
const [result, err] = jsonCreator(template);
// result:
// [
//   {
//     'name': 'Handmade Metal Salad 2102'
//   },
//   {
//     'name': 'Small Metal Mouse 6396'
//   },
//   {
//     'name': 'Rustic Granite Shirt 5762'
//   },
//   {
//     'name': 'Ergonomic Soft Chicken 5489'
//   }
// ]

API

The json-creator interface was some kind of inspired by json-generator (by Vazha Omanashvili), but simpler to learn and use, IMO.

function jsonCreator(
    template = null,
    dataContext = {},
    methodContext = utilFunctions,
    dataNamespace = '_',
    methodNamespace = ''
)

The template is a json-like any-type template data.

  • Basicly, it will produce json as it is. e.g. {name: ['Charlie', 'Green'], age: 18} will produce {name: ['Charlie', 'Green'], age: 18}.

  • In a template part, if there is an array with an object element obj whose key is in form of a..b or a...b, (a and b are integers and a<=b), it will produce an array with x elements a<=x<=b, and the x elements are generated by the obj's value. e.g. ['3..5': {name: ['Charlie', 'Green'], age: 18}] (['3...5': {name: ['Charlie', 'Green'], age: 18}] the same) will produce something as below or an array with maybe 4 or 5 elements.

    [
        { name: ['Charlie', 'Green'], age: 18 },
        { name: ['Charlie', 'Green'], age: 18 },
        { name: ['Charlie', 'Green'], age: 18 }
    ];
  • Additionally, any string part in the template (including object keys) who contains ES2015 template literals ${xx} (without back-tick), will be dynamicly evaluated. e.g. {name: ['Charlie', 'Green'], age: '${Math.round(Math.random()*50)}'}. will produce { name: [ 'Charlie', 'Green' ], age: 10 }.

  • The dynamic evaluation uses two contexts dataContext (default under namespace _, you can change it by parameter dataNamespace) and methodContext (default under global namespace, set by empty string '', you can also change it by parameter methodNamespace). e.g.

// generate data with dataContext
jsonCreator(
    {
        name: ['${_.firstName}', '${_.lastName}'],
        age: '${RANDINT(_.from, _.to)}'
    },
    {
        firstName: 'Charlie',
        lastName: 'Green',
        from: 10,
        to: 50
    }
);
// [ { name: [ 'Charlie', 'Green' ], age: 32 }, [] ]

This library uses JavaScript ES2015 template literals evaluation behind the scenes, but there are 2 key differences in our dynamic evaluation.

  • You do not need to "back-tick" the template, just use normal string (without any back-ticks)
  • ES2015 template literals return only string, but our occupiesdynamic evaluation keeps the type when the expression the whole template, which is in form'${xx}'(in constrast to'ww\${xx}yy\${zz}').

Just like the example above, even though the age in the template is defined as a string, the evaluation result will keep the type (age: 32 is evaluated into number, not a string "32"). But '{${1} ${2}}' will be evaluated into '1 2', since a number type cannot hold both values and a space between them.

Also, there are some default utilFunctions that is convenient to use. You can import and use them directly, or add your own util functions. e.g.

import jsonCreator, { utilFunctions } from 'json-creator';
import moment from 'moment';
// or using CommonJS
// const jsonCreator = require('json-creator').default;
// const { utilFunctions } = require('json-creator');
// const momnet = require('moment');

utilFunctions.RANGE(2, 8, 2);
// [ 2, 4, 6 ]

jsonCreator(
    { time: '${MOMENT().format("YYYY/MM/DD")}', description: '${_.str}' },
    { str: 'today' },
    Object.assign({ MOMENT: moment }, utilFunctions)
);
// [ { time: '2019/07/09', description: 'today' }, [] ]

Built-in utilFunctions

Here is a brief list of the currently supported built-in utilFunctions.

  • RANGE(start, stop, step) like Python2 range function, prototype RANGE(stop) and RANGE(start, stop) are also acceptalbe. Returns an array of integers.
  • CHOICE(array) like Python random.choice function, returns one random elememt from an array.
  • RANDINT(a, b) like python random.randint funcion, returns a integer ranged between a and b(included).
  • FAKE Faker.fake function from a powerful but light fake data generator, accepts mastache templates, returns a string.
  • RECURSIVE_REPLACE(obj, target = [], filterCond = value => value === null) will recursively replace values into target when filterCond(value) is true. The default target and filterCond will replace all null value into [], and returns the original obj after the replacement.

CLI

You can install the pacakge globally and use the commandline tool for convinience.

npm install -g json-creator

json-creator command recieves the template (in JSON5 format) from stdin. e.g.

$ echo '[{"3...5": "${RANGE(_.a, _.b)}"}]' | json-creator -d '{a:10, b:15}'

[[10,11,12,13,14],[10,11,12,13,14],[10,11,12,13,14],[10,11,12,13,14],[10,11,12,13,14]]

Output with 4-space indent JSON:

$ echo '[{"3..5": "${FAKE(`{{hacker.phrase}}`)}"}]' | json-creator -i 4

[
    'If we compress the bandwidth, we can get to the PNG sensor through the optical IB program!',
    "I'll navigate the bluetooth CSS capacitor, that should card the RSS program!",
    "You can't transmit the program without overriding the solid state JBOD transmitter!",
    'The PNG bus is down, compress the primary monitor so we can transmit the HDD protocol!'
]

The help message shows the command options.

$ json-creator -h
Usage: cli [options]

Options:
  -V, --version                       output the version number
  -d, --data-context [object]         data context
  -m, --method-context [object]       method context
  -n, --data-namespace [namespace]    data namespace, default to "_"
  -g, --method-namespace [namespace]  data namespace, default to global context
  -i, --json-indent [indent]          output json indent number (default: 0)
  -l, --list                          output available util functions
  -h, --help                          output usage information