jsarbit
v0.0.1
Published
Random property generator.
Maintainers
Readme
jsarbit
TODO: Write a project description
Installation
npm install jsarbitUsage
Example
gen
gen is set of helper functions to create Arbitrary.
gen.of(value: any)- Asis
valuegenerator.
- Asis
int(min: number, max: number)float(min: number, max: number)posNum()posNum(max: number)negNum()negNum(min: number)- Generator of numbers in range.
array(element: Arbitrary or any value, sizeMin: number, sizeMax: number)array(element: Arbitrary or any value, sizeMax: number)- Generator of array.
- Example:
var dogOrCatArray = gen.array(choose('dog', 'cat'), 10); var someOfPizza = gen.array('pizza', 1, 10);
array(element: Arbitrary or any value, sizeMin: number, sizeMax: number)array(element: Arbitrary or any value, sizeMax: number)- Generator of array.
arrayOfN(size: number, arb: Arbitrary or value)- Generator of array which has
sizenumber of elements generated byarb(orarbvalue ifarbis not Arbitrary).
- Generator of array which has
obj(propArbitrary: object)- Generator of object.
propArbitraryis object which property takes Arbitrary.- Example:
var arb = gen.obj({ name: gen.oneOf("Alice", "Bob"), address: { country: "Japan", // No Arbitrary property zip: gen.numericStr(3) }, }); var sample = arb.sample(); /* { name: "Alice", address: { country: "Japan" // as it is. zip: "491" } } */
choose(min: number, max: number)- Generator of arbitrary number in range.
oneOf(ary: array)- Generator which choose a element of
aryrandomly.
- Generator which choose a element of
bool()- Generator of boolean.
alphaLowerChar()alphaUpperChar()alphaChar()numericChar()alphaNumericChar()- Generator of a char.
charSeqOf(arb: Arbitrary which generates of char, minLen, maxLen)charSeqOf(arb: Arbitrary, minLen)- Generator of string consisted of
arb's sample.
- Generator of string consisted of
numericStr(minLen, maxLen)numericStr(maxLen)identifier(minLen, maxLen)identifier(maxLen)- Generator of string.
frequency(freq: array)- Chooses one of the given generators with a weighted random distribution.
- Example
var arb = gen.frequency([ [1, gen.oneOf(['a', 'b', 'c'])], [9, gen.choose(1,10)] ]); forAll(arb, function (value) { console.log(value); // char generated 10% of all. });
Arbitrary
Arbitrary is an arbitrary value generator class.
new Arbitrary(sampleFunc: function)- Creates new instance which has
sample()function assampleFunc.
- Creates new instance which has
Arbitrary.sample()- Generates arbitrary value.
Arbitrary.map(func: function)- Returns new Arbitrary which
sample()appliedfunctothis.sample()'s return value.
- Returns new Arbitrary which
Arbitrary.flatMap(func: function)- Returns new Arbitrary which returned by
funcapplied withthis.sample()'s return value.
- Returns new Arbitrary which returned by
forAll
function to generate with Arbitrary.
forAll(arb: Arbitrary, func: function, count=100)- Call
funcnumber ofcounttimes with arbitrary value generated byarb. - Example
forAll(choose(0, 50), function(num) { assert(num >= 0 && num <= 50); });
- Call
Contributing
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature - Commit your changes:
git commit -am 'Add some feature' - Push to the branch:
git push origin my-new-feature - Submit a pull request :D
Credits
TODO: Write credits
License
MIT
