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

half-faked

v1.0.0

Published

A simple tool for generating fake data using schemas.

Downloads

5

Readme

Half-Faked Build Status

Half-Faked is a tool for generating fake data for a given schema definition. It was originally apart of the Restfool library as the fixture() function, but has since been isolated into its own module. This tool can be used for creating more accurate and varied test cases, modeling data architecture, or even setting up mock services that deliver data for your front-end team(s) to develop against.

Half-Faked helps you get something close to what your final data will look like, without being your final data!

Getting Started

Install via npm:

Note: We recommend installing the awesome faker library to use in conjunction with half-faked.

npm install --save-dev half-faked

Then, require it in your project like so...

const createFactory = require('half-faked');

Create a Factory

This library exposes a single function that accepts either an object literal or function that provides the desired output schema. Any functional values found will be called and their results can will also be checked for functional values to invoke.

type createFactory = (Object|Func schema) -> Factory

The return value of this function is a "factory" function that will be used for generating n number of copies.

type Factory = (Integer count [, Object|Func modifier]) -> Array

Note: As previously mentioned, we're using the faker library below to quickly generate some fake data.

const createFactory = require('half-faked');
const faker = require('faker');

// object literal
const makeAddress = createFactory({
  street: faker.address.streetAddress,
  city: faker.address.city,
  state: faker.address.usState,
  country: 'usa',
  zipcode: faker.address.zipCode
});

// function
const makeUser = createFactory(function () {
  return {
    name: faker.name.firstName() + ' '+ faker.name.lastName(),
    email: faker.internet.email,
    password: faker.random.uuid,
    addresses: makeAddress(5)
  };
});

Generating Data

Now that you've created a factory for your data, you can start creating rows using the returned factory function. You can create a single item by providing no first argument, or you can create a collection of items by specifying a count of 1 or greater.

var one = makeUser();
// => { name: 'Nick Glenn', email: ... }

var oneInArray = makeUser(1);
// => [ { name: 'Nick Glenn', email: ... } ]

var many = makeUser(5);
// => [ {...}, {...}, {...}, {...}, {...} ]

You can override the results of custom values by passing an object or function in as a second argument. This is called a modifier.

Using an object as a second argument will simply merge the given object with the data of each row in the collection, overriding the value of any existing matched keys.

var active = makeUser(2, { active: true });
// => [ {name: '...', active: true}, {name: '...', active: true} ]

Providing a function will give you a greater level of control. The generated object will be passed to the function and whatever is returned will take its place.

var users = makeUser(10, function (user) {
  if (user.email === '[email protected]') {
    user.password = '123456789';
  }

  return user;
});