Skip to content

Instantly share code, notes, and snippets.

@dubzzz
Created July 2, 2020 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dubzzz/2378c3df135392365a1effbdb376d0a7 to your computer and use it in GitHub Desktop.
Save dubzzz/2378c3df135392365a1effbdb376d0a7 to your computer and use it in GitHub Desktop.
miniFc.array = (itemGenerator) => {
return {
generate(mrng) {
const size = mrng.next(0, 10);
const content = [];
for (let index = 0 ; index !== size ; ++index) {
content.push(itemGenerator.generate(mrng));
}
return content;
},
*shrink(value) {
// No shrink on empty arrays
if (value.length === 0) {
return;
}
// Step 1. Shrink on size first by keeping last items
let removedSize = Math.floor(value.length / 2);
while (removedSize > 0) {
yield value.slice(removedSize);
removedSize = Math.floor(removedSize / 2);
}
// Step 2. Shrink the first item alone
for (const shrunkItemValue of itemGenerator.shrink(value[0])) {
yield [shrunkItemValue, ...value.slice(1)];
}
// Step 3. Keep first item untouched
for (const shrunkValue of this.shrink(value.slice(1))) {
yield [value[0], ...shrunkValue];
}
}
}
}
// You can check the output by calling:
// > [...miniFc.array(miniFc.integer(0, 100)).shrink([4, 1, 2, 1, 3])]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment