Skip to content

Commit 5a8d3c6

Browse files
authored
Merge pull request #24 from neilboyd/criteria-once
set criteria once before doing search
2 parents 5a344b5 + 48818d8 commit 5a8d3c6

File tree

10 files changed

+107
-77
lines changed

10 files changed

+107
-77
lines changed

dest/simple-jekyll-search.js

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Simple-Jekyll-Search 1.13.3
2+
* Simple-Jekyll-Search 1.14.0
33
* Copyright 2015-2025, Christian Fei, Neil Boyd
44
* Licensed under the MIT License.
55
*/
@@ -67,11 +67,17 @@ var _$fuzzysearch_1 = fuzzysearch;
6767
var _$FuzzySearchStrategy_4 = new FuzzySearchStrategy()
6868

6969
function FuzzySearchStrategy () {
70-
this.matches = function (string, crit) {
70+
this.criteria = ''
71+
this.setCriteria = function (crit) {
72+
this.criteria = crit.toUpperCase()
73+
return this
74+
}
75+
76+
this.matches = function (string) {
7177
if (string === null) {
7278
return false
7379
}
74-
return _$fuzzysearch_1(crit.toUpperCase(), string.toUpperCase())
80+
return _$fuzzysearch_1(this.criteria, string.toUpperCase())
7581
}
7682
}
7783

@@ -82,30 +88,33 @@ var _$LiteralSearchStrategy_5 = new LiteralSearchStrategy()
8288
const segmenter = new Intl.Segmenter([], { granularity: 'word' })
8389

8490
function LiteralSearchStrategy () {
85-
this.matches = function (str, crit) {
86-
if (!str) {
87-
return false
88-
}
89-
str = str.trim().toUpperCase()
91+
this.critArray = []
92+
this.setCriteria = function (crit) {
9093
crit = crit.trim().toUpperCase()
91-
92-
let critArray = []
9394
if (crit.startsWith('"') && crit.endsWith('"')) {
94-
critArray = [crit.substring(1, crit.length - 1)]
95+
this.critArray = [crit.substring(1, crit.length - 1)]
9596
} else {
9697
const segmentedText = segmenter.segment(crit)
97-
critArray = [...segmentedText]
98+
this.critArray = [...segmentedText]
9899
.filter((s) => s.isWordLike)
99100
.map((s) => s.segment)
100101
}
102+
return this
103+
}
101104

102-
if (critArray.length === 0) {
105+
this.matches = function (str) {
106+
if (!str) {
107+
return false
108+
}
109+
if (this.critArray.length === 0) {
103110
return false
104111
}
105112

106-
const filter = critArray.filter((word) => str.indexOf(word) >= 0)
113+
str = str.trim().toUpperCase()
114+
115+
const filter = this.critArray.filter((word) => str.indexOf(word) >= 0)
107116

108-
return filter.length === critArray.length // true if it found all the words
117+
return filter.length === this.critArray.length // true if it found all the words
109118
}
110119
}
111120

@@ -188,21 +197,22 @@ function __setOptions_3 (_opt) {
188197

189198
function findMatches (data, crit, opt) {
190199
const matches = []
200+
opt.searchStrategy.setCriteria(crit)
191201
for (let i = 0; i < data.length; i++) {
192202
if (!opt.sort && matches.length >= opt.limit) {
193203
break
194204
}
195-
const match = findMatchesInObject(data[i], crit, opt)
205+
const match = findMatchesInObject(data[i], opt)
196206
if (match) {
197207
matches.push(match)
198208
}
199209
}
200210
return matches
201211
}
202212

203-
function findMatchesInObject (obj, crit, opt) {
213+
function findMatchesInObject (obj, opt) {
204214
for (const key in obj) {
205-
if (key !== 'query' && !isExcluded(obj[key], opt.exclude) && opt.searchStrategy.matches(obj[key], crit)) {
215+
if (key !== 'query' && !isExcluded(obj[key], opt.exclude) && opt.searchStrategy.matches(obj[key])) {
206216
return obj
207217
}
208218
}

dest/simple-jekyll-search.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/js/simple-jekyll-search.js

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Simple-Jekyll-Search 1.13.3
2+
* Simple-Jekyll-Search 1.14.0
33
* Copyright 2015-2025, Christian Fei, Neil Boyd
44
* Licensed under the MIT License.
55
*/
@@ -67,11 +67,17 @@ var _$fuzzysearch_1 = fuzzysearch;
6767
var _$FuzzySearchStrategy_4 = new FuzzySearchStrategy()
6868

6969
function FuzzySearchStrategy () {
70-
this.matches = function (string, crit) {
70+
this.criteria = ''
71+
this.setCriteria = function (crit) {
72+
this.criteria = crit.toUpperCase()
73+
return this
74+
}
75+
76+
this.matches = function (string) {
7177
if (string === null) {
7278
return false
7379
}
74-
return _$fuzzysearch_1(crit.toUpperCase(), string.toUpperCase())
80+
return _$fuzzysearch_1(this.criteria, string.toUpperCase())
7581
}
7682
}
7783

@@ -82,30 +88,33 @@ var _$LiteralSearchStrategy_5 = new LiteralSearchStrategy()
8288
const segmenter = new Intl.Segmenter([], { granularity: 'word' })
8389

8490
function LiteralSearchStrategy () {
85-
this.matches = function (str, crit) {
86-
if (!str) {
87-
return false
88-
}
89-
str = str.trim().toUpperCase()
91+
this.critArray = []
92+
this.setCriteria = function (crit) {
9093
crit = crit.trim().toUpperCase()
91-
92-
let critArray = []
9394
if (crit.startsWith('"') && crit.endsWith('"')) {
94-
critArray = [crit.substring(1, crit.length - 1)]
95+
this.critArray = [crit.substring(1, crit.length - 1)]
9596
} else {
9697
const segmentedText = segmenter.segment(crit)
97-
critArray = [...segmentedText]
98+
this.critArray = [...segmentedText]
9899
.filter((s) => s.isWordLike)
99100
.map((s) => s.segment)
100101
}
102+
return this
103+
}
101104

102-
if (critArray.length === 0) {
105+
this.matches = function (str) {
106+
if (!str) {
107+
return false
108+
}
109+
if (this.critArray.length === 0) {
103110
return false
104111
}
105112

106-
const filter = critArray.filter((word) => str.indexOf(word) >= 0)
113+
str = str.trim().toUpperCase()
114+
115+
const filter = this.critArray.filter((word) => str.indexOf(word) >= 0)
107116

108-
return filter.length === critArray.length // true if it found all the words
117+
return filter.length === this.critArray.length // true if it found all the words
109118
}
110119
}
111120

@@ -188,21 +197,22 @@ function __setOptions_3 (_opt) {
188197

189198
function findMatches (data, crit, opt) {
190199
const matches = []
200+
opt.searchStrategy.setCriteria(crit)
191201
for (let i = 0; i < data.length; i++) {
192202
if (!opt.sort && matches.length >= opt.limit) {
193203
break
194204
}
195-
const match = findMatchesInObject(data[i], crit, opt)
205+
const match = findMatchesInObject(data[i], opt)
196206
if (match) {
197207
matches.push(match)
198208
}
199209
}
200210
return matches
201211
}
202212

203-
function findMatchesInObject (obj, crit, opt) {
213+
function findMatchesInObject (obj, opt) {
204214
for (const key in obj) {
205-
if (key !== 'query' && !isExcluded(obj[key], opt.exclude) && opt.searchStrategy.matches(obj[key], crit)) {
215+
if (key !== 'query' && !isExcluded(obj[key], opt.exclude) && opt.searchStrategy.matches(obj[key])) {
206216
return obj
207217
}
208218
}

0 commit comments

Comments
 (0)