mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-03-12 00:45:14 +00:00
Update rand.js
This commit is contained in:
parent
6f90f2f3d2
commit
f3a34f560d
26
lib/rand.js
26
lib/rand.js
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue
Block a user