Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

half-faked

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

half-faked

A simple tool for generating fake data using schemas.

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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 === 'admin@example.com') {
    user.password = '123456789';
  }

  return user;
});

Keywords

FAQs

Package last updated on 26 Mar 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc