DataWeave Mock Data Generators Library
RandomHelpers
mocks::helpers::RandomHelpers
This module condenses helper functions for Number randomization in different formats.
Index
Functions
Name | Description |
---|---|
pickRandom | Returns a random value from an array. |
randomIntWithBase | Returns a random integer between two respective base and ceiling values. If the base value is greater than the ceiling value, base is returned. |
randomIntWithLength | Returns a random integer of a certain digit length. Or 0 if the length is less than 1 . |
randomNumber | Returns a random number between two respective base and ceiling values, with the capability to specify the desired amount of decimal places. |
Functions
pickRandom ↑↑
pickRandom<T>(options: Array<T>): T
Returns a random value from an array.
Parameters
Name | Type | Description |
---|---|---|
options | Array<T> | Array of values from which to pick. |
Example
This example shows how pickRandom
behaves under an arbitrary array.
Source
%dw 2.0
import * from mocks::helpers::RandomHelpers
---
pickRandom([1, "Nacho", 3])
Output
3
pickRandom(options: Null): Null
Helper function that enables pickRandom
to work with a null
value.
randomIntWithBase ↑↑
randomIntWithBase(base: Number, max: Number): Number
Returns a random integer between two respective base and ceiling values.
If the base value is greater than the ceiling value, base
is returned.
Parameters
Name | Type | Description |
---|---|---|
base | Number | The base value. |
max | Number | The ceiling value. |
Example
This example shows how the randomIntWithBase
function behaves for two arbitrary values.
Source
%dw 2.0
output application/json
import randomIntWithBase from mocks::helpers::RandomHelpers
---
randomIntWithBase(2,10)
Output
4
randomIntWithBase(base: Null, max: Any = null): Null
Helper function that enables randomIntWithBase
to work with a null
value.
randomIntWithBase(base: Any, max: Null): Null
Helper function that enables randomIntWithBase
to work with a null
value.
randomIntWithLength ↑↑
randomIntWithLength(len: Number): Number
Returns a random integer of a certain digit length.
Or 0
if the length is less than 1
.
Parameters
Name | Type | Description |
---|---|---|
len | Number | The desired lenght for the output number. |
Example
This example shows how the randomIntWithLength
behaves under different inputs.
Source
%dw 2.0
output application/json
import * from mocks::helpers::RandomHelpers
---
randomIntWithLength(20)
Output
71020173281033233000
randomIntWithLength(len: Null): Null
Helper function that enables randomIntWithLength
to work with a null
value.
randomNumber ↑↑
randomNumber(base: Number, max: Number, precision: Number): Number
Returns a random number between two respective base and ceiling values,
with the capability to specify the desired amount of decimal places.
If base
> max
, base
is returned. If precision
is a negative number, no decimal part is added.
Parameters
Name | Type | Description |
---|---|---|
base | Number | The base value. |
max | Number | The ceiling value. |
precision | Number | The desired amount of decimal places to be returned. |
Example
This example shows how the randomNumber
behaves under different inputs.
Source
%dw 2.0
output application/json
import * from mocks::helpers::RandomHelpers
---
randomNumber(0,1,10)
Output
0.3294639395
randomNumber(base: Null, max: Any, precision: Any): Null
Helper function that enables randomNumber
to work with a null
value.
randomNumber(base: Any, max: Null, precision: Any): Null
Helper function that enables randomNumber
to work with a null
value.
randomNumber(base: Any, max: Any, precision: Null): Null
Helper function that enables randomNumber
to work with a null
value.