Skip to content

Instantly share code, notes, and snippets.

@matthewharwood
Created July 19, 2019 14:58
Show Gist options
  • Save matthewharwood/1aec2d1d18681539713f5bebd40f5ee1 to your computer and use it in GitHub Desktop.
Save matthewharwood/1aec2d1d18681539713f5bebd40f5ee1 to your computer and use it in GitHub Desktop.
fixture generator
// Utils
function * typeGenerator (iterable) {
yield * iterable;
}
function * composeGenerator (...iterables) {
for (const iterable of iterables) yield * iterable();
}
function runGenerator(generator) {
const [key] = Object.keys(generator);
if(key) {
for(const g of generator[key]()) {
console.log({[key]: g});
}
}
else {
for(const g of generator()) {
console.log(g);
}
}
}
const makeObject = (key, gen) => ({[key]: gen});
// Primatives
const bool = () => typeGenerator([true, false]);
const str = () => typeGenerator([
'',
'Lorem',
]);
const obj = () => typeGenerator([
{},
])
// Composed Types
const strObj = () => composeGenerator(str, obj)
const boolObj = () => composeGenerator(bool, obj)
// Complex Types
const Str = (key) => makeObject(key, strObj);
const Bool = (key) => makeObject(key, boolObj);
// Tests
console.log('*********** strObj ***********');
runGenerator(strObj)
console.log('*********** boolObj ***********');
runGenerator(boolObj)
console.log('*********** Str("name") ***********');
runGenerator(Str('name'));
console.log('*********** Bool("active") ***********');
runGenerator(Bool('active'));
const Obj = (key, children) => {
return makeObject(key, Object.assign(...children))
}
console.log(Obj('hello', [Str('name'), Bool('active')]))
/**
* hello: { {} | {hello: {name: {}}} | {hello: {name: {}, active:{}}}
* name: '' | undefined | 'Lorem' |'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'|'😋😋😋😋'
* active: true | false
* }
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment