Add ignoreCase option
MODIFY
packages/@romejs/consume/Consumer.ts
packages/@romejs/consume/Consumer.ts
@@ -537,9 +537,7 @@ export default class Consumer {
target: 'key',
at: 'suffix',
atParent: true,
- advice: buildSuggestionAdvice({
- value: key,
- items: knownProperties,
+ advice: buildSuggestionAdvice(key, knownProperties, {
ignoreCase: true,
}),
});

MODIFY
packages/@romejs/core/master/dependencies/DependencyNode.ts
packages/@romejs/core/master/dependencies/DependencyNode.ts
@@ -265,11 +265,8 @@ export default class DependencyNode {
const exportedNames = resolved.node.getExportedNames(kind);
advice = advice.concat(
- buildSuggestionAdvice(
- resolved.name,
- Array.from(exportedNames),
- 0,
- name => {
+ buildSuggestionAdvice(resolved.name, Array.from(exportedNames), {
+ formatItem: name => {
const exportInfo = resolved.node.resolveImport(name, undefined);
if (exportInfo.type === 'NOT_FOUND') {
@@ -291,7 +288,7 @@ export default class DependencyNode {
return name;
},
- ),
+ }),
);
}

MODIFY
packages/@romejs/core/master/fs/resolverSuggest.ts
packages/@romejs/core/master/fs/resolverSuggest.ts
@@ -185,11 +185,9 @@ export default function resolverSuggest(
advice = [
...advice,
- ...buildSuggestionAdvice(
- query.source.join(),
- relativeSuggestions,
- 0,
- relative => {
+ ...buildSuggestionAdvice(query.source.join(), relativeSuggestions, {
+ minRating: 0,
+ formatItem: relative => {
const absolute = relativeToAbsolute.get(relative);
if (absolute === undefined) {
throw new Error('Should be valid');
@@ -197,7 +195,7 @@ export default function resolverSuggest(
return `<filelink target="${absolute}">${relative}</filelink>`;
},
- ),
+ }),
];
}
}
@@ -315,10 +313,12 @@ function tryPathSuggestions(
const ratings = orderBySimilarity(
path.getExtensionlessBasename(),
entries,
- MIN_SIMILARITY,
- target => {
+ {
+ minRating: MIN_SIMILARITY,
+ formatItem: target => {
return createUnknownFilePath(target).getExtensionlessBasename();
},
+ },
);
for (const rating of ratings) {
@@ -361,10 +361,13 @@ function getPackageSuggestions(
// TODO Add node_modules
- const matches: Array<[string, string]> = orderBySimilarity(
+ const matches: Array<[
+ string,
+ string,
+ ]> = orderBySimilarity(
query.source.join(),
Array.from(possibleGlobalPackages.keys()),
- MIN_SIMILARITY,
+ {minRating: MIN_SIMILARITY},
).map(item => {
const name = item.target;

MODIFY
packages/@romejs/diagnostics/helpers.ts
packages/@romejs/diagnostics/helpers.ts
@@ -13,28 +13,22 @@ import {get1} from '@romejs/ob1';
import {NEWLINE} from '@romejs/js-parser-utils';
type BuildSuggestionAdviceOptions = {
- value: string;
- items: Array<string>;
minRating?: number;
ignoreCase?: boolean;
formatItem?: (item: string) => string;
};
-export function buildSuggestionAdvice({
- value,
- items,
- minRating = 0.5,
- ignoreCase,
- formatItem,
-}: BuildSuggestionAdviceOptions): PartialDiagnosticAdvice {
+export function buildSuggestionAdvice(
+ value: string,
+ items: Array<string>,
+ {minRating = 0.5, ignoreCase, formatItem}: BuildSuggestionAdviceOptions = {},
+): PartialDiagnosticAdvice {
const advice: PartialDiagnosticAdvice = [];
- const ratings = orderBySimilarity(
- value,
- items,
+ const ratings = orderBySimilarity(value, items, {
minRating,
formatItem,
ignoreCase,
- );
+ });
const strings = ratings.map(item => {
const {target} = item;

MODIFY
packages/@romejs/string-utils/orderBySimilarity.test.ts
packages/@romejs/string-utils/orderBySimilarity.test.ts
@@ -76,15 +76,31 @@ test('orderBySimilarity', t => {
]);
t.looksLike(
- orderBySimilarity('french', ['quebec', '123', 'france', 'french'], 0.5),
+ orderBySimilarity('french', ['quebec', '123', 'france', 'french'], {
+ minRating: 0.5,
+ }),
[{target: 'french', rating: 1}],
);
t.looksLike(
- orderBySimilarity('iphone', ['ipod', 'iphone 5s', 'iphone x'], 0.5),
+ orderBySimilarity('iphone', ['ipod', 'iphone 5s', 'iphone x', 'IPHONE'], {
+ minRating: 0.5,
+ }),
[
{target: 'iphone x', rating: 0.9090909090909091},
{target: 'iphone 5s', rating: 0.8333333333333334},
],
);
+
+ t.looksLike(
+ orderBySimilarity('iphone', ['IPHONE', 'iphone 5s', 'iphone x'], {
+ minRating: 0.5,
+ ignoreCase: true,
+ }),
+ [
+ {target: 'IPHONE', rating: 1},
+ {target: 'iphone x', rating: 0.9090909090909091},
+ {target: 'iphone 5s', rating: 0.8333333333333334},
+ ],
+ );
});

MODIFY
packages/@romejs/string-utils/orderBySimilarity.ts
packages/@romejs/string-utils/orderBySimilarity.ts
@@ -96,12 +96,15 @@ export type Rating = {
export type Ratings = Array<Rating>;
+type OrderBySimilarityOptions = {
+ minRating?: number;
+ formatItem?: (str: string) => string;
+ ignoreCase?: boolean;
+};
export function orderBySimilarity(
compareStr: string,
targets: Array<string>,
- minRating?: number,
- strMap?: (str: string) => string,
- ignoreCase?: boolean = false,
+ {minRating, formatItem, ignoreCase = false}: OrderBySimilarityOptions = {},
): Ratings {
if (targets.length === 0) {
return [];
@@ -112,8 +115,8 @@ export function orderBySimilarity(
targets,
(target: string): Rating => {
let compareTarget = target;
- if (strMap !== undefined) {
- compareTarget = strMap(target);
+ if (formatItem !== undefined) {
+ compareTarget = formatItem(target);
}
if (ignoreCase) {

Add ignoreCase option