@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
31 lines (24 loc) • 1.01 kB
JavaScript
/**
* Copy data from one array to another if they are not present in the destination array
* @template T
* @param {T[]} source The source array
* @param {number} source_position Starting position where to copy from inside the source array
* @param {T[]} destination The destination array
* @param {number} destination_position Starting position where to copy to inside the destination array
* @param {number} length How many elements should be copied
* @returns {number} unique addition count
*/
export function array_copy_unique(source, source_position, destination, destination_position, length) {
let j = destination_position;
let k = 0;
for (; k < length; k++) {
const source_index = source_position + k;
const element = source[source_index];
const p = destination.indexOf(element);
if (p === -1 || p >= j) {
destination[j] = element;
j++;
}
}
return j - destination_position;
}