-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26289 from winchesHe/feature-scrips-diff
Build: Add build-package invalid package name tips
- Loading branch information
Showing
4 changed files
with
195 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { diff } from 'diff-match-patch-es'; | ||
|
||
function matchTextScore(text: string, pattern: string) { | ||
const result = diff(text, pattern); | ||
|
||
if (!result.length) { | ||
return 0; | ||
} | ||
|
||
return result.reduce((pre, cur) => { | ||
const [matchIndex, matchText] = cur; | ||
|
||
if (matchIndex === 0) { | ||
return pre + matchText.length; | ||
} | ||
|
||
return pre; | ||
}, 0); | ||
} | ||
|
||
export function findMostMatchText(list: string[], pattern: string) { | ||
let maxScore = 0; | ||
let result = ''; | ||
|
||
for (const text of list) { | ||
const score = matchTextScore(text, pattern); | ||
|
||
if (score > maxScore) { | ||
maxScore = score; | ||
result = text; | ||
} | ||
} | ||
|
||
return result !== '' ? result : null; | ||
} |
Oops, something went wrong.