-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
99 lines (87 loc) · 2.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
var debug = false;
var options = {includeIndex: false, range: 1, allowRepeats:false}
function error(msg){
return new Error(`Error : ${msg}`);
}
function switchDebug(){
debug = !debug;
}
function randomNumberBetween(min, max) {
min = Math.ceil(min)
max = Math.floor(max)
var r = Math.random()
return Math.floor(r * (max - min + 1) + min)
}
function selectManyRepeatingRandomArrayItems(input, range, includeIndex){
if(!input || typeof input !== "object"){
return error("Invalid input array");
}
var output = [];
var arrayLength = input.length - 1;
for (let i = 0; i < range; i++) {
var index = randomNumberBetween(0,arrayLength);
var outputValue = input[index];
if(includeIndex){
outputValue = {key:index, value:input[index] };
}
output.push(outputValue)
}
return output;
}
function selectRandomArrayItem(input, args = undefined) {
if(!input || typeof input !== "object"){
return error("Invalid input array");
}
var {includeIndex, range, allowRepeats} = options;
if(arguments.length > 2)
{
return error("Too Many Params are Passed");
}
if(args !== undefined){
if(Number.isInteger(args)){
includeIndex = false;
range = args;
}
else if(typeof args === "boolean"){
range = 1;
includeIndex = args;
}
else if(typeof args === "object"){
range = 1;
includeIndex = false;
allowRepeats = false;
if(args.includeIndex){
includeIndex = args.includeIndex;
}
if(args.range){
range = args.range;
}
if(args.allowRepeats){
allowRepeats = args.allowRepeats;
}
}
else
{
return error("The Arguments (args) Does not Accept Value Except Boolean or Int");
}
}
if(allowRepeats){
return selectManyRepeatingRandomArrayItems(input, range, includeIndex);
}
//ToDo : performance test this for large arrays.
var result = shuffleArray([...input]).splice(0,range);
return !includeIndex ? result : {key:input.indexOf(result[0]), value: result[0]}
}
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
module.exports = {
selectRandomArrayItem,
switchDebug,
shuffleArray,
selectManyRepeatingRandomArrayItems
}