@quenty/fzy 1.0.1-canary.317.53a24a4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ ## 1.0.1-canary.317.53a24a4.0 (2022-12-29)
7
+
8
+
9
+ ### Features
10
+
11
+ * Add Fzy package for fuzzy searching ([d1a4864](https://github.com/Quenty/NevermoreEngine/commit/d1a486423c84db737895c65c4b7f44005bc48e2f))
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2014-2022 Quenty
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,23 @@
1
+ ## Fzy
2
+
3
+ <div align="center">
4
+ <a href="http://quenty.github.io/NevermoreEngine/">
5
+ <img src="https://github.com/Quenty/NevermoreEngine/actions/workflows/docs.yml/badge.svg" alt="Documentation status" />
6
+ </a>
7
+ <a href="https://discord.gg/mhtGUS8">
8
+ <img src="https://img.shields.io/discord/385151591524597761?color=5865F2&label=discord&logo=discord&logoColor=white" alt="Discord" />
9
+ </a>
10
+ <a href="https://github.com/Quenty/NevermoreEngine/actions">
11
+ <img src="https://github.com/Quenty/NevermoreEngine/actions/workflows/build.yml/badge.svg" alt="Build and release status" />
12
+ </a>
13
+ </div>
14
+
15
+ Lua implementation of fzy string search algorithm
16
+
17
+ <div align="center"><a href="https://quenty.github.io/NevermoreEngine/api/FzyUtils">View docs →</a></div>
18
+
19
+ ## Installation
20
+
21
+ ```
22
+ npm install @quenty/fzy --save
23
+ ```
@@ -0,0 +1,6 @@
1
+ {
2
+ "name": "fzy",
3
+ "tree": {
4
+ "$path": "src"
5
+ }
6
+ }
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@quenty/fzy",
3
+ "version": "1.0.1-canary.317.53a24a4.0",
4
+ "description": "Lua implementation of fzy string search algorithm",
5
+ "keywords": [
6
+ "Roblox",
7
+ "Nevermore",
8
+ "Lua",
9
+ "Fzy",
10
+ "String",
11
+ "Fuzzy-Search",
12
+ "Filter"
13
+ ],
14
+ "bugs": {
15
+ "url": "https://github.com/Quenty/NevermoreEngine/issues"
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/Quenty/NevermoreEngine.git",
20
+ "directory": "src/fzy/"
21
+ },
22
+ "license": "MIT",
23
+ "contributors": [
24
+ "Quenty"
25
+ ],
26
+ "dependencies": {
27
+ "@quenty/loader": "6.0.1"
28
+ },
29
+ "publishConfig": {
30
+ "access": "public"
31
+ },
32
+ "gitHead": "53a24a494885e51dba271922a1848b240872013c"
33
+ }
@@ -0,0 +1,417 @@
1
+ --[=[
2
+ The lua implementation of the fzy string matching algorithm. This algorithm
3
+ is optimized for matching stuff on the terminal, but should serve well as a
4
+ baseline search algorithm within a game too.
5
+
6
+ See:
7
+ * https://github.com/swarn/fzy-lua
8
+ * https://github.com/jhawthorn/fzy/blob/master/ALGORITHM.md
9
+
10
+ Modified from the initial code to fit this codebase. While this
11
+ definitely messes with some naming which may have been better, it
12
+ also keeps usage of this library consistent with other libraries.
13
+
14
+ Notes:
15
+ * A higher score is better than a lower score
16
+ * Scoring time is `O(n*m)` where `n` is the length of the needle
17
+ and `m` is the length of the haystack.
18
+ * Scoring memory is also `O(n*m)`
19
+ * Should do quite well with small lists
20
+
21
+ TODO: Support UTF8
22
+
23
+ @class Fzy
24
+ ]=]
25
+
26
+ --[[
27
+ The MIT License (MIT)
28
+
29
+ Copyright (c) 2020 Seth Warn
30
+
31
+ Permission is hereby granted, free of charge, to any person obtaining a copy
32
+ of this software and associated documentation files (the "Software"), to deal
33
+ in the Software without restriction, including without limitation the rights
34
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
35
+ copies of the Software, and to permit persons to whom the Software is
36
+ furnished to do so, subject to the following conditions:
37
+
38
+ The above copyright notice and this permission notice shall be included in
39
+ all copies or substantial portions of the Software.
40
+
41
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
42
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
43
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
44
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
45
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
46
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
47
+ THE SOFTWARE.
48
+ ]]
49
+
50
+ local MAX_SCORE = math.huge
51
+ local MIN_SCORE = -math.huge
52
+
53
+ local Fzy = {}
54
+
55
+ --[=[
56
+ Configuration for Fzy. See [Fzy.createConfig] for details. This affects scoring
57
+ and how the matching is done.
58
+
59
+ @interface FzyConfig
60
+ .caseSensitive boolean
61
+ .gapLeadingScore number
62
+ .gapTrailingScore number
63
+ .gapInnerScore number
64
+ .consecutiveMatchScore number
65
+ .slashMatchScore number
66
+ .wordMatchScore number
67
+ .capitalMatchScore number
68
+ .dotMatchScore number
69
+ .maxMatchLength number
70
+ @within Fzy
71
+ ]=]
72
+
73
+ --[=[
74
+ Creates a new configuration for Fzy.
75
+
76
+ @param config table
77
+ @return FzyConfig
78
+ ]=]
79
+ function Fzy.createConfig(config)
80
+ assert(type(config) == "table" or config == nil, "Bad config")
81
+
82
+ config = config or {}
83
+
84
+ if config.caseSensitive == nil then
85
+ config.caseSensitive = false
86
+ elseif type(config.caseSensitive) ~= "boolean" then
87
+ error("Bad config.caseSensitive")
88
+ end
89
+
90
+ -- These numbers are from the Fzy, algorithm but may be adjusted
91
+ config.gapLeadingScore = config.gapLeadingScore or -0.005
92
+ config.gapTrailingScore = config.gapTrailingScore or -0.005
93
+ config.gapInnerScore = config.gapInnerScore or -0.01
94
+ config.consecutiveMatchScore = config.consecutiveMatchScore or 1.0
95
+ config.slashMatchScore = config.slashMatchScore or 0.9
96
+ config.wordMatchScore = config.wordMatchScore or 0.8
97
+ config.capitalMatchScore = config.capitalMatchScore or 0.7
98
+ config.dotMatchScore = config.dotMatchScore or 0.6
99
+ config.maxMatchLength = config.maxMatchLength or 1024
100
+
101
+ return config
102
+ end
103
+
104
+ --[=[
105
+ Returns true if it is a config
106
+
107
+ @param config any
108
+ @return boolean
109
+ ]=]
110
+ function Fzy.isFzyConfig(config)
111
+ return type(config) == "table"
112
+ and type(config.gapLeadingScore) == "number"
113
+ and type(config.gapTrailingScore) == "number"
114
+ and type(config.gapInnerScore) == "number"
115
+ and type(config.consecutiveMatchScore) == "number"
116
+ and type(config.slashMatchScore) == "number"
117
+ and type(config.wordMatchScore) == "number"
118
+ and type(config.capitalMatchScore) == "number"
119
+ and type(config.dotMatchScore) == "number"
120
+ and type(config.maxMatchLength) == "number"
121
+ and type(config.caseSensitive) == "boolean"
122
+ end
123
+
124
+ --[=[
125
+ Check if `needle` is a subsequence of the `haystack`.
126
+
127
+ Usually called before `score` or `positions`.
128
+
129
+ @param config FzyConfig
130
+ @param needle string
131
+ @param haystack string
132
+ @return boolean
133
+ ]=]
134
+ function Fzy.hasMatch(config, needle: string, haystack: string)
135
+ if not config.caseSensitive then
136
+ needle = string.lower(needle)
137
+ haystack = string.lower(haystack)
138
+ end
139
+
140
+ local j = 1
141
+ for i = 1, string.len(needle) do
142
+ j = string.find(haystack, needle:sub(i, i), j, true)
143
+ if not j then
144
+ return false
145
+ else
146
+ j = j + 1
147
+ end
148
+ end
149
+
150
+ return true
151
+ end
152
+
153
+ local function is_lower(c)
154
+ return string.match(c, "%l")
155
+ end
156
+
157
+ local function is_upper(c)
158
+ return string.match(c, "%u")
159
+ end
160
+
161
+ local function precomputeBonus(config, haystack: string)
162
+ local matchBonus = {}
163
+
164
+ local last_char = "/"
165
+ for i = 1, string.len(haystack) do
166
+ local this_char = haystack:sub(i, i)
167
+ if last_char == "/" or last_char == "\\" then
168
+ matchBonus[i] = config.slashMatchScore
169
+ elseif last_char == "-" or last_char == "_" or last_char == " " then
170
+ matchBonus[i] = config.wordMatchScore
171
+ elseif last_char == "." then
172
+ matchBonus[i] = config.dotMatchScore
173
+ elseif is_lower(last_char) and is_upper(this_char) then
174
+ matchBonus[i] = config.capitalMatchScore
175
+ else
176
+ matchBonus[i] = 0
177
+ end
178
+
179
+ last_char = this_char
180
+ end
181
+
182
+ return matchBonus
183
+ end
184
+
185
+ local function compute(config, needle: string, haystack: string, D, M)
186
+ -- Note that the match bonuses must be computed before the arguments are
187
+ -- converted to lowercase, since there are bonuses for camelCase.
188
+
189
+ local matchBonus = precomputeBonus(config, haystack)
190
+ local n = string.len(needle)
191
+ local m = string.len(haystack)
192
+
193
+ if not config.caseSensitive then
194
+ needle = string.lower(needle)
195
+ haystack = string.lower(haystack)
196
+ end
197
+
198
+ -- Because lua only grants access to chars through substring extraction,
199
+ -- get all the characters from the haystack once now, to reuse below.
200
+ local haystackChars = {}
201
+ for i = 1, m do
202
+ haystackChars[i] = haystack:sub(i, i)
203
+ end
204
+
205
+ for i = 1, n do
206
+ D[i] = {}
207
+ M[i] = {}
208
+
209
+ local prevScore = MIN_SCORE
210
+ local gapScore = i == n and config.gapTrailingScore or config.gapInnerScore
211
+ local needle_char = needle:sub(i, i)
212
+
213
+ for j = 1, m do
214
+ if needle_char == haystackChars[j] then
215
+ local score = MIN_SCORE
216
+ if i == 1 then
217
+ score = ((j - 1) * config.gapLeadingScore) + matchBonus[j]
218
+ elseif j > 1 then
219
+ local a = M[i - 1][j - 1] + matchBonus[j]
220
+ local b = D[i - 1][j - 1] + config.consecutiveMatchScore
221
+ score = math.max(a, b)
222
+ end
223
+ D[i][j] = score
224
+ prevScore = math.max(score, prevScore + gapScore)
225
+ M[i][j] = prevScore
226
+ else
227
+ D[i][j] = MIN_SCORE
228
+ prevScore = prevScore + gapScore
229
+ M[i][j] = prevScore
230
+ end
231
+ end
232
+ end
233
+ end
234
+
235
+ --[=[
236
+ Computes whether a needle or haystack are a perfect match or not
237
+
238
+ @param config FzyConfig
239
+ @param needle string -- must be a subequence of `haystack`, or the result is undefined.
240
+ @param haystack string
241
+ @return boolean
242
+ ]=]
243
+ function Fzy.isPerfectMatch(config, needle, haystack)
244
+ if config.caseSensitive then
245
+ return needle == haystack
246
+ else
247
+ return string.lower(needle) == string.lower(haystack)
248
+ end
249
+ end
250
+
251
+ --[=[
252
+ Compute a matching score.
253
+
254
+ @param config FzyConfig
255
+ @param needle string -- must be a subequence of `haystack`, or the result is undefined.
256
+ @param haystack string
257
+ @return number -- higher scores indicate better matches. See also [Fzy.getMinScore] and [Fzy.getMaxScore].
258
+ ]=]
259
+ function Fzy.score(config, needle: string, haystack: string): number
260
+ local n = string.len(needle)
261
+ local m = string.len(haystack)
262
+
263
+ if n == 0 or m == 0 or m > config.maxMatchLength or n > m then
264
+ return MIN_SCORE
265
+ elseif Fzy.isPerfectMatch(config, needle, haystack) then
266
+ return MAX_SCORE
267
+ else
268
+ local D = {}
269
+ local M = {}
270
+ compute(config, needle, haystack, D, M)
271
+ return M[n][m]
272
+ end
273
+ end
274
+
275
+
276
+ --[=[
277
+ Compute the locations where fzy matches a string.
278
+
279
+ Determine where each character of the `needle` is matched to the `haystack`
280
+ in the optimal match.
281
+
282
+ @param config FzyConfig
283
+ @param needle string -- must be a subequence of `haystack`, or the result is undefined.
284
+ @param haystack string
285
+ @return { int } -- indices, where `indices[n]` is the location of the `n`th character of `needle` in `haystack`.
286
+ @return number -- the same matching score returned by `score`
287
+ ]=]
288
+ function Fzy.positions(config, needle: string, haystack: string)
289
+ local n = string.len(needle)
290
+ local m = string.len(haystack)
291
+
292
+ if n == 0 or m == 0 or m > config.maxMatchLength or n > m then
293
+ return {}, MIN_SCORE
294
+ elseif Fzy.isPerfectMatch(config, needle, haystack) then
295
+ local consecutive = {}
296
+ for i = 1, n do
297
+ consecutive[i] = i
298
+ end
299
+ return consecutive, MAX_SCORE
300
+ end
301
+
302
+ local D = {}
303
+ local M = {}
304
+ compute(config, needle, haystack, D, M)
305
+
306
+ local positions = {}
307
+ local match_required = false
308
+ local j = m
309
+ for i = n, 1, -1 do
310
+ while j >= 1 do
311
+ if D[i][j] ~= MIN_SCORE and (match_required or D[i][j] == M[i][j]) then
312
+ match_required = (i ~= 1) and (j ~= 1) and (M[i][j] == D[i - 1][j - 1] + config.consecutiveMatchScore)
313
+ positions[i] = j
314
+ j = j - 1
315
+ break
316
+ else
317
+ j = j - 1
318
+ end
319
+ end
320
+ end
321
+
322
+ return positions, M[n][m]
323
+ end
324
+
325
+ --[=[
326
+ Apply [Fzy.hasMatch] and [Fzy.positions] to an array of haystacks.
327
+
328
+ Returns an array with one entry per matching line in `haystacks`,
329
+ each entry giving the index of the line in `haystacks` as well as
330
+ the equivalent to the return value of `positions` for that line.
331
+
332
+ @param config FzyConfig
333
+ @param needle string
334
+ @param haystacks { string }
335
+ @return {{idx, positions, score}, ...}
336
+ ]=]
337
+ function Fzy.filter(config, needle: string, haystacks: string)
338
+ local result = {}
339
+
340
+ for i, line in ipairs(haystacks) do
341
+ if Fzy.hasMatch(config, needle, line) then
342
+ local p, s = Fzy.positions(config, needle, line)
343
+ table.insert(result, {i, p, s})
344
+ end
345
+ end
346
+
347
+ return result
348
+ end
349
+
350
+ --[=[
351
+ The lowest value returned by `score`.
352
+
353
+ In two special cases:
354
+ - an empty `needle`, or
355
+ - a `needle` or `haystack` larger than than [Fzy.getMaxLength],
356
+
357
+ the [Fzy.score] function will return this exact value, which can be used as a
358
+ sentinel. This is the lowest possible score.
359
+
360
+ @return number
361
+ ]=]
362
+ function Fzy.getMinScore(): number
363
+ return MIN_SCORE
364
+ end
365
+
366
+ --[=[
367
+ The score returned for exact matches. This is the highest possible score.
368
+
369
+ @return number
370
+ ]=]
371
+ function Fzy.getMaxScore(): number
372
+ return MAX_SCORE
373
+ end
374
+
375
+ --[=[
376
+ The maximum size for which `fzy` will evaluate scores.
377
+
378
+ @param config FzyConfig
379
+ @return number
380
+ ]=]
381
+ function Fzy.getMaxLength(config): number
382
+ assert(Fzy.isFzyConfig(config), "Bad config")
383
+
384
+ return config.maxMatchLength
385
+ end
386
+
387
+ --[=[
388
+ The minimum score returned for normal matches.
389
+
390
+ For matches that don't return [Fzy.getMinScore], their score will be greater
391
+ than than this value.
392
+
393
+ @param config FzyConfig
394
+ @return number
395
+ ]=]
396
+ function Fzy.getScoreFloor(config): number
397
+ assert(Fzy.isFzyConfig(config), "Bad config")
398
+
399
+ return config.maxMatchLength * config.gapInnerScore
400
+ end
401
+
402
+ --[=[
403
+ The maximum score for non-exact matches.
404
+
405
+ For matches that don't return [Fzy.getMaxScore], their score will be less than
406
+ this value.
407
+
408
+ @param config FzyConfig
409
+ @return number
410
+ ]=]
411
+ function Fzy.getScoreCeiling(config): number
412
+ assert(Fzy.isFzyConfig(config), "Bad config")
413
+
414
+ return config.maxMatchLength * config.consecutiveMatchScore
415
+ end
416
+
417
+ return Fzy
@@ -0,0 +1,253 @@
1
+ --[[
2
+ Tests for fzy-lua
3
+
4
+ See:
5
+ https://github.com/swarn/fzy-lua
6
+ https://github.com/jhawthorn/fzy
7
+
8
+ @class Fzy.spec.lua
9
+ ]]
10
+
11
+ --[[
12
+ The MIT License (MIT)
13
+
14
+ Copyright (c) 2020 Seth Warn
15
+
16
+ Permission is hereby granted, free of charge, to any person obtaining a copy
17
+ of this software and associated documentation files (the "Software"), to deal
18
+ in the Software without restriction, including without limitation the rights
19
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20
+ copies of the Software, and to permit persons to whom the Software is
21
+ furnished to do so, subject to the following conditions:
22
+
23
+ The above copyright notice and this permission notice shall be included in
24
+ all copies or substantial portions of the Software.
25
+
26
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32
+ THE SOFTWARE.
33
+ ]]
34
+
35
+ local EPSILON = 0.000001
36
+
37
+ local Fzy = require(script.Parent.Fzy)
38
+ local MIN_SCORE = Fzy.getMinScore()
39
+ local MAX_SCORE = Fzy.getMaxScore()
40
+
41
+ local function compareTables(a, b)
42
+ for key, value in pairs(a) do
43
+ if b[key] ~= value then
44
+ return false
45
+ end
46
+ end
47
+
48
+ for key, value in pairs(b) do
49
+ if a[key] ~= value then
50
+ return false
51
+ end
52
+ end
53
+
54
+ return true
55
+ end
56
+
57
+ return function()
58
+ local config = Fzy.createConfig({
59
+ caseSensitive = false;
60
+ })
61
+ local caseSensitiveConfig = Fzy.createConfig({
62
+ caseSensitive = true;
63
+ })
64
+ local MATCH_MAX_LENGTH = Fzy.getMaxLength(config)
65
+
66
+ describe("matching", function()
67
+ it("exact matches", function()
68
+ expect(Fzy.hasMatch(config, "a", "a")).to.equal(true)
69
+ expect(Fzy.hasMatch(caseSensitiveConfig, "a", "a")).to.equal(true)
70
+ expect(Fzy.hasMatch(caseSensitiveConfig, "A", "A")).to.equal(true)
71
+ expect(Fzy.hasMatch(config, "a.bb", "a.bb")).to.equal(true)
72
+ end)
73
+ it("handles special characters", function()
74
+ expect(Fzy.hasMatch(config, "\\", "\\")).to.equal(true)
75
+ expect(Fzy.hasMatch(config, "/", "/")).to.equal(true)
76
+ expect(Fzy.hasMatch(config, "[", "[")).to.equal(true)
77
+ expect(Fzy.hasMatch(config, "%", "%")).to.equal(true)
78
+ end)
79
+ it("ignores case by default", function()
80
+ expect(Fzy.hasMatch(config, "AbB", "abb")).to.equal(true)
81
+ expect(Fzy.hasMatch(config, "abb", "ABB")).to.equal(true)
82
+ end)
83
+ it("is case-sensitive when requested", function()
84
+ expect(Fzy.hasMatch(caseSensitiveConfig, "AbB", "abb")).to.equal(false)
85
+ expect(Fzy.hasMatch(caseSensitiveConfig, "abb", "ABB")).to.equal(false)
86
+ end)
87
+ it("partial matches", function()
88
+ expect(Fzy.hasMatch(config, "a", "ab")).to.equal(true)
89
+ expect(Fzy.hasMatch(config, "a", "ba")).to.equal(true)
90
+ expect(Fzy.hasMatch(config, "aba", "baabbaab")).to.equal(true)
91
+ end)
92
+ it("with delimiters between", function()
93
+ expect(Fzy.hasMatch(config, "abc", "a|b|c")).to.equal(true)
94
+ end)
95
+ it("with empty query", function()
96
+ expect(Fzy.hasMatch(config, "", "")).to.equal(true)
97
+ expect(Fzy.hasMatch(config, "", "a")).to.equal(true)
98
+ end)
99
+ it("rejects non-matches", function()
100
+ expect(Fzy.hasMatch(config, "a", "")).to.equal(false)
101
+ expect(Fzy.hasMatch(config, "a", "b")).to.equal(false)
102
+ expect(Fzy.hasMatch(config, "aa", "a")).to.equal(false)
103
+ expect(Fzy.hasMatch(config, "ba", "a")).to.equal(false)
104
+ expect(Fzy.hasMatch(config, "ab", "a")).to.equal(false)
105
+ end)
106
+ end)
107
+
108
+ describe("scoring", function()
109
+ local function compare(queryStr, a, b)
110
+ return Fzy.score(config, queryStr, a) > Fzy.score(config, queryStr, b)
111
+ end
112
+
113
+ it("prefers beginnings of words", function()
114
+ expect(compare("amor", "app/models/order", "app/models/zrder")).to.equal(true)
115
+ expect(compare("amor", "app models order", "app models zrder")).to.equal(true)
116
+ expect(compare("amor", "appModelsOrder", "appModelsZrder")).to.equal(true)
117
+ expect(compare("amor", "app\\models\\order", "app\\models\\zrder")).to.equal(true)
118
+ expect(compare("a", ".a", "ba")).to.equal(true)
119
+ end)
120
+ it("prefers consecutive letters", function()
121
+ expect(compare("amo", "app/models/foo", "app/m/foo")).to.equal(true)
122
+ expect(compare("amo", "app/models/foo", "app/m/o")).to.equal(true)
123
+ expect(compare("erf", "perfect", "terrific")).to.equal(true)
124
+ expect(compare("abc", "*ab**c*", "*a*b*c*")).to.equal(true)
125
+ end)
126
+ it("prefers contiguous over letter following period", function()
127
+ expect(compare("gemfil", "Gemfile", "Gemfile.lock")).to.equal(true)
128
+ end)
129
+ it("prefers shorter matches", function()
130
+ expect(compare("abce", "abcdef", "abc de")).to.equal(true)
131
+ expect(compare("abc", " a b c ", " a b c ")).to.equal(true)
132
+ expect(compare("abc", " a b c ", " a b c ")).to.equal(true)
133
+ expect(compare("aa", "*a*a*", "*a**a")).to.equal(true)
134
+ end)
135
+ it("prefers shorter candidates", function()
136
+ expect(compare("test", "tests", "testing")).to.equal(true)
137
+ end)
138
+ it("prefers matches at the beginning", function()
139
+ expect(compare("ab", "abbb", "babb")).to.equal(true)
140
+ expect(compare("test", "testing", "/testing")).to.equal(true)
141
+ end)
142
+ it("returns the max score for exact matches", function()
143
+ expect(Fzy.score(config, "abc", "abc")).to.equal(MAX_SCORE)
144
+ expect(Fzy.score(config, "aBc", "abC")).to.equal(MAX_SCORE)
145
+ end)
146
+ it("returns the min score for empty queries", function()
147
+ expect(Fzy.score(config, "", "")).to.equal(MIN_SCORE)
148
+ expect(Fzy.score(config, "", "a")).to.equal(MIN_SCORE)
149
+ expect(Fzy.score(config, "", "bb")).to.equal(MIN_SCORE)
150
+ end)
151
+ it("rewards matching slashes correctly", function()
152
+ expect(compare("a", "*/a", "**a")).to.equal(true)
153
+ expect(compare("a", "*\\a", "**a")).to.equal(true)
154
+ expect(compare("a", "**/a", "*a")).to.equal(true)
155
+ expect(compare("a", "**\\a", "*a")).to.equal(true)
156
+ expect(compare("aa", "a/aa", "a/a")).to.equal(true)
157
+ end)
158
+ it("rewards matching camelCase correctly", function()
159
+ expect(compare("a", "bA", "ba")).to.equal(true)
160
+ expect(compare("a", "baA", "ba")).to.equal(true)
161
+ end)
162
+ it("scores in the prescribed bounds", function()
163
+ local aaa = string.rep("a", MATCH_MAX_LENGTH)
164
+ local aa = string.rep("a", MATCH_MAX_LENGTH - 1)
165
+ expect(Fzy.getScoreCeiling(config) > Fzy.score(config, aa, aaa)).to.equal(true)
166
+ local aba = "a" .. string.rep("b", MATCH_MAX_LENGTH - 2) .. "a"
167
+ expect(Fzy.getScoreFloor(config) < Fzy.score(config, "aa", aba)).to.equal(true)
168
+ end)
169
+ it("ignores really long strings", function()
170
+ local longstring = string.rep("a", MATCH_MAX_LENGTH + 1)
171
+ expect(Fzy.score(config, "aa", longstring)).to.equal(MIN_SCORE)
172
+ expect(Fzy.score(config, longstring, "aa")).to.equal(MIN_SCORE)
173
+ expect(Fzy.score(config, longstring, longstring)).to.equal(MIN_SCORE)
174
+ end)
175
+ it("respects the case-sensitive argument", function()
176
+ expect(Fzy.score(config, "aa", "bbbabab")).to.be.near(Fzy.score(caseSensitiveConfig, "AA", "aaBABAB"), EPSILON)
177
+ expect(Fzy.score(config, "bab", "bAacb")).to.be.near(Fzy.score(caseSensitiveConfig, "bAb", "bAaBb"), EPSILON)
178
+ end)
179
+ end)
180
+
181
+ describe("positioning", function()
182
+ it("favors consecutive positions", function()
183
+ expect(compareTables({1, 5, 6}, Fzy.positions(config, "amo", "app/models/foo"))).to.be.equal(true)
184
+ end)
185
+ it("favors word beginnings", function()
186
+ expect(compareTables({1, 5, 12, 13}, Fzy.positions(config, "amor", "app/models/order"))).to.be.equal(true)
187
+ expect(compareTables({3, 4}, Fzy.positions(config, "aa", "baAa"))).to.be.equal(true)
188
+ expect(compareTables({4}, Fzy.positions(config, "a", "ba.a"))).to.be.equal(true)
189
+ end)
190
+ it("works when there are no bonuses", function()
191
+ expect(compareTables({2, 4}, Fzy.positions(config, "as", "tags"))).to.be.equal(true)
192
+ expect(compareTables({3, 8}, Fzy.positions(config, "as", "examples.txt"))).to.be.equal(true)
193
+ end)
194
+ it("favors smaller groupings of positions", function()
195
+ expect(compareTables({3, 5, 7}, Fzy.positions(config, "abc", "a/a/b/c/c"))).to.be.equal(true)
196
+ expect(compareTables({3, 5, 7}, Fzy.positions(config, "abc", "a\\a\\b\\c\\c"))).to.be.equal(true)
197
+ expect(compareTables({4, 6, 8}, Fzy.positions(config, "abc", "*a*a*b*c*c"))).to.be.equal(true)
198
+ expect(compareTables({3, 5}, Fzy.positions(config, "ab", "caacbbc"))).to.be.equal(true)
199
+ end)
200
+ it("handles exact matches", function()
201
+ expect(compareTables({1, 2, 3}, Fzy.positions(config, "foo", "foo"))).to.be.equal(true)
202
+ end)
203
+ it("ignores empty requests", function()
204
+ expect(compareTables({}, Fzy.positions(config, "", ""))).to.be.equal(true)
205
+ expect(compareTables({}, Fzy.positions(config, "", "foo"))).to.be.equal(true)
206
+ end)
207
+ it("ignores really long strings", function()
208
+ local longstring = string.rep("a", MATCH_MAX_LENGTH + 1)
209
+ expect(Fzy.score(config, "aa", longstring)).to.equal(MIN_SCORE)
210
+ expect(Fzy.score(config, longstring, "aa")).to.equal(MIN_SCORE)
211
+ expect(Fzy.score(config, longstring, longstring)).to.equal(MIN_SCORE)
212
+ end)
213
+ it("is case-sensitive when requested", function()
214
+ expect(compareTables({2, 5}, Fzy.positions(caseSensitiveConfig, "AB", "aAabBb", true))).to.be.equal(true)
215
+ end)
216
+ it("returns the same score as `score()`", function()
217
+ local _, s = Fzy.positions(config, "ab", "aaabbb")
218
+ expect(Fzy.score(config, "ab", "aaabbb")).to.equal(s)
219
+ _, s = Fzy.positions(config, "aaa", "aaa")
220
+ expect(Fzy.score(config, "aaa", "aaa")).to.equal(s)
221
+ _, s = Fzy.positions(config, "", "aaa")
222
+ expect(Fzy.score(config, "", "aaa")).to.equal(s)
223
+ end)
224
+ end)
225
+
226
+ describe("filtering", function()
227
+ it("repeats application of hasMatch and positions", function()
228
+
229
+ -- compare the result of `filter` with repeated calls to `positions`
230
+ local function check_filter(needle, haystacks, case)
231
+ local result = Fzy.filter(config, needle, haystacks, case)
232
+ local r = 0
233
+ for i, line in ipairs(haystacks) do
234
+ local match = Fzy.hasMatch(config, needle, line, case)
235
+ if match then
236
+ r = r + 1
237
+ expect(i).to.equal(result[r][1])
238
+ local p, s = Fzy.positions(config, needle, line, case)
239
+ expect(compareTables(p, result[r][2])).to.equal(true)
240
+ expect(s).to.equal(result[r][3])
241
+ end
242
+ end
243
+ expect(#result).to.equal(r)
244
+ end
245
+
246
+ check_filter("a", {"a", "A", "aa", "b", ""})
247
+ check_filter("a", {"a", "A", "aa", "b", ""}, true)
248
+ check_filter("", {"a", "A", "aa", "b", ""})
249
+ check_filter("a", {"b"})
250
+ check_filter("a", {})
251
+ end)
252
+ end)
253
+ end
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "node_modules",
3
+ "globIgnorePaths": [ "**/.package-lock.json" ],
4
+ "tree": {
5
+ "$path": { "optional": "../node_modules" }
6
+ }
7
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "FzyTest",
3
+ "tree": {
4
+ "$className": "DataModel",
5
+ "ServerScriptService": {
6
+ "fzy": {
7
+ "$path": ".."
8
+ }
9
+ }
10
+ }
11
+ }