UNPKG

3.44 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2018 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18const chai_1 = require("chai");
19const fuzzy_search_1 = require("./fuzzy-search");
20describe('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//# sourceMappingURL=fuzzy-search.spec.js.map
\No newline at end of file