1.0.0 • Published 3 years ago
ranture v1.0.0
Ranture is a library to create custom structured mock data in seconds. When I made mock data, biggest difficulty was about random lengthed arrays. Ranture can set up a random iterations with one sentence, hierarchical structures, nested arrays, etc. It uses property names as arguments to have a cleaner syntax:
import { ranture, random, RantureArray } from "./ranture.js"
let pets = ['cat', 'dog', 'fish']
let places = ['europe', 'asia', 'america', 'africa', 'australia', 'antarctica']
pets = new RantureArray(...pets)
places = new RantureArray(...places)
let mock = ranture({
"pets from 7 to 22 step 7 salt 5 ": lifespan => ({
pet: pets.single,
lifespan,
found: places.min(1).max(4)
})
})
console.log(JSON.stringify(mock, 0, 2))Pets with lifespans from little to much.
The first word before space is actual property's name.
Followed by key value fashioned arguments:
Loop Keywords
fromiterator starts from thisminiterator starts from a random number greater than (>=) thistoiterator goes up to thismaxiterator goes up to a random number less than (<=) thissaltmaximum ± random number to add to the iterator every runstepevery iteration iterator increased by this value
RantureArray
.singlereturns a random element.shuffledreturns its shuflled version.repeatablereturns its shuffled version that can has duplicates.sample(n)returns a sample from it withnnumbers of elements.min(n)returns random sample with length greater than n.max(n)returns random sample with length less than n
To use .min(a) and .max(b) both, you should write them in .min(a).max(b)
fashion since .max(b) may return an array with length lower than a but
.min(a) leaves a mark for .max(b) if they're in row.
Result of the code snippet above (every run will probably be different)
{
"pets": [
{
"pet": "cat",
"lifespan": 8,
"found": [
"asia"
]
},
{
"pet": "dog",
"lifespan": 17,
"found": [
"africa"
]
},
{
"pet": "dog",
"lifespan": 22,
"found": [
"australia",
"europe",
]
}
]
}1.0.0
3 years ago