agentsqripts
Version:
Comprehensive static code analysis toolkit for identifying technical debt, security vulnerabilities, performance issues, and code quality problems
25 lines (20 loc) • 690 B
JavaScript
/**
* @file Find common prefix in strings
* @description Single responsibility: Extract shared prefix from string array
*/
function findCommonPrefix(strings) {
if (!strings || strings.length === 0) return '';
if (strings.length === 1) return strings[0];
// Sort to make comparison easier
const sorted = strings.slice().sort();
const first = sorted[0];
const last = sorted[sorted.length - 1];
let i = 0;
while (i < first.length && first[i] === last[i]) {
i++;
}
// Return common prefix, but only if it's meaningful (at least 3 chars)
const prefix = first.substring(0, i);
return prefix.length >= 3 ? prefix : '';
}
module.exports = findCommonPrefix;