Skip to content

Instantly share code, notes, and snippets.

@agusnavce
Last active March 28, 2020 19:58
Show Gist options
  • Save agusnavce/56697af7c5a2866e92fce2f7f43e9b38 to your computer and use it in GitHub Desktop.
Save agusnavce/56697af7c5a2866e92fce2f7f43e9b38 to your computer and use it in GitHub Desktop.
function prepareWordListWithSuggestions(phrase = '', suggestions = []) {
const result = [];
let phraseAux = phrase;
let phrasePointer = 0;
suggestions.forEach((suggestion) => {
const start = suggestion.start - phrasePointer;
const end = suggestion.offset - phrasePointer;
const firstPart = phraseAux.slice(0, start);
if (firstPart !== '') {
result.push({ phrase: firstPart });
}
result.push({
phrase: phraseAux.substr(start, suggestion.offset),
suggestions: suggestion.recommends,
});
phraseAux = phraseAux.slice(end);
phrasePointer += end;
});
if (phraseAux !== '') {
result.push({ phrase: phraseAux });
}
return result;
}
function removeFromListWithSuggestions(listWithSuggestions = [], phrase = '', suggestion = '') {
const index = listWithSuggestions.findIndex(text => text.phrase === phrase);
const result = listWithSuggestions.slice();
if (index >= 0) {
result.splice(index, 1, { phrase: phrase });
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment