js-randomness-predictor
Version:
Predict Math.random output in Node, Chrome, and Firefox
63 lines (62 loc) • 2.06 kB
JavaScript
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { runPredictor } from "./runPredictor.js";
import { PREDICTOR_ENVIRONMENTS } from "./types.js";
const predictCommand = {
command: "*",
describe: "Predict future Math.random() values",
builder: (yargs) => {
return yargs
.option("environment", {
alias: "e",
describe: "Predictor environment",
choices: PREDICTOR_ENVIRONMENTS,
demandOption: true,
type: "string",
})
.option("sequence", {
alias: "s",
describe: "Observed sequence of numbers, separated by space (required for firefox/chrome)",
type: "array",
coerce: (arr) => {
return arr.map((v) => {
const n = Number(v);
if (isNaN(n)) {
throw new Error(`Invalid number in sequence: ${v}`);
}
return n;
});
},
})
.option("predictions", {
alias: "p",
describe: "Number of predictions to generate",
type: "number",
default: 10,
})
.check((argv) => {
if (argv.environment !== "v8" && (!argv.sequence || argv.sequence.length === 0)) {
throw new Error("The --sequence (-s) option is required when using firefox or chrome.");
}
return true;
});
},
handler: async (argv) => {
try {
const result = await runPredictor(argv);
console.log(JSON.stringify(result, null, 2));
}
catch (err) {
console.error(err.message);
process.exit(1);
}
},
};
// prettier-ignore
yargs(hideBin(process.argv))
.scriptName("js-randomness-predictor")
.usage("Usage:\n$0 --environment <env> [--sequence <numbers...>] [--predictions <count>]")
.command(predictCommand)
.help()
.argv;