1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | const chai_1 = require("chai");
|
19 | const fuzzy_search_1 = require("./fuzzy-search");
|
20 | describe('fuzzy-search', () => {
|
21 | [
|
22 | {
|
23 | pattern: 'a',
|
24 | items: ['alma'],
|
25 | expected: [
|
26 | {
|
27 | item: 'alma',
|
28 | ranges: [
|
29 | { offset: 0, length: 1 }
|
30 | ]
|
31 | }
|
32 | ]
|
33 | },
|
34 | {
|
35 | pattern: 'a',
|
36 | items: ['körte'],
|
37 | expected: []
|
38 | },
|
39 | {
|
40 | pattern: 'bcn',
|
41 | items: ['baconing', 'narwhal', 'a mighty bear canoe'],
|
42 | expected: [
|
43 | {
|
44 | item: 'baconing',
|
45 | ranges: [
|
46 | { offset: 0, length: 1 },
|
47 | { offset: 2, length: 1 },
|
48 | { offset: 4, length: 1 }
|
49 | ]
|
50 | },
|
51 | {
|
52 | item: 'a mighty bear canoe',
|
53 | ranges: [
|
54 | { offset: 9, length: 1 },
|
55 | { offset: 14, length: 1 },
|
56 | { offset: 16, length: 1 }
|
57 | ]
|
58 | }
|
59 | ]
|
60 | }
|
61 | ].forEach(test => {
|
62 | const { pattern, items, expected } = test;
|
63 | it(`should match ${expected.length} item${expected.length === 1 ? '' : 's'} when filtering [${items.join(', ')}] with pattern: '${pattern}'`, async () => {
|
64 | expectSearch(await search(pattern, items), expected);
|
65 | });
|
66 | });
|
67 | [
|
68 | ['con', ['configs', 'base.tsconfig.json', 'tsconfig.json', 'base.nyc.json', 'CONTRIBUTING.MD']],
|
69 | ['bcn', ['baconing', 'narwhal', 'a mighty bear canoe'], ['baconing', 'a mighty bear canoe']]
|
70 | ].forEach(test => {
|
71 | const [pattern, items, expected] = test;
|
72 | it(`should match the order of items after the filtering with pattern: '${pattern}'`, async () => {
|
73 | expectOrder(await search(pattern, items), expected || items);
|
74 | });
|
75 | });
|
76 | function expectOrder(actual, expected) {
|
77 | (0, chai_1.expect)(actual.map(result => result.item)).to.be.deep.equal(expected);
|
78 | }
|
79 | function expectSearch(actual, expected) {
|
80 | (0, chai_1.expect)(actual).to.be.deep.equal(expected);
|
81 | }
|
82 | async function search(pattern, items) {
|
83 | return new fuzzy_search_1.FuzzySearch().filter({
|
84 | items,
|
85 | pattern,
|
86 | transform: arg => arg
|
87 | });
|
88 | }
|
89 | });
|
90 |
|
\ | No newline at end of file |