Created
June 26, 2018 15:43
-
-
Save aire-con-gas/8b0d942b166dbaabae106cbe40f00fd3 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/bujuqih
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
'use strict'; | |
var generatePositions = function generatePositions(input, subStrings) { | |
var positionsArr = []; | |
var match = undefined; | |
subStrings.forEach(function (subString) { | |
var regex = new RegExp(subString); | |
if ((match = regex.exec(input)) !== null) { | |
positionsArr.push({ | |
start: match.index, | |
end: match.index + subString.length | |
}); | |
} | |
}); | |
return positionsArr; | |
}; | |
var findOverlaps = function findOverlaps(positionsArr) { | |
var newPositionsArr = []; | |
var lastEndIdx = 0; | |
var prevIdx = -1; | |
positionsArr.forEach(function (position, idx) { | |
if (position.start <= lastEndIdx && prevIdx >= 0) { | |
newPositionsArr[prevIdx].end = position.end; | |
} else { | |
newPositionsArr.push({ | |
start: position.start, | |
end: position.end | |
}); | |
} | |
lastEndIdx = position.end; | |
prevIdx = idx; | |
}); | |
return newPositionsArr; | |
}; | |
var embeddedBoldTags = function embeddedBoldTags(input, subStrings) { | |
var clonedInput = input.slice(); | |
// find all subString positions and create initialMap | |
var initialPositions = generatePositions(clonedInput, subStrings); | |
// sort by startIdx | |
initialPositions.sort(function (a, b) { | |
return a.start - b.start; | |
}); | |
// find overlaps and create new positionMap | |
var newPositions = findOverlaps(initialPositions); | |
// iterate through new positionMap and insert bold | |
var startBold = '<b>'; | |
var endBold = '</b>'; | |
var sbLength = startBold.length; | |
var ebLength = endBold.length; | |
var sentenceArr = clonedInput.split(''); | |
newPositions.forEach(function (insertions, idx) { | |
var offset = 0; | |
if (idx > 0) { | |
offset = 2; | |
} | |
var s = insertions.start + offset; | |
var e = insertions.end + 1 + offset; | |
console.log(s, e); | |
sentenceArr.splice(s, 0, startBold); | |
sentenceArr.splice(e, 0, endBold); | |
}); | |
return sentenceArr.join(''); | |
}; | |
console.log("embeddedBoldTags('abcdef', ['abc'])", embeddedBoldTags('abcdef', ['abc'])); | |
console.log("embeddedBoldTags('abcdef', ['abc', 'bcd'])", embeddedBoldTags('abcdef', ['abc', 'bcd'])); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">const generatePositions = (input, subStrings) => { | |
let positionsArr = []; | |
let match; | |
subStrings.forEach(subString => { | |
const regex = new RegExp(subString); | |
if ((match = regex.exec(input)) !== null) { | |
positionsArr.push({ | |
start: match.index, | |
end: match.index + subString.length | |
}) | |
} | |
}); | |
return positionsArr; | |
} | |
const findOverlaps = (positionsArr) => { | |
let newPositionsArr = []; | |
let lastEndIdx = 0; | |
let prevIdx = -1; | |
positionsArr.forEach((position, idx) => { | |
if (position.start <= lastEndIdx && prevIdx >= 0) { | |
newPositionsArr[prevIdx].end = position.end; | |
} else { | |
newPositionsArr.push({ | |
start: position.start, | |
end: position.end | |
}); | |
} | |
lastEndIdx = position.end; | |
prevIdx = idx; | |
}); | |
return newPositionsArr; | |
} | |
const embeddedBoldTags = (input, subStrings) => { | |
let clonedInput = input.slice(); | |
// find all subString positions and create initialMap | |
let initialPositions = generatePositions(clonedInput, subStrings); | |
// sort by startIdx | |
initialPositions.sort((a, b) => (a.start - b.start)); | |
// find overlaps and create new positionMap | |
let newPositions = findOverlaps(initialPositions); | |
// iterate through new positionMap and insert bold | |
const startBold = '<b>'; | |
const endBold = '</b>'; | |
const sbLength = startBold.length; | |
const ebLength = endBold.length; | |
let sentenceArr = clonedInput.split(''); | |
newPositions.forEach((insertions, idx) => { | |
let offset = 0; | |
if (idx > 0) { | |
offset = 2; | |
} | |
let s = insertions.start + offset; | |
let e = (insertions.end + 1) + offset; | |
console.log(s, e); | |
sentenceArr.splice(s, 0, startBold); | |
sentenceArr.splice(e, 0, endBold); | |
}); | |
return sentenceArr.join(''); | |
}; | |
console.log("embeddedBoldTags('abcdef', ['abc'])", embeddedBoldTags('abcdef', ['abc'])); | |
console.log("embeddedBoldTags('abcdef', ['abc', 'bcd'])", embeddedBoldTags('abcdef', ['abc', 'bcd']));</script></body> | |
</html> |
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
'use strict'; | |
var generatePositions = function generatePositions(input, subStrings) { | |
var positionsArr = []; | |
var match = undefined; | |
subStrings.forEach(function (subString) { | |
var regex = new RegExp(subString); | |
if ((match = regex.exec(input)) !== null) { | |
positionsArr.push({ | |
start: match.index, | |
end: match.index + subString.length | |
}); | |
} | |
}); | |
return positionsArr; | |
}; | |
var findOverlaps = function findOverlaps(positionsArr) { | |
var newPositionsArr = []; | |
var lastEndIdx = 0; | |
var prevIdx = -1; | |
positionsArr.forEach(function (position, idx) { | |
if (position.start <= lastEndIdx && prevIdx >= 0) { | |
newPositionsArr[prevIdx].end = position.end; | |
} else { | |
newPositionsArr.push({ | |
start: position.start, | |
end: position.end | |
}); | |
} | |
lastEndIdx = position.end; | |
prevIdx = idx; | |
}); | |
return newPositionsArr; | |
}; | |
var embeddedBoldTags = function embeddedBoldTags(input, subStrings) { | |
var clonedInput = input.slice(); | |
// find all subString positions and create initialMap | |
var initialPositions = generatePositions(clonedInput, subStrings); | |
// sort by startIdx | |
initialPositions.sort(function (a, b) { | |
return a.start - b.start; | |
}); | |
// find overlaps and create new positionMap | |
var newPositions = findOverlaps(initialPositions); | |
// iterate through new positionMap and insert bold | |
var startBold = '<b>'; | |
var endBold = '</b>'; | |
var sbLength = startBold.length; | |
var ebLength = endBold.length; | |
var sentenceArr = clonedInput.split(''); | |
newPositions.forEach(function (insertions, idx) { | |
var offset = 0; | |
if (idx > 0) { | |
offset = 2; | |
} | |
var s = insertions.start + offset; | |
var e = insertions.end + 1 + offset; | |
console.log(s, e); | |
sentenceArr.splice(s, 0, startBold); | |
sentenceArr.splice(e, 0, endBold); | |
}); | |
return sentenceArr.join(''); | |
}; | |
console.log("embeddedBoldTags('abcdef', ['abc'])", embeddedBoldTags('abcdef', ['abc'])); | |
console.log("embeddedBoldTags('abcdef', ['abc', 'bcd'])", embeddedBoldTags('abcdef', ['abc', 'bcd'])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment