-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add help messages when passing unknown flags
- Loading branch information
Showing
2 changed files
with
34 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
function intersection<T>(a: Set<T>, b: Set<T>): number { | ||
let intersection = 0; | ||
for (const item of a) if (b.has(item)) intersection += 1; | ||
return intersection; | ||
} | ||
|
||
function union<T>(a: Set<T>, b: Set<T>): number { | ||
let union = a.size; | ||
for (const item of b) if (!a.has(item)) union += 1; | ||
return union; | ||
} | ||
|
||
export function closest(string: string, options: Set<string>): string | null { | ||
const stringSet = new Set(string); | ||
let bestItem = null; | ||
let bestScore = 0; | ||
for (const item of options) { | ||
const itemSet = new Set(item); | ||
const score = intersection(stringSet, itemSet) / union(stringSet, itemSet); | ||
if (bestScore <= score) { | ||
bestScore = score; | ||
bestItem = item; | ||
} | ||
} | ||
return bestItem; | ||
} |