Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add @babel/helper-validator-option (#12006)
* refactor: add @babel/helper-validator-option * refactor: simplify validateTopLevelOptions * perf: the recursive version is not practically fast * Update packages/babel-helper-validator-option/README.md Co-authored-by: Brian Ng <bng412@gmail.com> * Update packages/babel-helper-validator-option/src/validator.js * fix: incorrect type annotation * refactor: use babel/helper-option-validator in babel/compat-data * chore: fix flow types error * Address review comments * address review comments Co-authored-by: Brian Ng <bng412@gmail.com>
- Loading branch information
1 parent
0d32e3f
commit f2da186
Showing
19 changed files
with
375 additions
and
197 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
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
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,3 @@ | ||
src | ||
test | ||
*.log |
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,19 @@ | ||
# @babel/helper-validator-option | ||
|
||
> Validate plugin/preset options | ||
See our website [@babel/helper-validator-option](https://babeljs.io/docs/en/next/babel-helper-validator-option.html) for more information. | ||
|
||
## Install | ||
|
||
Using npm: | ||
|
||
```sh | ||
npm install --save-dev @babel/helper-validator-option | ||
``` | ||
|
||
or using yarn: | ||
|
||
```sh | ||
yarn add @babel/helper-validator-option --dev | ||
``` |
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,16 @@ | ||
{ | ||
"name": "@babel/helper-validator-option", | ||
"version": "7.11.4", | ||
"description": "Validate plugin/preset options", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/babel/babel.git", | ||
"directory": "packages/babel-helper-validator-option" | ||
}, | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"main": "./lib/index.js", | ||
"exports": "./lib/index.js" | ||
} |
50 changes: 50 additions & 0 deletions
50
packages/babel-helper-validator-option/src/find-suggestion.js
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,50 @@ | ||
// @flow | ||
|
||
const { min } = Math; | ||
|
||
// a minimal leven distance implementation | ||
// balanced maintenability with code size | ||
// It is not blazingly fast but should be okay for Babel user case | ||
// where it will be run for at most tens of time on strings | ||
// that have less than 20 ASCII characters | ||
|
||
// https://rosettacode.org/wiki/Levenshtein_distance#ES5 | ||
function levenshtein(a, b) { | ||
let t = [], | ||
u = [], | ||
i, | ||
j; | ||
const m = a.length, | ||
n = b.length; | ||
if (!m) { | ||
return n; | ||
} | ||
if (!n) { | ||
return m; | ||
} | ||
for (j = 0; j <= n; j++) { | ||
t[j] = j; | ||
} | ||
for (i = 1; i <= m; i++) { | ||
for (u = [i], j = 1; j <= n; j++) { | ||
u[j] = | ||
a[i - 1] === b[j - 1] ? t[j - 1] : min(t[j - 1], t[j], u[j - 1]) + 1; | ||
} | ||
t = u; | ||
} | ||
return u[n]; | ||
} | ||
|
||
/** | ||
* Given a string `str` and an array of candidates `arr`, | ||
* return the first of elements in candidates that has minimal | ||
* Levenshtein distance with `str`. | ||
* @export | ||
* @param {string} str | ||
* @param {string[]} arr | ||
* @returns {string} | ||
*/ | ||
export function findSuggestion(str: string, arr: string[]): string { | ||
const distances = arr.map<number>(el => levenshtein(el, str)); | ||
return arr[distances.indexOf(min(...distances))]; | ||
} |
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,2 @@ | ||
export { OptionValidator } from "./validator"; | ||
export { findSuggestion } from "./find-suggestion"; |
Oops, something went wrong.