UNPKG

randomness

Version:
35 lines 1.35 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.round = exports.getData = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const fromString = (str) => str.split('').map((piece) => (piece === one ? 1 : 0)); const one = '1'; const cache = {}; const getData = (filename) => { if (cache[filename]) return cache[filename]; const buf = fs_1.default.readFileSync(path_1.default.join(__dirname, '../../resources/data', filename)); if (path_1.default.extname(filename) === '.bin') { let str = ''; for (let index = 0; index < buf.length; index++) { str += buf[index].toString(2).padStart(8, '0'); } const bits = fromString(str); cache[filename] = bits; return bits; } else if (path_1.default.extname(filename) === '.txt') { const bits = fromString(buf.toString('utf-8').trim()); cache[filename] = bits; return bits; } throw new Error('Unsupported extension'); }; exports.getData = getData; const round = (n, precision = 6) => Number(n.toFixed(precision)); exports.round = round; //# sourceMappingURL=utils.js.map