Update rand.js

This commit is contained in:
Namhyeon Go 2022-06-22 22:03:20 +09:00 committed by GitHub
parent 6f90f2f3d2
commit f3a34f560d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,7 @@
// rand.js
var FILE = require("lib/file");
function randomize() {
return Math.random();
}
@ -75,6 +77,26 @@ function sample(arr, length) {
return shuffle(arr).slice(0, length);
}
function discardCallback(callback, filename) {
if (typeof callback !== "function") return;
var discarded = splitLn(FILE.readFile(filename, "utf-8"));
var chosen = callback();
while (chosen == null || discarded.indexOf(chosen) > -1) {
console.log("Chosen other item.");
chosen = callback(arr);
}
FILE.appendFile(filename, "\r\n" + chosen, "utf-8");
return chosen;
}
function discardOne(arr, filename) {
return discardCallback(one, filename);
}
exports.randomize = randomize;
exports.getInt = getInt;
exports.one = one;
@ -83,7 +105,9 @@ exports.uuidv4 = uuidv4;
exports.shuffle = shuffle;
exports.rotate = rotate;
exports.sample = sample;
exports.discardOne = discardOne;
exports.discardCallback = discardCallback;
exports.VERSIONINFO = "Random Module (rand.js) version 0.4";
exports.VERSIONINFO = "Random Module (rand.js) version 0.5";
exports.global = global;
exports.require = global.require;