jsarbit
v0.0.1
Published
Random property generator.
Downloads
5
Maintainers
Readme
jsarbit
TODO: Write a project description
Installation
npm install jsarbit
Usage
Example
gen
gen
is set of helper functions to create Arbitrary.
gen.of(value: any)
- Asis
value
generator.
- 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
size
number of elements generated byarb
(orarb
value ifarb
is not Arbitrary).
- Generator of array which has
obj(propArbitrary: object)
- Generator of object.
propArbitrary
is 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
ary
randomly.
- 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()
appliedfunc
tothis.sample()
's return value.
- Returns new Arbitrary which
Arbitrary.flatMap(func: function)
- Returns new Arbitrary which returned by
func
applied 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
func
number ofcount
times 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