-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Include possible field, argument, type names when validation fails #355
Changes from 6 commits
03377cb
c4e619f
e25fe3e
d460b74
10997c5
8982f88
6a80c43
5a9fb1e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Copyright (c) 2015, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
import { expect } from 'chai'; | ||
import { describe, it } from 'mocha'; | ||
import { suggestionList } from '../suggestionList'; | ||
|
||
describe('suggestionList', () => { | ||
|
||
it('Returns results when input is empty', () => { | ||
expect(suggestionList('', [ 'a' ])).to.deep.equal([ 'a' ]); | ||
}); | ||
|
||
it('Returns empty array when there are no options', () => { | ||
expect(suggestionList('input', [])).to.deep.equal([]); | ||
}); | ||
|
||
it('Returns options sorted based on simularity', () => { | ||
expect(suggestionList('abc', [ 'a', 'ab', 'abc' ])) | ||
.to.deep.equal([ 'abc', 'ab' ]); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* @flow */ | ||
/** | ||
* Copyright (c) 2015, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
/** | ||
* Given an invalid input string and a list of valid options, returns a filtered | ||
* list of valid options sorted based on their simularity with the input. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. simularity -> similarity |
||
*/ | ||
export function suggestionList( | ||
input: string, | ||
options: Array<string> | ||
): Array<string> { | ||
let i; | ||
const d = {}; | ||
const oLength = options.length; | ||
const inputThreshold = input.length / 2; | ||
for (i = 0; i < oLength; i++) { | ||
const distance = lexicalDistance(input, options[i]); | ||
const threshold = Math.max(inputThreshold, options[i].length / 2, 1); | ||
if (distance <= threshold) { | ||
d[options[i]] = distance; | ||
} | ||
} | ||
const result = Object.keys(d); | ||
return result.sort((a , b) => d[a] - d[b]); | ||
} | ||
|
||
/** | ||
* Computes the lexical distance between strings A and B. | ||
* | ||
* The "distance" between two strings is given by counting the minimum number | ||
* of edits needed to transform string A into string B. An edit can be an | ||
* insertion, deletion, or substitution of a single character, or a swap of two | ||
* adjacent characters. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be worth mentioning "Levenshtein" somewhere in here. (Also, did you consider using Damerau–Levenshtein, which considers transpositions as well?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is copied pasta from https://github.com/graphql/codemirror-graphql/blob/master/src/utils/hintList.js#L40 I did not put too much though on which specific algorithm to use. I just found the one that will get us most of the way there. Improvements can always be made later when we understand the pitfalls of this one better. Looking at the wikipedia page for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, no! I missed the word "swap" in there. It is exactly transposition. So this probably is Damerau–Levenshtein already. |
||
* | ||
* This distance can be useful for detecting typos in input or sorting | ||
* | ||
* @param {string} a | ||
* @param {string} b | ||
* @return {int} distance in number of edits | ||
*/ | ||
function lexicalDistance(a, b) { | ||
let i; | ||
let j; | ||
const d = []; | ||
const aLength = a.length; | ||
const bLength = b.length; | ||
|
||
for (i = 0; i <= aLength; i++) { | ||
d[i] = [ i ]; | ||
} | ||
|
||
for (j = 1; j <= bLength; j++) { | ||
d[0][j] = j; | ||
} | ||
|
||
for (i = 1; i <= aLength; i++) { | ||
for (j = 1; j <= bLength; j++) { | ||
const cost = a[i - 1] === b[j - 1] ? 0 : 1; | ||
|
||
d[i][j] = Math.min( | ||
d[i - 1][j] + 1, | ||
d[i][j - 1] + 1, | ||
d[i - 1][j - 1] + cost | ||
); | ||
|
||
if (i > 1 && j > 1 && | ||
a[i - 1] === b[j - 2] && | ||
a[i - 2] === b[j - 1]) { | ||
d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + cost); | ||
} | ||
} | ||
} | ||
|
||
return d[aLength][bLength]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/simularity/similarity/