Last active
March 28, 2020 19:58
-
-
Save agusnavce/56697af7c5a2866e92fce2f7f43e9b38 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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