Generator functions to test variants

by Arnaud Buchholz

JavaScript

function * invalidVariants(template) {
  const members = Object.keys(template);
  for (const member of members) {
    const missing = { ...template };
    delete missing[member];
    yield missing;
    const type = typeof template[member];
    if (type === 'string') {
      const invalidType = { ...template };
      invalidType[member] = 1;
      yield invalidType;
      invalidType[member] = 0;
      yield invalidType;
    } else {
      const invalidType = { ...template };
      invalidType[member] = 'not_a_valid_string';
      yield invalidType;
      invalidType[member] = '';
      yield invalidType;
    }
  }
}

const variants = [...invalidVariants({
  hello: 'world',
  count: 0
})];

console.log(variants);