Skip to content

Instantly share code, notes, and snippets.

@muhsalaa
Last active October 17, 2019 02:14
Show Gist options
  • Save muhsalaa/2da8f389a354a3140d54aa5eeafa0a61 to your computer and use it in GitHub Desktop.
Save muhsalaa/2da8f389a354a3140d54aa5eeafa0a61 to your computer and use it in GitHub Desktop.
function joinArrayRange(array, start, end, color, normalize) {
let result = '';
const joined = array.slice(start, end + 1).join(' ');
array.splice(start, end - start + 1, `<span style="background: ${color}">${joined}</span>`, ...[...Array(end - start).fill('<xxx>')]);
if (normalize) {
result = array.filter(item => item !== '<xxx>').join(' ');
} else {
result = array.join(' ');
}
return result;
}
function getWordsCoordinate(words, start, end) {
const targetIndex = words.slice(0, start).trim().split(' ').length;
const numberOfWord = word.slice(start, end).trim().split(' ').length
return [targetIndex, targetIndex + numberOfWord - 1 ];
}
function replaceWordByCoordinateUnreliable(foo, coord) {
let words = foo
const changed = [];
for (let index of coord) {
const id = makeid(index[1] - index[0] + 1)
const target = words.substring(index[0], index[1] + 1);
changed.push({ id, content: `<spans>${target}</spans>`});
words = words.substring(0, index[0])
+ id
+ words.substring(index[1] + 1, words.length)
}
console.log(words)
for (let item of changed) {
words = words.replace(item.id, item.content);
}
return words;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment