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 = ; var itemDefinition = accountNumber: dfbankaccountNumber // Account Number sortCode: dfbanksortCode // Sort Code accountBalance: dfother // Balance between 1000 and 9999 currency: dfotherccy // Currency exchangeRate: dfother // Exchange rate for that currency eligible: dfop // True or false randomly var generated = df; // Generate 10 rowsconsole; // 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 = ;var itemDefinition = randomFloat: dfnumber
If you want to multiply this number by 10 and then round it, do
var df = ;var itemDefinition = randomFloat: dfop
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 = ;var itemDefinition = myProductCode: dfbankproductCode productName: dfbank
Generator
You can pass another generator as a parameter
var df = ;var itemDefinition = productName: dfbank
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
- 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 = ;var itemDefinition = myProductCode: dfbankproductCode productName: dfbank action1: dfop
which will result in
//... productName: "Current Account"//...