Skip to content

Commit

Permalink
don't use trailing space as exact search indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
neilboyd committed Aug 8, 2024
1 parent bc164c2 commit c72250c
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions src/SearchStrategies/LiteralSearchStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,25 @@ module.exports = new LiteralSearchStrategy()

const segmenter = new Intl.Segmenter([], { granularity: 'word' })

function LiteralSearchStrategy () {
function LiteralSearchStrategy() {
this.matches = function (str, crit) {
if (!str) return false
if (!str) {
return false
}
str = str.trim().toLowerCase()
crit = crit.trim().toLowerCase()

let exact = false
if (crit.endsWith(' ')) {
exact = true
}
if (crit.startsWith('"') && crit.endsWith('"')) {
exact = true
crit = crit.substring(1, crit.length - 1)
}
crit = crit.toLowerCase()
let critArray = [crit]
if (!exact) {
let critArray = [crit.substring(1, crit.length - 1)]
} else {
const segmentedText = segmenter.segment(crit)
critArray = [...segmentedText].filter(s => s.isWordLike).map(s => s.segment)
let critArray = [...segmentedText]
.filter((s) => s.isWordLike)
.map((s) => s.segment)
}

return (
critArray
.filter((word) => str.indexOf(word) >= 0)
.length === critArray.length
)
let filter = critArray.filter((word) => str.indexOf(word) >= 0)

return filter.length === critArray.length // true if it found all the words
}
}

0 comments on commit c72250c

Please sign in to comment.