-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
mod.ts
31 lines (27 loc) · 776 Bytes
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* Ensure a function is stable, meaning the same input always produces the same output.
*
* @param testFunction - Function to be tested.
* @param count - Count is the number of times to call the `testFunction` (Default: `1000`).
* @returns Whether the output of `testFunction` was stable.
*
* @example
* ```javascript
* import { stableFunction } from "https://deno.land/x/stable_fn/mod.ts";
* stableFunction(() => true); //=> true
* ```
*/
export function stableFunction(
testFunction: () => unknown,
count = 1000,
): boolean {
const first = testFunction();
let currentValue: unknown;
for (let index = 0; index < count; index++) {
currentValue = testFunction();
if (currentValue !== first) {
return false;
}
}
return true;
}