Skip to main content

Constant

Promote any set of constant values to arbitraries.

constant

Always produce the same value

Signatures:

  • fc.constant(value)

with:

  • valuevalue that will be produced by the arbitrary

Usages:

fc.constant(1);
// Examples of generated values: 1…

fc.constant({});
// Examples of generated values: {}…

Resources: API reference.
Available since 0.0.1.

constantFrom

One of the values specified as argument.

Randomly chooses among the provided values. It considers the first value as the default value so that in case of failure it will shrink to it. It expects a minimum of one value and throws whether it receives no value as parameters. It can easily be used on arrays with fc.constantFrom(...myArray).

Signatures:

  • fc.constantFrom(...values)

with:

  • ...valuesall the values that could possibly be generated by the arbitrary

Usages:

fc.constantFrom(1, 2, 3);
// Examples of generated values: 1, 3, 2…

fc.constantFrom(1, 'string', {});
// Examples of generated values: 1, "string", {}…

Resources: API reference.
Available since 0.0.12.

mapToConstant

Map indexes to values.

Generate non-contiguous ranges of values by mapping integer values to constant.

Signatures:

  • fc.mapToConstant(...{ num, build })

with:

  • ...{ num, build }describe how to map integer values to their final values. For each entry, the entry defines num corresponding to the number of integer values it covers and build, a method that will produce a value given an integer in the range 0 (included) to num - 1 (included)

Usages:

fc.mapToConstant(
{ num: 26, build: (v) => String.fromCharCode(v + 0x61) },
{ num: 10, build: (v) => String.fromCharCode(v + 0x30) },
);
// Examples of generated values: "6", "8", "d", "9", "r"…

Resources: API reference.
Available since 1.14.0.

subarray

Generate values corresponding to any possible sub-array of an original array.

Values of the resulting subarray are ordered the same way they were in the original array.

Signatures:

  • fc.subarray(originalArray)
  • fc.subarray(originalArray, {minLength?, maxLength?})

with:

  • originalArraythe array from which we want to extract sub-arrays
  • minLength? — default: 0minimal length (included)
  • maxLength? — default: originalArray.lengthmaximal length (included)

Usages:

fc.subarray([1, 42, 48, 69, 75, 92]);
// Examples of generated values: [], [1,48,69,75,92], [48], [1,42,75], [1,48,75,92]…

fc.subarray([1, 42, 48, 69, 75, 92], { minLength: 5 });
// Examples of generated values: [1,42,48,69,75], [1,42,48,69,92], [1,42,48,75,92], [42,48,69,75,92], [1,42,69,75,92]…

fc.subarray([1, 42, 48, 69, 75, 92], { maxLength: 5 });
// Examples of generated values: [48,75], [1], [], [48,92], [69,75]…

fc.subarray([1, 42, 48, 69, 75, 92], { minLength: 2, maxLength: 3 });
// Examples of generated values: [48,75], [48,69,92], [42,75], [69,92], [1,42]…

Resources: API reference.
Available since 1.5.0.

shuffledSubarray

Generate values corresponding to any possible sub-array of an original array.

Values of the resulting subarray are ordered randomly.

Signatures:

  • fc.shuffledSubarray(originalArray)
  • fc.shuffledSubarray(originalArray, {minLength?, maxLength?})

with:

  • originalArraythe array from which we want to extract sub-arrays
  • minLength? — default: 0minimal length (included)
  • maxLength? — default: originalArray.lengthmaximal length (included)

Usages:

fc.shuffledSubarray([1, 42, 48, 69, 75, 92]);
// Examples of generated values: [69,92], [92,69,42,75], [48,69,92,75,42,1], [1,42], [75]…

fc.shuffledSubarray([1, 42, 48, 69, 75, 92], { minLength: 5 });
// Examples of generated values: [48,1,92,69,75,42], [42,1,92,75,69], [69,75,92,48,1], [92,42,48,75,69], [1,69,75,92,42]…

fc.shuffledSubarray([1, 42, 48, 69, 75, 92], { maxLength: 5 });
// Examples of generated values: [48,1,92], [], [75,1,69,92], [42], [75,1,69,48,42]…

fc.shuffledSubarray([1, 42, 48, 69, 75, 92], { minLength: 2, maxLength: 3 });
// Examples of generated values: [1,92], [92,75], [1,48], [42,75], [48,69]…

Resources: API reference.
Available since 1.5.0.