js-randomness-predictor
Version:
Predict Math.random output in Node, Deno, Bun, Chrome, Firefox, and Safari
6 lines (5 loc) • 2.23 kB
JavaScript
import t from"yargs";import{hideBin as s}from"yargs/helpers";import{runPredictor as c}from"./runPredictor.js";import{R as a,N as l}from"./types-ruK-I6EJ.js";import"z3-solver-jsrp";import"./exportResults.js";import"node:fs";import"node:path";import"./getCurrentNodeJsMajorVersion.js";function p(r){return`\x1B[31m${r}\x1B[0m`}function m(r){return`\x1B[33m${r}\x1B[0m`}function u(r){return`\x1B[34m${r}\x1B[0m`}class i{static info(...e){console.info(u(`[INFO] ${e.join(" ")}`))}static log(...e){console.log(`[LOG] ${e.join(" ")}`)}static warn(...e){console.warn(m(`[WARN] ${e.join(" ")}`))}static error(...e){console.error(p(`[ERROR] ${e.join(" ")}`))}}const d={command:"*",describe:"Predict future Math.random() values",builder:r=>r.option("environment",{alias:"e",describe:"Predictor environment",choices:a,demandOption:!0,type:"string"}).option("sequence",{alias:"s",describe:"Observed sequence",type:"array",coerce:e=>e.map(n=>{const o=Number(n);if(isNaN(o))throw new Error(`Invalid number in sequence: ${n}`);return o})}).option("predictions",{alias:"p",describe:"Number of predictions",type:"number",default:10,coerce:e=>{if(e<=0)throw new Error(`--predictions must be greater than 0! Got ${e}`);return e}}).option("env-version",{alias:"v",describe:"Node.js major version",type:"number",choices:l}).option("export",{alias:"x",describe:"File path to export results. Must be to a .json file. Path relative to current working directory!",type:"string"}).option("force",{alias:"f",describe:"If exporting, overwrite existing file or create needed directories in path",type:"boolean"}),handler:async r=>{try{const e=await c(r);process.env.JSRP_DRY_RUN==="1"&&(console.log(JSON.stringify(e,null,2)),process.exit(0));const n={sequence:e.sequence,predictions:e.predictions,actual:e.actual};e?.isCorrect!==void 0&&(n.isCorrect=e.isCorrect),console.log(JSON.stringify(n,null,2)),e._info&&e._info.length&&(console.log(),e._info.forEach(o=>i.info(o,`
`))),e._warnings&&e._warnings.length&&(console.log(),e._warnings.forEach(o=>i.warn(o,`
`)))}catch(e){console.log(),i.error("Something went wrong!",e?.message,`
`),process.exit(1)}}};t(s(process.argv)).scriptName("js-randomness-predictor").command(d).help().argv;