| 1 |
import { test } from "uvu"; |
| 2 |
import assert from "uvu/assert"; |
| 3 |
|
| 4 |
import { fuzzyfilter, fuzzytest, fuzzywrap } from "../src/lib/fuzzy"; |
| 5 |
|
| 6 |
test("fuzzy test", () => { |
| 7 |
assert.ok(fuzzytest("asdf", "asdfasdf")); |
| 8 |
assert.ok(fuzzytest("asdf", "ASDFASDF")); |
| 9 |
// "smart-case" matching |
| 10 |
assert.ok(fuzzytest("Asdf", "ASDFASDF") < fuzzytest("asdf", "ASDFASDF")); |
| 11 |
assert.ok(fuzzytest("ASDF", "ASDFASDF") === fuzzytest("asdf", "ASDFASDF")); |
| 12 |
|
| 13 |
assert.ok(!fuzzytest("Asdf", "asdfasdf")); |
| 14 |
assert.ok(fuzzytest("asdf", "a;lks;lk;lkd;lk;flkj;l")); |
| 15 |
assert.ok(!fuzzytest("asdf", "sdfsdf")); |
| 16 |
assert.ok(!fuzzytest("a", "sdfsdf")); |
| 17 |
}); |
| 18 |
|
| 19 |
test("fuzzy filter", () => { |
| 20 |
assert.equal(fuzzyfilter("", ["asdfasdf", "a"]), ["asdfasdf", "a"]); |
| 21 |
assert.equal(fuzzyfilter("asdf", ["asdfasdf", "nomatch"]), ["asdfasdf"]); |
| 22 |
assert.equal(fuzzyfilter("asdf", ["assdfsdf", "asdfasdf", "nomatch"]), [ |
| 23 |
"asdfasdf", |
| 24 |
"assdfsdf", |
| 25 |
]); |
| 26 |
assert.equal( |
| 27 |
fuzzyfilter("asdf", [ |
| 28 |
"test", |
| 29 |
"asdfasdf", |
| 30 |
"asdxf", |
| 31 |
"asxxdf", |
| 32 |
"nomatch", |
| 33 |
"asdf", |
| 34 |
]), |
| 35 |
["asdfasdf", "asdf", "asdxf", "asxxdf"], |
| 36 |
); |
| 37 |
}); |
| 38 |
|
| 39 |
test("fuzzy wap", () => { |
| 40 |
assert.equal(fuzzywrap("", "tenotest"), [["text", "tenotest"]]); |
| 41 |
assert.equal(fuzzywrap("", "<>tenotest"), [["text", "<>tenotest"]]); |
| 42 |
|
| 43 |
assert.equal(fuzzywrap("test", "tenotest"), [ |
| 44 |
["text", "teno"], |
| 45 |
["match", "test"], |
| 46 |
]); |
| 47 |
// no match for case sensitive pattern: |
| 48 |
assert.equal(fuzzywrap("tesT", "test"), [["text", "test"]]); |
| 49 |
assert.equal(fuzzywrap("sdf", "nomatch"), [["text", "nomatch"]]); |
| 50 |
assert.equal(fuzzywrap("test", "tetest"), [ |
| 51 |
["text", "te"], |
| 52 |
["match", "test"], |
| 53 |
]); |
| 54 |
assert.equal(fuzzywrap("test", "teTEST"), [ |
| 55 |
["text", "te"], |
| 56 |
["match", "TEST"], |
| 57 |
]); |
| 58 |
assert.equal(fuzzywrap("a", "asdfasdf"), [ |
| 59 |
["match", "a"], |
| 60 |
["text", "sdfasdf"], |
| 61 |
]); |
| 62 |
assert.equal(fuzzywrap("as", "asdfasdf"), [ |
| 63 |
["match", "as"], |
| 64 |
["text", "dfasdf"], |
| 65 |
]); |
| 66 |
assert.equal(fuzzywrap("as", "as"), [["match", "as"]]); |
| 67 |
assert.equal(fuzzywrap("te", "tae"), [ |
| 68 |
["match", "t"], |
| 69 |
["text", "a"], |
| 70 |
["match", "e"], |
| 71 |
]); |
| 72 |
assert.equal(fuzzywrap("te", "ta<e"), [ |
| 73 |
["match", "t"], |
| 74 |
["text", "a<"], |
| 75 |
["match", "e"], |
| 76 |
]); |
| 77 |
assert.equal(fuzzywrap("as", "<span>as"), [ |
| 78 |
["text", "<span>"], |
| 79 |
["match", "as"], |
| 80 |
]); |
| 81 |
}); |
| 82 |
|
| 83 |
test.run(); |