Embolden parts of auto-complete suggestions that don't match the query











up vote
0
down vote

favorite












I made a function to embolden text that doesn't match a string, but it appears to be very performance-heavy. I am certain that there are much better ways to do this, but with what I tried, I ran into a lot of trouble.



As a side note, one of the ways I know this is performance-heavy, is that when I type a few characters, and backspace them all, Chrome starts taking up more and more memory until it crashes.



Yes! I know this is not StackOverflow, so I'm just asking for some pointers on what the issues are with the code.



function Bolden(value, query) {
var arr =
if (!value) return;
while (value !== "") { // While the value still has work to be done on it
if (value.toLowerCase().startsWith(query.toLowerCase())) {
arr.push("<span style='font-weight: normal'>" + value.slice(0, query.length) + "</span>")
value = value.slice(query.length, value.length)
}
else {
arr.push(value.slice(0, 1))
value = value.slice(1, value.length)
}
}
return arr.join('')
}









share|improve this question
























  • could you add more context - e.g. how is Bolden() used? is it called once for each element in a list? if possible, provide sample HTML ...
    – Sᴀᴍ Onᴇᴌᴀ
    Aug 14 at 21:18












  • It would take a lot of time for me to separate just the search box from everything else to be able to put here. Also, the search API it uses is only available on my company's domain.
    – Don't Know
    Aug 14 at 21:20










  • Hmm ... I made a sample on jsFiddle - could you fork it and update it to be closer to how it is actually used?
    – Sᴀᴍ Onᴇᴌᴀ
    Aug 14 at 22:07















up vote
0
down vote

favorite












I made a function to embolden text that doesn't match a string, but it appears to be very performance-heavy. I am certain that there are much better ways to do this, but with what I tried, I ran into a lot of trouble.



As a side note, one of the ways I know this is performance-heavy, is that when I type a few characters, and backspace them all, Chrome starts taking up more and more memory until it crashes.



Yes! I know this is not StackOverflow, so I'm just asking for some pointers on what the issues are with the code.



function Bolden(value, query) {
var arr =
if (!value) return;
while (value !== "") { // While the value still has work to be done on it
if (value.toLowerCase().startsWith(query.toLowerCase())) {
arr.push("<span style='font-weight: normal'>" + value.slice(0, query.length) + "</span>")
value = value.slice(query.length, value.length)
}
else {
arr.push(value.slice(0, 1))
value = value.slice(1, value.length)
}
}
return arr.join('')
}









share|improve this question
























  • could you add more context - e.g. how is Bolden() used? is it called once for each element in a list? if possible, provide sample HTML ...
    – Sᴀᴍ Onᴇᴌᴀ
    Aug 14 at 21:18












  • It would take a lot of time for me to separate just the search box from everything else to be able to put here. Also, the search API it uses is only available on my company's domain.
    – Don't Know
    Aug 14 at 21:20










  • Hmm ... I made a sample on jsFiddle - could you fork it and update it to be closer to how it is actually used?
    – Sᴀᴍ Onᴇᴌᴀ
    Aug 14 at 22:07













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I made a function to embolden text that doesn't match a string, but it appears to be very performance-heavy. I am certain that there are much better ways to do this, but with what I tried, I ran into a lot of trouble.



As a side note, one of the ways I know this is performance-heavy, is that when I type a few characters, and backspace them all, Chrome starts taking up more and more memory until it crashes.



Yes! I know this is not StackOverflow, so I'm just asking for some pointers on what the issues are with the code.



function Bolden(value, query) {
var arr =
if (!value) return;
while (value !== "") { // While the value still has work to be done on it
if (value.toLowerCase().startsWith(query.toLowerCase())) {
arr.push("<span style='font-weight: normal'>" + value.slice(0, query.length) + "</span>")
value = value.slice(query.length, value.length)
}
else {
arr.push(value.slice(0, 1))
value = value.slice(1, value.length)
}
}
return arr.join('')
}









share|improve this question















I made a function to embolden text that doesn't match a string, but it appears to be very performance-heavy. I am certain that there are much better ways to do this, but with what I tried, I ran into a lot of trouble.



As a side note, one of the ways I know this is performance-heavy, is that when I type a few characters, and backspace them all, Chrome starts taking up more and more memory until it crashes.



Yes! I know this is not StackOverflow, so I'm just asking for some pointers on what the issues are with the code.



function Bolden(value, query) {
var arr =
if (!value) return;
while (value !== "") { // While the value still has work to be done on it
if (value.toLowerCase().startsWith(query.toLowerCase())) {
arr.push("<span style='font-weight: normal'>" + value.slice(0, query.length) + "</span>")
value = value.slice(query.length, value.length)
}
else {
arr.push(value.slice(0, 1))
value = value.slice(1, value.length)
}
}
return arr.join('')
}






javascript strings autocomplete






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 14 at 21:16









Sᴀᴍ Onᴇᴌᴀ

7,67561748




7,67561748










asked Aug 14 at 21:01









Don't Know

1




1












  • could you add more context - e.g. how is Bolden() used? is it called once for each element in a list? if possible, provide sample HTML ...
    – Sᴀᴍ Onᴇᴌᴀ
    Aug 14 at 21:18












  • It would take a lot of time for me to separate just the search box from everything else to be able to put here. Also, the search API it uses is only available on my company's domain.
    – Don't Know
    Aug 14 at 21:20










  • Hmm ... I made a sample on jsFiddle - could you fork it and update it to be closer to how it is actually used?
    – Sᴀᴍ Onᴇᴌᴀ
    Aug 14 at 22:07


















  • could you add more context - e.g. how is Bolden() used? is it called once for each element in a list? if possible, provide sample HTML ...
    – Sᴀᴍ Onᴇᴌᴀ
    Aug 14 at 21:18












  • It would take a lot of time for me to separate just the search box from everything else to be able to put here. Also, the search API it uses is only available on my company's domain.
    – Don't Know
    Aug 14 at 21:20










  • Hmm ... I made a sample on jsFiddle - could you fork it and update it to be closer to how it is actually used?
    – Sᴀᴍ Onᴇᴌᴀ
    Aug 14 at 22:07
















could you add more context - e.g. how is Bolden() used? is it called once for each element in a list? if possible, provide sample HTML ...
– Sᴀᴍ Onᴇᴌᴀ
Aug 14 at 21:18






could you add more context - e.g. how is Bolden() used? is it called once for each element in a list? if possible, provide sample HTML ...
– Sᴀᴍ Onᴇᴌᴀ
Aug 14 at 21:18














It would take a lot of time for me to separate just the search box from everything else to be able to put here. Also, the search API it uses is only available on my company's domain.
– Don't Know
Aug 14 at 21:20




It would take a lot of time for me to separate just the search box from everything else to be able to put here. Also, the search API it uses is only available on my company's domain.
– Don't Know
Aug 14 at 21:20












Hmm ... I made a sample on jsFiddle - could you fork it and update it to be closer to how it is actually used?
– Sᴀᴍ Onᴇᴌᴀ
Aug 14 at 22:07




Hmm ... I made a sample on jsFiddle - could you fork it and update it to be closer to how it is actually used?
– Sᴀᴍ Onᴇᴌᴀ
Aug 14 at 22:07










1 Answer
1






active

oldest

votes

















up vote
0
down vote













Based on the current context available, if your query is not to be treated as a regex/custom pattern, I think a simple string.split should be enough. I am not that familiar with conventions in the JS world, but the last time I had worked with it, function names used to be in camelCase.



Also note that I am using the template literals when returning from bolden function; which should actually be named unemphasize (or unbold or normaliser).






function bolden(value, query) {
if (!value) return;
let splt = value.split(query)
return splt.join(`<span style="font-weight:normal">${query}</span>`)
}

let tests = new Map([
['some', 'e'],
['things', 'thi'],
['other', 'te'],
['looooooooong', 'o']
])

for (let [v, q] of tests)
console.log(bolden(v, q))








share|improve this answer























  • This isn't case insensitive. Even if I were to call toLowerCase on query, it would replace capitalized matches with the lower case version. Thanks!
    – Don't Know
    Aug 14 at 21:41












  • Bolden is not defined in that snippet (since it was declared as bolden); While you mentioned template literals with a reference link, it might be wise to avoid using es-6 features like let unless the OP uses the tag ecmascript-6 or already has such features used; if you do use let it might be wise to only use it when re-assignment is necessary - otherwise use const
    – Sᴀᴍ Onᴇᴌᴀ
    Aug 14 at 22:16













Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f201691%2fembolden-parts-of-auto-complete-suggestions-that-dont-match-the-query%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













Based on the current context available, if your query is not to be treated as a regex/custom pattern, I think a simple string.split should be enough. I am not that familiar with conventions in the JS world, but the last time I had worked with it, function names used to be in camelCase.



Also note that I am using the template literals when returning from bolden function; which should actually be named unemphasize (or unbold or normaliser).






function bolden(value, query) {
if (!value) return;
let splt = value.split(query)
return splt.join(`<span style="font-weight:normal">${query}</span>`)
}

let tests = new Map([
['some', 'e'],
['things', 'thi'],
['other', 'te'],
['looooooooong', 'o']
])

for (let [v, q] of tests)
console.log(bolden(v, q))








share|improve this answer























  • This isn't case insensitive. Even if I were to call toLowerCase on query, it would replace capitalized matches with the lower case version. Thanks!
    – Don't Know
    Aug 14 at 21:41












  • Bolden is not defined in that snippet (since it was declared as bolden); While you mentioned template literals with a reference link, it might be wise to avoid using es-6 features like let unless the OP uses the tag ecmascript-6 or already has such features used; if you do use let it might be wise to only use it when re-assignment is necessary - otherwise use const
    – Sᴀᴍ Onᴇᴌᴀ
    Aug 14 at 22:16

















up vote
0
down vote













Based on the current context available, if your query is not to be treated as a regex/custom pattern, I think a simple string.split should be enough. I am not that familiar with conventions in the JS world, but the last time I had worked with it, function names used to be in camelCase.



Also note that I am using the template literals when returning from bolden function; which should actually be named unemphasize (or unbold or normaliser).






function bolden(value, query) {
if (!value) return;
let splt = value.split(query)
return splt.join(`<span style="font-weight:normal">${query}</span>`)
}

let tests = new Map([
['some', 'e'],
['things', 'thi'],
['other', 'te'],
['looooooooong', 'o']
])

for (let [v, q] of tests)
console.log(bolden(v, q))








share|improve this answer























  • This isn't case insensitive. Even if I were to call toLowerCase on query, it would replace capitalized matches with the lower case version. Thanks!
    – Don't Know
    Aug 14 at 21:41












  • Bolden is not defined in that snippet (since it was declared as bolden); While you mentioned template literals with a reference link, it might be wise to avoid using es-6 features like let unless the OP uses the tag ecmascript-6 or already has such features used; if you do use let it might be wise to only use it when re-assignment is necessary - otherwise use const
    – Sᴀᴍ Onᴇᴌᴀ
    Aug 14 at 22:16















up vote
0
down vote










up vote
0
down vote









Based on the current context available, if your query is not to be treated as a regex/custom pattern, I think a simple string.split should be enough. I am not that familiar with conventions in the JS world, but the last time I had worked with it, function names used to be in camelCase.



Also note that I am using the template literals when returning from bolden function; which should actually be named unemphasize (or unbold or normaliser).






function bolden(value, query) {
if (!value) return;
let splt = value.split(query)
return splt.join(`<span style="font-weight:normal">${query}</span>`)
}

let tests = new Map([
['some', 'e'],
['things', 'thi'],
['other', 'te'],
['looooooooong', 'o']
])

for (let [v, q] of tests)
console.log(bolden(v, q))








share|improve this answer














Based on the current context available, if your query is not to be treated as a regex/custom pattern, I think a simple string.split should be enough. I am not that familiar with conventions in the JS world, but the last time I had worked with it, function names used to be in camelCase.



Also note that I am using the template literals when returning from bolden function; which should actually be named unemphasize (or unbold or normaliser).






function bolden(value, query) {
if (!value) return;
let splt = value.split(query)
return splt.join(`<span style="font-weight:normal">${query}</span>`)
}

let tests = new Map([
['some', 'e'],
['things', 'thi'],
['other', 'te'],
['looooooooong', 'o']
])

for (let [v, q] of tests)
console.log(bolden(v, q))








function bolden(value, query) {
if (!value) return;
let splt = value.split(query)
return splt.join(`<span style="font-weight:normal">${query}</span>`)
}

let tests = new Map([
['some', 'e'],
['things', 'thi'],
['other', 'te'],
['looooooooong', 'o']
])

for (let [v, q] of tests)
console.log(bolden(v, q))





function bolden(value, query) {
if (!value) return;
let splt = value.split(query)
return splt.join(`<span style="font-weight:normal">${query}</span>`)
}

let tests = new Map([
['some', 'e'],
['things', 'thi'],
['other', 'te'],
['looooooooong', 'o']
])

for (let [v, q] of tests)
console.log(bolden(v, q))






share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 15 at 5:03

























answered Aug 14 at 21:27









hjpotter92

5,11111539




5,11111539












  • This isn't case insensitive. Even if I were to call toLowerCase on query, it would replace capitalized matches with the lower case version. Thanks!
    – Don't Know
    Aug 14 at 21:41












  • Bolden is not defined in that snippet (since it was declared as bolden); While you mentioned template literals with a reference link, it might be wise to avoid using es-6 features like let unless the OP uses the tag ecmascript-6 or already has such features used; if you do use let it might be wise to only use it when re-assignment is necessary - otherwise use const
    – Sᴀᴍ Onᴇᴌᴀ
    Aug 14 at 22:16




















  • This isn't case insensitive. Even if I were to call toLowerCase on query, it would replace capitalized matches with the lower case version. Thanks!
    – Don't Know
    Aug 14 at 21:41












  • Bolden is not defined in that snippet (since it was declared as bolden); While you mentioned template literals with a reference link, it might be wise to avoid using es-6 features like let unless the OP uses the tag ecmascript-6 or already has such features used; if you do use let it might be wise to only use it when re-assignment is necessary - otherwise use const
    – Sᴀᴍ Onᴇᴌᴀ
    Aug 14 at 22:16


















This isn't case insensitive. Even if I were to call toLowerCase on query, it would replace capitalized matches with the lower case version. Thanks!
– Don't Know
Aug 14 at 21:41






This isn't case insensitive. Even if I were to call toLowerCase on query, it would replace capitalized matches with the lower case version. Thanks!
– Don't Know
Aug 14 at 21:41














Bolden is not defined in that snippet (since it was declared as bolden); While you mentioned template literals with a reference link, it might be wise to avoid using es-6 features like let unless the OP uses the tag ecmascript-6 or already has such features used; if you do use let it might be wise to only use it when re-assignment is necessary - otherwise use const
– Sᴀᴍ Onᴇᴌᴀ
Aug 14 at 22:16






Bolden is not defined in that snippet (since it was declared as bolden); While you mentioned template literals with a reference link, it might be wise to avoid using es-6 features like let unless the OP uses the tag ecmascript-6 or already has such features used; if you do use let it might be wise to only use it when re-assignment is necessary - otherwise use const
– Sᴀᴍ Onᴇᴌᴀ
Aug 14 at 22:16




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f201691%2fembolden-parts-of-auto-complete-suggestions-that-dont-match-the-query%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Сан-Квентин

8-я гвардейская общевойсковая армия

Алькесар