Skip to content

Commit

Permalink
Replace custom "fundSuggestion" function with "levenary" (#10924)
Browse files Browse the repository at this point in the history
* Replace custom "fundSuggestion" function with "levenary"

* Update
  • Loading branch information
nicolo-ribaudo committed Dec 25, 2019
1 parent db3c31a commit e1b01cd
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 38 deletions.
6 changes: 0 additions & 6 deletions lib/third-party-libs.js.flow
Expand Up @@ -190,12 +190,6 @@ declare module "convert-source-map" {
};
}

declare module "js-levenshtein" {
declare module.exports: {
(string, string): number,
};
}

declare module "core-js-compat/data" {
declare type Target = "node" | "chrome" | "opera" | "edge" | "firefox" | "safari" | "ie" | "ios" | "android" | "electron" | "samsung";

Expand Down
2 changes: 1 addition & 1 deletion packages/babel-preset-env/package.json
Expand Up @@ -63,7 +63,7 @@
"browserslist": "^4.6.0",
"core-js-compat": "^3.6.0",
"invariant": "^2.2.2",
"js-levenshtein": "^1.1.3",
"levenary": "^1.1.0",
"semver": "^5.5.0"
},
"peerDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-preset-env/src/normalize-options.js
Expand Up @@ -8,7 +8,7 @@ import moduleTransformations from "./module-transformations";
import { TopLevelOptions, ModulesOption, UseBuiltInsOption } from "./options";
import { defaultWebIncludes } from "./polyfills/corejs2/get-platform-specific-default";
import { isBrowsersQueryValid } from "./targets-parser";
import { findSuggestion } from "./utils";
import findSuggestion from "levenary";

import type {
BuiltInsOption,
Expand Down
8 changes: 2 additions & 6 deletions packages/babel-preset-env/src/targets-parser.js
Expand Up @@ -3,12 +3,8 @@
import browserslist from "browserslist";
import invariant from "invariant";
import semver from "semver";
import {
semverify,
isUnreleasedVersion,
getLowestUnreleased,
findSuggestion,
} from "./utils";
import findSuggestion from "levenary";
import { semverify, isUnreleasedVersion, getLowestUnreleased } from "./utils";
import browserModulesData from "../data/built-in-modules.json";
import { TargetNames } from "./options";
import type { Targets } from "./types";
Expand Down
13 changes: 0 additions & 13 deletions packages/babel-preset-env/src/utils.js
Expand Up @@ -4,7 +4,6 @@ import * as t from "@babel/types";
import type { NodePath } from "@babel/traverse";
import invariant from "invariant";
import semver from "semver";
import levenshtein from "js-levenshtein";
import { addSideEffect } from "@babel/helper-module-imports";
import unreleasedLabels from "../data/unreleased-labels";
import { semverMin } from "./targets-parser";
Expand Down Expand Up @@ -53,18 +52,6 @@ export function intersection<T>(
return result;
}

export function findSuggestion(options: string[], option: string): string {
let levenshteinValue = Infinity;
return options.reduce((suggestion, validOption) => {
const value = levenshtein(validOption, option);
if (value < levenshteinValue) {
levenshteinValue = value;
return validOption;
}
return suggestion;
}, "");
}

export function prettifyVersion(version: string) {
if (typeof version !== "string") {
return version;
Expand Down
12 changes: 1 addition & 11 deletions packages/babel-preset-env/test/utils.spec.js
Expand Up @@ -2,7 +2,7 @@

const utils = require("../lib/utils");

const { prettifyTargets, prettifyVersion, semverify, findSuggestion } = utils;
const { prettifyTargets, prettifyVersion, semverify } = utils;

describe("utils", () => {
describe("semverify", () => {
Expand Down Expand Up @@ -50,14 +50,4 @@ describe("utils", () => {
});
});
});

describe("findSuggestion", () => {
it("returns", () => {
const options = ["one", "two", "three"];
expect(findSuggestion(options, "onr")).toEqual("one");
expect(findSuggestion(options, "tree")).toEqual("three");
expect(findSuggestion(options, "")).toEqual("one");
expect(findSuggestion(options, "xxx")).toEqual("one");
});
});
});

0 comments on commit e1b01cd

Please sign in to comment.