Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove distance method and use std/text #681

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions _utils/distance.ts

This file was deleted.

17 changes: 2 additions & 15 deletions flags/_utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { FlagOptions } from "./types.ts";
import { distance } from "../_utils/distance.ts";
import { closestString } from "./deps.ts";

/** Convert param case string to camel case. */
export function paramCaseToCamelCase(str: string): string {
Expand Down Expand Up @@ -63,7 +63,7 @@ export function didYouMean(
type: string,
types: Array<string>,
): string {
const match: string | undefined = closest(type, types);
const match: string | undefined = closestString(type, types);
return match ? `${message} "${match}"?` : "";
}

Expand Down Expand Up @@ -123,19 +123,6 @@ function matchWildCardOption(
return option;
}

function closest(str: string, arr: string[]): string | undefined {
let minDistance = Infinity;
let minIndex = 0;
for (let i = 0; i < arr.length; i++) {
const dist = distance(str, arr[i]);
if (dist < minDistance) {
minDistance = dist;
minIndex = i;
}
}
return arr[minIndex];
}

export function getDefaultValue(option: FlagOptions): unknown {
return typeof option.default === "function"
? option.default()
Expand Down
1 change: 1 addition & 0 deletions flags/deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { closestString } from "https://deno.land/std@0.216.0/text/closest_string.ts";
12 changes: 9 additions & 3 deletions prompt/_generic_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ import {
GenericInputPromptSettings,
} from "./_generic_input.ts";
import { WidenType } from "./_utils.ts";
import { bold, brightBlue, dim, stripColor, yellow } from "./deps.ts";
import {
bold,
brightBlue,
dim,
levenshteinDistance,
stripColor,
yellow,
} from "./deps.ts";
import { Figures, getFiguresByKeys } from "./_figures.ts";
import { distance } from "../_utils/distance.ts";

type UnsupportedInputOptions = "suggestions" | "list";

Expand Down Expand Up @@ -802,7 +808,7 @@ function matchOptions<
if (matchOption(searchInput, option)) {
matched.push({
option,
distance: distance(option.name, searchInput),
distance: levenshteinDistance(option.name, searchInput),
children: [],
});
}
Expand Down
6 changes: 3 additions & 3 deletions prompt/_generic_suggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import {
dim,
dirname,
join,
levenshteinDistance,
normalize,
stripColor,
underline,
} from "./deps.ts";
import { Figures, getFiguresByKeys } from "./_figures.ts";
import { distance } from "../_utils/distance.ts";

/** Generic input prompt options. */
export interface GenericSuggestionsOptions<TValue, TRawValue>
Expand Down Expand Up @@ -246,8 +246,8 @@ export abstract class GenericSuggestions<TValue, TRawValue>
.startsWith(input.toLowerCase())
)
.sort((a: string | number, b: string | number) =>
distance((a || a).toString(), input) -
distance((b || b).toString(), input)
levenshteinDistance((a || a).toString(), input) -
levenshteinDistance((b || b).toString(), input)
);
}

Expand Down
1 change: 1 addition & 0 deletions prompt/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ export type {
Writer,
WriterSync,
} from "https://deno.land/std@0.216.0/io/types.ts";
export { levenshteinDistance } from "https://deno.land/std@0.216.0/text/levenshtein_distance.ts";
Loading