README
PhonyData - Test Data Generator
This module provides a class that can be used to generate realistic looking test data. It's handier than trying to copy and paste or manually repeat data structures so that a UI can be vetted. The data can be fed into other systems, fake API calls can be made, then it can all happen again. The generator uses a random number generator that can be seeded, so the same data is created time after time.
What It Does
This creates random test data. It's able to produce booleans, numbers, strings, and even complex objects. Rules can select a random element from an array, use a format string, select from a list of different templated values or a custom function can be written. Data is retrieved using getters or function calls. The power is yours.
PhonyData uses some of the approach of casual and data from faker. Both of those projects are great, but have not been maintained in a while and pull requests are just sitting and waiting to be accepted.
Installation
npm can do this for you.
npm install --save-dev phonydata
Upgrades
Version 2 changed how generators are added. You can now add them to the prototype of the object, resulting in faster object creation. There's helpful functions exported, such as defineForObject, which will assist. Everything was rewritten in TypeScript as well in order to provide better type hints to others using the library. Previous code should still work.
Usage
Import or require this in order to get the class.
// Import version
import { PhonyData } from "phonydata";
// Require version
const PhonyData = require("phonydata").PhonyData;
Next, create an instance.
let instance = new PhonyData();
Finally, create data.
console.log(instance.loremSentence);
// Sint quos voluptas.
console.log(instance.loremSentence);
// Dolores et ratione atque, voluptas quia eos consectetur natus.
console.log(instance.loremSentence);
// Explicabo sed ut.
Since this is a class, you could extend it and add additional data generators. It isn't strictly necessary to extend the class to add your own generators. See the documentation for instance.define().
import { PhonyData, defineForObject } from "phonydata";
class PhonyExtended extends PhonyData {}
defineForObject(PhonyExtended.prototype, "sillyUnicodeCharacters",
[ "☃", "☠", "〠", "⍨" ])
defineForObject(PhonyExtended.prototype, "complexObject", () => {
return {
integer: this.integer(0, 9999),
letter: this.letter,
nestedObject: {
loremWord: this.loremWord,
word: this.word
}
};
});
const phonyExtended = new PhonyExtended();
console.log(phonyExtended.sillyUnicodeCharacters);
// ☃
console.log(phonyExtended.complexObject);
// { integer: 1807,
// letter: undefined,
// nestedObject: { loremWord: 'exercitationem', word: 'deserunt' } }
API - Special Functions
There are a few special functions that are primarily used as the engine behind the random data generation.
defineForObject(target, name, generator)
Creates a new data generator and a new underscore function. The underscore function is described a bit where the data generators are listed.
import { defineForObject, PhonyData } from 'phonydata';
class ExtendedPhonyData extends PhonyData {}
defineForObject(ExtendedPhonyData.prototype, 'biasedCoinFlip', function () {
return this.random > 0.8;
});
const instance = new ExtendedPhonyData();
console.log(instance.biasedCoinFlip);
// true
console.log(instance.biasedCoinFlip);
// false
console.log(instance.biasedCoinFlip);
// false
console.log(instance.biasedCoinFlip);
// false
defineForObject(target, name, arrayOfItems)
Shortcut that will make a generator function that produces a random member of the array.
import { defineForObject, PhonyData } from 'phonydata';
class ExtendedPhonyData extends PhonyData {}
defineForObject(ExtendedPhonyData.prototype, 'biasedCoinFlip',
[ "sunny", "rainy", "cloudy", "night" ]);
const instance = new ExtendedPhonyData();
console.log(instance.weather);
// sunny
defineForObject(target, hashOfGenerators)
Adds multiple generators. Shorthand, convenience function.
import { defineForObject, PhonyData } from 'phonydata';
class ExtendedPhonyData extends PhonyData {}
defineForObject(ExtendedPhonyData.prototype, {
numberWord: [ "one", "two", "three", "four", "five" ],
numberWordBigger: [ "twenty", "thirty", "forty" ]
});
const instance = new ExtendedPhonyData();
console.log(instance.numberWord);
// two
console.log(instance.numberWordBigger);
// forty
instance.define(name, generator)
Creates a new data generator and a new underscore function. The underscore function is described a bit where the data generators are listed.
instance.define("biasedCoinFlip", () => instance.random > 0.8);
console.log(instance.biasedCoinFlip);
// true
console.log(instance.biasedCoinFlip);
// false
console.log(instance.biasedCoinFlip);
// false
console.log(instance.biasedCoinFlip);
// false
instance.define(name, arrayOfItems)
Shortcut that will make a generator function that produces a random member of the array.
instance.define("weather", [ "sunny", "rainy", "cloudy", "night" ]);
console.log(instance.weather);
// sunny
instance.define(hashOfGenerators)
Adds multiple generators. Shorthand, convenience function.
instance.define({
numberWord: [ "one", "two", "three", "four", "five" ],
numberWordBigger: [ "twenty", "thirty", "forty" ]
});
console.log(instance.numberWord);
// two
console.log(instance.numberWordBigger);
// forty
formatGenerator(arrayOfParseStrings)
This function is exported from the library and is also available on instances.
// Pick your favorite
import { formatGenerator } from 'phonydata';
const formatGenerator = require('phonydata').formatGenerator;
instance.formatGenerator
Creates a generator function that should be passed into instance.define(). The generator function will select a random string from the array and pass it through instance.parse().
instance.define("someDigits", instance.formatGenerator([ "#", "##", "###" ]));
console.log(instance.someDigits);
// 553
console.log(instance.someDigits);
// 70
parseGenerator(arrayOfParseStrings)
This function is exported from the library and is also available on instances.
// Pick your favorite
import { parseGenerator } from 'phonydata';
const parseGenerator = require('phonydata').parseGenerator;
instance.parseGenerator
Creates a generator function that should be passed into instance.define(). The generator function will select a random string from the array and pass it through instance.parse().
instance.define("mood", instance.parseGenerator([ "happy", "sad", "bored" ]));
instance.define("pronoun", instance.parseGenerator([ "he", "she", "it" ]));
console.log(instance.mood);
// bored
console.log(instance.parse("{{pronoun}} is {{mood}}"));
// it is sad
instance.define("speech", instance.parseGenerator([
"{{pronoun}} is {{mood}}",
"{{pronoun}} is not {{mood}}",
"{{mood}} {{pronoun}} is"
]));
console.log(instance.speech);
// sad it is
sequenceGenerator(arrayOfValues)
This function is exported from the library and is also available on instances.
// Pick your favorite
import { sequenceGenerator } from 'phonydata';
const sequenceGenerator = require('phonydata').sequenceGenerator;
instance.sequenceGenerator
Returns a function that will provide the values in the array sequentially. When at the end, the list will begin again from the beginning.
instance.define("powerLevel", instance.sequenceGenerator([ "low", "medium", "high" ]));
console.log(instance.powerLevel);
// low
console.log(instance.powerLevel);
// medium
console.log(instance.powerLevel);
// high
console.log(instance.powerLevel);
// low
instance.seed(number?)
Seeds the random number generator. When number is not passed, the generator is seeded with 0.
instance.seed();
console.log(instance.random);
// 0.548813502304256
console.log(instance.random);
// 0.5928446163889021
instance.seed(0);
console.log(instance.random);
// 0.548813502304256
console.log(instance.random);
// 0.5928446163889021
API - Data Getters
Each of these generators are able to be used in two different ways. First, you may use them as you would any other property. It just returns a different value each time. Secondly, you may add an underscore to the beginning, such as _name to access a function.
// This shows the "random" getter.
console.log(instance.random);
// 0.42365479678846896
// Calling the function version of this same generator.
console.log(instance._random());
// 0.6235636963974684
This is a complete list of getters. Ones tagged with » at the beginning indicate properties that are overridden when using a locale.
| Generator | Description | Sample Shown As JSON |
|---|---|---|
| addressLine1 | » The first line of an address. |
"915 Quae Laboriosam" |
| buildingNumber | » A number of a building as a number. |
404 |
| alphaNumericLower | » A lowercase letter or number as a string. |
"w" |
| alphaNumericUpper | » A capitalized letter or number as a string. |
"G" |
| boolean | Boolean. | true |
| byteHex | A hexadecimal value of a single 8-bit byte. | "4b" |
| byteValue | A decimal value of a single 8-bit byte. | 75 |
| city | » Name of a city or town. |
"Blanditiis" |
| cssBasicColorName | A valid CSS3 color name. | "olive" |
| cssColorName | An extended CSS3 color name. It's also capitalized. | "SpringGreen" |
| currency | An object that details a random type of currency. | See note below. |
| currencyCode | Three-letter code for a currency. | "IQD" |
| currencyDigitalCode | Three-digit code for a currency. | "776" |
| currencyName | How the currency is said in conversation. | "Somali Shilling" |
| currencySymbol | The symbol for a currency. Might be an empty string. | " |