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('')
}
javascript strings autocomplete
add a comment |
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('')
}
javascript strings autocomplete
could you add more context - e.g. how isBolden()
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
add a comment |
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('')
}
javascript strings autocomplete
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
javascript strings autocomplete
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 isBolden()
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
add a comment |
could you add more context - e.g. how isBolden()
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
add a comment |
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))
This isn't case insensitive. Even if I were to calltoLowerCase
onquery
, 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 asbolden
); While you mentioned template literals with a reference link, it might be wise to avoid using es-6 features likelet
unless the OP uses the tag ecmascript-6 or already has such features used; if you do uselet
it might be wise to only use it when re-assignment is necessary - otherwise useconst
– Sᴀᴍ Onᴇᴌᴀ
Aug 14 at 22:16
add a comment |
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))
This isn't case insensitive. Even if I were to calltoLowerCase
onquery
, 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 asbolden
); While you mentioned template literals with a reference link, it might be wise to avoid using es-6 features likelet
unless the OP uses the tag ecmascript-6 or already has such features used; if you do uselet
it might be wise to only use it when re-assignment is necessary - otherwise useconst
– Sᴀᴍ Onᴇᴌᴀ
Aug 14 at 22:16
add a comment |
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))
This isn't case insensitive. Even if I were to calltoLowerCase
onquery
, 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 asbolden
); While you mentioned template literals with a reference link, it might be wise to avoid using es-6 features likelet
unless the OP uses the tag ecmascript-6 or already has such features used; if you do uselet
it might be wise to only use it when re-assignment is necessary - otherwise useconst
– Sᴀᴍ Onᴇᴌᴀ
Aug 14 at 22:16
add a comment |
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))
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))
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 calltoLowerCase
onquery
, 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 asbolden
); While you mentioned template literals with a reference link, it might be wise to avoid using es-6 features likelet
unless the OP uses the tag ecmascript-6 or already has such features used; if you do uselet
it might be wise to only use it when re-assignment is necessary - otherwise useconst
– Sᴀᴍ Onᴇᴌᴀ
Aug 14 at 22:16
add a comment |
This isn't case insensitive. Even if I were to calltoLowerCase
onquery
, 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 asbolden
); While you mentioned template literals with a reference link, it might be wise to avoid using es-6 features likelet
unless the OP uses the tag ecmascript-6 or already has such features used; if you do uselet
it might be wise to only use it when re-assignment is necessary - otherwise useconst
– 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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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