- 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
import {random as mathRandom, weightedRandom as mathWeightedRandom} from "./../math/math.js";
/**
* a collection of array utility functions
* @namespace utils.array
*/
/**
* Remove the specified object from the given Array
* @public
* @memberof utils.array
* @name remove
* @param {Array.<number|string|Object>} arr - array from which to remove an object
* @param {object} obj - to be removed
* @returns {Array.<number|string|Object>} the modified Array
* let arr = [ "foo", "bar", "baz" ];
* // remove "foo" from the array
* me.utils.array.remove(arr, "foo");
*/
export function remove(arr, obj) {
let i = Array.prototype.indexOf.call(arr, obj);
if (i !== -1) {
Array.prototype.splice.call(arr, i, 1);
}
return arr;
}
/**
* return a random array element
* @public
* @memberof utils.array
* @name random
* @param {Array.<number|string|Object>} arr - array to pick a element
* @returns {any} random member of array
* @example
* // Select a random array element
* let arr = [ "foo", "bar", "baz" ];
* console.log(me.utils.array.random(arr));
*/
export function random(arr) {
return arr[mathRandom(0, arr.length)];
}
/**
* return a weighted random array element, favoring the earlier entries
* @public
* @memberof utils.array
* @name weightedRandom
* @param {Array.<number|string|Object>} arr - array to pick a element
* @returns {any} random member of array
*/
export function weightedRandom(arr) {
return arr[mathWeightedRandom(0, arr.length)];
}