gibbon.js
Version:
Actor/Component system for use with pixi.js.
54 lines • 1.19 kB
JavaScript
/**
* Add elements of b to target when not already
* in the array.
* @param target
* @param b
*/
export const addUnique = (target, b) => {
for (let i = b.length - 1; i >= 0; i--) {
if (target.indexOf(b[i]) < 0) {
target.push(b[i]);
}
}
};
/**
* Splices an element from the array by replacing it
* with the last element.
* Array order is not preserved.
* If used in a loop, the loop must count down from the last element.
* @param {number[]} a
* @param {number} i
*/
export const quickSplice = (a, i) => {
a[i] = a[a.length - 1];
a.pop();
};
/**
* @returns Random element from an array.
*/
export const randElm = (a) => {
if (a.length > 0) {
return a[Math.floor(Math.random() * a.length)];
}
};
/**
* Removes element from array, if it exists.
*/
export const remove = (a, e) => {
const i = a.indexOf(e);
if (i >= 0) {
a.splice(i, 1);
}
};
/**
* @returns True if array contains element.
*/
export const contains = (a, e) => {
for (let i = a.length - 1; i >= 0; i--) {
if (a[i] === e) {
return true;
}
}
return false;
};
//# sourceMappingURL=array-utils.js.map