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

data-forgery

v1.2.0

Published

Data Forgery for generation of mocked data

Downloads

10

Readme

data-forgery

Version 1.2

Library is used for forging mocked data. It supports simple dependencies among the fields (for generating code-name values). It also supports post-generating operations, such as rounding, concatenating, etc.

Quick Start

Install package

npm install data-forgery
npm test

Create a javascript file (myGenerator.js)

"use strict"
var df = require("data-forgery");

var itemDefinition = {
    accountNumber: df.bank.accountNumber,                                       // Account Number
    sortCode: df.bank.sortCode,                                                 // Sort Code
    accountBalance: df.other.flt(1000, 9999),                                   // Balance between 1000 and 9999
    currency: df.other.ccy,                                                     // Currency
    exchangeRate: df.other.exchangeRate("{currency}"),                          // Exchange rate for that currency
    eligible: df.op.random(true, false)                                         // True or false randomly
}

var generated = df.forge(10, itemDefinition);                                   // Generate 10 rows
console.log(JSON.stringify(generated, null, 3));                                // Print out on screen

Save and run

node myGenerator.js

About

The engine consists of 3 types of operations

  • Generators, which are responsible for value generation

  • Operators, which are responsible for modification of generated value (such as formatting, concatenation, multiplication of numbers, etc.)

  • Actions, which are responsible for performing actions on the "item" level (such as deleting given property after processing)

For example

To generate random float number between 0 and 5, do

var df = require("data-forgery");
var itemDefinition = {
    randomFloat: df.number.flt(0, 10)
}

If you want to multiply this number by 10 and then round it, do

var df = require("data-forgery");
var itemDefinition = {
    randomFloat: df.op.round(df.op.multiply(df.number.flt(0, 5), 10))
}

Argument types

There are 3 argument types which you can pass to the operators and generators

Raw value

Examples are 10, "abc", 0.111123, true, false, null

Referenced Values
var df = require("data-forgery");
var itemDefinition = {
    myProductCode: df.bank.productCode,
    productName: df.bank.productName("{myProductCode}"),
}
Generator

You can pass another generator as a parameter

var df = require("data-forgery");
var itemDefinition = {
    productName: df.bank.productName(df.bank.productCode),
}

Generators

Currently, the following generators are available: Generator is referenced by a full namespace, for example

df.date.randomDate

Date (df.date.*)
  • randomDate(minDate, maxDate)
  • randomTime(minTime, maxTime)
Number (df.number.*)
  • flt(min, max)
  • int(min, max)
String (df.string.*)
  • firstName
  • lastName
  • postCode
  • company
  • street
  • town
  • address
  • email
  • random(length, [stringOfPossibleCharacters])
Other (df.other.*)
  • ccy
  • exchangeRate(targetCurrency, baseCurrency)
Product (df.product.*)
  • productCode
  • productName(productCode)
  • productCategory(productCode)
  • productType(productCode)
  • productTypeDescription(productType)
Bank (df.bank.*)
  • frn
  • frnUk
  • frnUlster
  • bankName
  • franchiseCode
  • franchise
  • accountNumber
  • sortCode
  • scvId
  • customerType:
  • customerTypeNonIndividual
  • customerTypeDescription
  • accountStatusCode

Operators / Formatters

All operators are stored in df.op namespace.

Currently, following formatters and post processing operations are available

  • op.round(numericValue)
  • op.dateFormat(dateRef, format)
  • op.timeFormat(timeRef, format)
  • op.join(args...)
  • obsolete op.use(valueRef) - use op.ref instead
  • op.ref(valueRef)
  • op.random(args...)
  • op.sequence(args...)
  • op.sequenceSpread(values, spreadAcrossItemsCount)
  • oneInN(desiredValue, inHowMany, ifNotDesiredThenWhatValue)
  • when(value, compareAgainst, ifTrue, ifElse)
  • dictValue(key, dictionary)
  • genToArray(value, generatedArraySize)
  • multiply(value1, value2)
  • divide(value1, value2)
  • sum(args...)
  • addMinutes(date, numberOfMinutesToAdd)
  • substr(string, start, length)

Actions

Actions don't generate or modify values. They perform some other action instead.

  • action.delete(fieldName)
Example of action:
var df = require("data-forgery");
var itemDefinition = {
    myProductCode: df.bank.productCode,
    productName: df.bank.productName("{myProductCode}"),


    action1: df.op.delete("myProductCode")
}

which will result in

//...
{
  productName: "Current Account"
}
//...