@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
45 lines (30 loc) • 905 B
JavaScript
/**
* @example: ['abra', 'abc', 'abode'] => 'ab'
* @param {String[]} strings
* @returns string
*/
export function string_compute_common_prefix(strings) {
let i, j;
const numInputs = strings.length;
let result = "";
if (numInputs === 0) {
return result;
}
const firstString = strings[0];
let lengthLimit = firstString.length;
for (i = 1; i < numInputs; i++) {
lengthLimit = Math.min(strings[i].length, lengthLimit);
}
main_loop:for (i = 0; i < lengthLimit; i++) {
const letter0 = firstString.charAt(i);
for (j = 1; j < numInputs; j++) {
const string = strings[j];
const letter1 = string.charAt(i);
if (letter0 !== letter1) {
break main_loop;
}
}
result += letter0;
}
return result;
}