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 🙏

© 2025 – Pkg Stats / Ryan Hefner

json-mocker

v1.2.2

Published

An extendable tool to generate mock json data.

Readme

JsonMocker - An extendable tool to generate mock json data.

Usage

Node.js

1) Quick demo

var mocker = require('json-mocker');

var mockdata = mock.build({
  count: 50,
  index: 1000,
  template: {
    name: {
      first: 'firstName()',
      last: 'lastName()'
    },
    address: {
      street: 'street()',
      city: 'city()',
      state: 'state()'
    }
  }
});

//More examples in the examples directory

2) Extend the library with a custom method using provider

var mocker = require('json-mocker');

mocker.provider.add({
  type: 'quality',
  data: ['learned', 'hardworking', 'careless', 'casual', 'professional']
});

jsonMocker.provider.add({
  type: 'color',
  data: [{"_id":"c001","name":"red","hex":"#f00"},{"_id":"c002","name":"green","hex":"#0f0"},{"_id":"c003","name":"blue","hex":"#00f"},{"_id":"c004","name":"cyan","hex":"#0ff"},{"_id":"c005","name":"magenta","hex":"#f0f"},{"_id":"c006","name":"yellow","hex":"#ff0"},{"_id":"c007","name":"black","hex":"#000"}]
});

var mockdata = mock.build({
  count: 50,
  index: 250,
  template: {
    name: 'firstName()',
    quality: 'quality()',
    color: 'color()',
    desc: function () {
      return 'firstName() has ' + this.quality() + ' quality. His/Her favorite color is: ' + this.util.getValueById('color', this.color()).name + '.';
    }
  }
});

3) Direct invocation of data service methods.

var jsonMocker = require('../');

var dataItem = jsonMocker.dataItem(20);

console.log(dataItem.index());
console.log(dataItem.email());
console.log(dataItem.company());
console.log(dataItem.salary());


for (var i = 0; i < 10; i++) {
  dataItem = jsonMocker.dataItem(i);
  console.log(JSON.stringify({
    id: dataItem.index(),
    name: dataItem.firstName() + ' ' + dataItem.lastName(),
    gender: dataItem.gender(),
    ratings: dataItem.ratings(),
  }, null, 2));
}

API - base methods

JsonMocker.build

JsonMocker.provider.add

API - data service methods

method | Usage | Description | Returns same value * | --- | --- | --- | --- | index|index()|Index of current data item. It's starting number can be specified at root level using property index|true| boolean|boolean()|Random boolean value - true or false|true| date|date(fromYear, toYear, momentFormat)|fromYear and toYear should be a four-digit integer. The date format can be given in MomentJs formats (http://momentjs.com/docs/#/displaying/)|false| integer|integer(topLimit)|topLimit should be a integer. The random integer returned by this method, will be less than topLimit.|false| gender|gender()|The tool sets a random gender for each data item.|true| firstName|firstName()|Returns a random first name based on the set gender by gender()|true| lastName|lastName()|Returns a random last name based on the set gender by gender()|true| email|email()|Returns email based on firstName, lastName and company name|true| street|street()|Returns a address with 3 parts - number, street part 1 and street part 2|true| city|city()|Returns a random US city. It belongs to the same state return by state()|true| state|state()|Returns a random US state.|true| company|company()|Returns a random company name.|true| phone|phone()|Returns a 10 digit number|true| lorem|lorem(format)|The format parameter should be a combination of integer (for count) and any one of the letter 'w', 's' or 'p' (for words, sentences and paragraphs respectively). Examples: '10s', '38w', '5p'. If not mentioned, count will default to 10 and text type default to words.|false| sibling|sibling()|This returns one index value other than the current value. This can be used to create relationship among the data items|false| siblings|siblings(max, noEmptyList)|This returns a list of index values excluding the current value. This can be used to create relationship among the data items. The number of values in the list is random with max argument as upper limit.|false|

(*) - Returns same value for every use in a template.