MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / longestCommonPrefix

Function longestCommonPrefix

source code/utils/bash/prefix.ts:184–206  ·  view source on GitHub ↗

* Compute the longest common prefix of strings, aligned to word boundaries. * e.g. ["git fetch", "git worktree"] → "git" * ["npm run test", "npm run lint"] → "npm run"

(strings: string[])

Source from the content-addressed store, hash-verified

182 * ["npm run test", "npm run lint"] → "npm run"
183 */
184function longestCommonPrefix(strings: string[]): string {
185 if (strings.length === 0) return ''
186 if (strings.length === 1) return strings[0]!
187
188 const first = strings[0]!
189 const words = first.split(' ')
190 let commonWords = words.length
191
192 for (let i = 1; i < strings.length; i++) {
193 const otherWords = strings[i]!.split(' ')
194 let shared = 0
195 while (
196 shared < commonWords &&
197 shared < otherWords.length &&
198 words[shared] === otherWords[shared]
199 ) {
200 shared++
201 }
202 commonWords = shared
203 }
204
205 return words.slice(0, Math.max(1, commonWords)).join(' ')
206}

Callers 1

Calls 1

maxMethod · 0.80

Tested by

no test coverage detected