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

@meeshkanml/jaymock

v1.1.0

Published

Minimal fake JSON test data generator

Downloads

12

Readme

jaymock

Minimal fake JSON test data generator.

CircleCI XO Codecov

Install

~ ❯❯❯ npm install @meeshkanml/jaymock

Usage

const jaymock = require('@meeshkanml/jaymock')

const data = {
  firstName: 'name.firstName',
  lastName: 'name.lastName',
  ssn: 'ssn',
  address: {
    streetAddress: 'address.streetAddress',
    city: 'address.city',
    zipCode: 'address.zipCode'
  },
  emails: 'internet.email',
  ipAddress: 'internet.ip',
  _repeat: 2
}

const jm = jaymock()

const randExp = require('randexp').randexp
// Add custom functions using `.extend()`
jm.extend({
  ssn: () => randExp(/^\d{3}-\d{2}-\d{4}$/)
})

const fakeData = jm.populate(data)
/*
  [
    {
      firstName: 'Marguerite',
      lastName: 'Will',
      ssn: '076-86-6001',
      address: {
        streetAddress: '4509 Abernathy Port',
        city: 'Port Charles',
        zipCode: '26322'
      },
      emails: '[email protected]',
      ipAddress: '44.210.55.248'
    },
    {
      firstName: 'Fredrick',
      lastName: 'McClure',
      ssn: '610-42-4980',
      address: {
        streetAddress: '56363 Goyette Station',
        city: 'West Floydmouth',
        zipCode: '73634-6751'
      },
      emails: '[email protected]',
      ipAddress: '237.7.221.162'
    }
  ]
*/

Mock API using express

const jaymock = require('@meeshkanml/jaymock')
const express = require('express')

app = express()
app.use(express.json())

const jm = jaymock()
jm.extend('chance', new require('chance')())

app.post('/', (req, res) => res.json(jm.populate(req.body)))

app.listen(3000)

API

.populate(template)

Returns an object, populated with fake data.

template

Type: object

Each object's value can be one of Faker.js's API methods, in the format '{topic}.{subtopic}' (e.g. 'name.firstName') or a custom method, defined using the .extend function, in the format '{function_name}' or {function_name}.{nested_function_name} (e.g. 'foo' will call foo() and 'foo.bar' will call foo.bar()).

A fake value can be generated n times, into an array of n values, by including |n at the end of the individual object's method name (e.g. 'name.firstName|5' will generate an array, populated with 5 fake first names). This also works with custom functions, accordingly.

To use the faker.fake() method (which permits the combination of faker API methods), use the format 'fake({mustache_strings})' (e.g. 'fake({{name.lastName}}, {{name.firstName}} {{name.suffix}})').

.extend(name, body)

Adds a custom data generation function that can be called in the .populate template using the value of name.

name

Type: string

body

Type: function

.extend(functions)

Adds custom data generation functions that can be called in the .populate template using the value of each object key.

functions

Type: object

Each object key should be the relevant function's name and value the function's body (e.g. { 'chance': new require('chance')() }).

.setFakerLocale(locale)

Sets Faker.js's language locale.

locale

Type: string

Any of Faker.js's locale options.

.setFakerSeed(seed)

Sets Faker.js's randomness seed.

seed

Type: number

Related

Contributing

Thanks for wanting to contribute! We will soon have a contributing page detailing how to contribute. Meanwhile, feel free to star this repository, open issues, and ask for more features and support.

Please note that this project is governed by the Meeshkan Community Code of Conduct. By participating in this project, you agree to abide by its terms.

Credits

  • Faker.js is used as jaymock's core fake data generator.

License

MIT © Meeshkan