Skip to content

Commit

Permalink
suggestionList: convert strings to arrays before calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Feb 18, 2020
1 parent 4c10844 commit 268888b
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/jsutils/suggestionList.js
Expand Up @@ -42,11 +42,13 @@ export default function suggestionList(
class LexicalDistance {
_input: string;
_inputLowerCase: string;
_inputArray: Array<number>;
_rows: [Array<number>, Array<number>, Array<number>];

constructor(input: string) {
this._input = input;
this._inputLowerCase = input.toLowerCase();
this._inputArray = stringToArray(this._inputLowerCase);

this._rows = [
new Array(input.length + 1).fill(0),
Expand All @@ -67,13 +69,14 @@ class LexicalDistance {
return 1;
}

let a = optionLowerCase;
let b = this._inputLowerCase;
let a = stringToArray(optionLowerCase);
let b = this._inputArray;

if (a.length < b.length) {
const tmp = a;
a = b;
b = optionLowerCase;
b = tmp;
}

const aLength = a.length;
const bLength = b.length;

Expand Down Expand Up @@ -123,3 +126,12 @@ class LexicalDistance {
return distance <= threshold ? distance : undefined;
}
}

function stringToArray(str) {
const strLength = str.length;
const array = new Array(strLength);
for (let i = 0; i < strLength; ++i) {
array[i] = str.charCodeAt(i);
}
return array;
}

0 comments on commit 268888b

Please sign in to comment.