Created
July 2, 2020 18:02
-
-
Save dubzzz/2378c3df135392365a1effbdb376d0a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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