This file contains hidden or 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.tuple = (...itemGenerators) => { | |
| return { | |
| generate(mrng) { | |
| return itemGenerators.map(g => g.generate(mrng)); | |
| } | |
| }; | |
| } | |
| // It can be used as follow: | |
| // > miniFc.tuple(miniFc.integer(0, 50), miniFc.boolean()).generate(mrng) |
This file contains hidden or 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.boolean = () => map( | |
| miniFc.integer(0, 1), | |
| Boolean | |
| ) | |
| miniFc.character = () => map( | |
| miniFc.integer(0, 25), | |
| n => String.fromCharCode(97 + n) | |
| ) |
This file contains hidden or 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
| function map(g, mapper) { | |
| return { | |
| generate(mrng) { | |
| const value = g.generate(mrng); | |
| return mapper(value); | |
| } | |
| }; | |
| } |
This file contains hidden or 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
| declare function map<T, U>(g: Generator<T>, mapper: (v: T) => U): Generator<U>; |
This file contains hidden or 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
| // const miniFc = {} | |
| miniFc.integer = (min, max) => { | |
| return { | |
| generate(mrng) { | |
| return mrng.next(min, max); | |
| } | |
| }; | |
| } | |
| // It can be used as follow: |
This file contains hidden or 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
| class Random { | |
| constructor(rng) { | |
| this.rng = rng; | |
| } | |
| next(min, max) { | |
| const g = prand.uniformIntDistribution(min, max, this.rng); | |
| this.rng = g[1]; | |
| return g[0]; | |
| } | |
| } |
This file contains hidden or 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
| type Generator<T> = { | |
| generate(mrng: Random): T; | |
| } |
This file contains hidden or 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
| // Log everything | |
| function logAllCalls(ClassType) { | |
| for (const k in ClassType.prototype) { | |
| try { | |
| if (typeof ClassType.prototype[k] !== "function") { | |
| continue; | |
| } | |
| } | |
| catch(err) { |
This file contains hidden or 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
| type EnableIf<T, U, ErrorMessage extends string = never> = T extends true ? U : ErrorMessage; | |
| type Not<T> = T extends true ? false : true; | |
| type And<T, U, V = true, W = true, X = true> = T extends true ? U extends true ? V extends true ? W extends true ? X extends true ? true : false : false : false : false : false; | |
| type Or<T, U> = T extends true ? true : U extends true ? true : false; | |
| type Extends<T, U> = T extends U ? true : false; | |
| type IsPrimitive<T> = T extends object ? false : true; | |
| type IsNever<T> = [T] extends [never] ? true : false |
This file contains hidden or 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
| type EnableIf<T, U, ErrorMessage extends string = never> = T extends true ? U : ErrorMessage; | |
| type IsArrayOf<T, U> = T extends U[] ? true : false; | |
| function enableIf<T, U>(t: T, u: EnableIf<IsArrayOf<T, U>, U, 'T has to be an array of U'>) { | |
| // Code | |
| } | |
| enableIf([1, 2], 3); | |
| enableIf([1, 2], '3'); |