UNPKG

@theia/core

Version:

Theia is a cloud & desktop IDE framework implemented in TypeScript.

109 lines 4.83 kB
"use strict"; // ***************************************************************************** // Copyright (C) 2018 TypeFox and others. // // This program and the accompanying materials are made available under the // terms of the Eclipse Public License v. 2.0 which is available at // http://www.eclipse.org/legal/epl-2.0. // // This Source Code may also be made available under the following Secondary // Licenses when the conditions for such availability set forth in the Eclipse // Public License v. 2.0 are satisfied: GNU General Public License, version 2 // with the GNU Classpath Exception which is available at // https://www.gnu.org/software/classpath/license.html. // // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 // ***************************************************************************** Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const fuzzy_search_1 = require("./fuzzy-search"); describe('fuzzy-search', () => { [ { pattern: 'a', items: ['alma'], expected: [ { item: 'alma', ranges: [ { offset: 0, length: 1 } ] } ] }, { pattern: 'a', items: ['körte'], expected: [] }, { pattern: 'bcn', items: ['baconing', 'narwhal', 'a mighty bear canoe'], expected: [ { item: 'baconing', ranges: [ { offset: 0, length: 1 }, { offset: 2, length: 1 }, { offset: 4, length: 1 } ] }, { item: 'a mighty bear canoe', ranges: [ { offset: 9, length: 1 }, { offset: 14, length: 1 }, { offset: 16, length: 1 } ] } ] } ].forEach(test => { const { pattern, items, expected } = test; it(`should match ${expected.length} item${expected.length === 1 ? '' : 's'} when filtering [${items.join(', ')}] with pattern: '${pattern}'`, async () => { expectSearch(await search(pattern, items), expected); }); }); [ // "con" prefix matches (configs, CONTRIBUTING.MD) first, then substring matches (base.tsconfig.json, tsconfig.json), then fuzzy-only (base.nyc.json) ['con', ['configs', 'base.tsconfig.json', 'tsconfig.json', 'base.nyc.json', 'CONTRIBUTING.MD'], ['configs', 'CONTRIBUTING.MD', 'base.tsconfig.json', 'tsconfig.json', 'base.nyc.json']], ['bcn', ['baconing', 'narwhal', 'a mighty bear canoe'], ['baconing', 'a mighty bear canoe']] ].forEach(test => { const [pattern, items, expected] = test; it(`should match the order of items after the filtering with pattern: '${pattern}'`, async () => { expectOrder(await search(pattern, items), expected || items); }); }); it('should rank substring matches before fuzzy-only matches', async () => { const results = await search('font', ['reformatting', 'fontSize', 'fontFamily']); // "reformatting" contains f-o-n-t scattered but not as substring; fontSize/fontFamily do expectOrder(results, ['fontSize', 'fontFamily']); }); it('should preserve original order for equal-score fuzzy-only matches', async () => { // Both are fuzzy-only matches for "bcn" with different scores from the fuzzy library; // when scores are equal, original array order (index) should be preserved. const results = await search('ab', ['xab', 'yab']); expectOrder(results, ['xab', 'yab']); }); it('should highlight contiguous substring range instead of scattered fuzzy ranges', async () => { const results = await search('works', ['browser-only-workspace-server.ts']); (0, chai_1.expect)(results).to.have.length(1); // "works" appears at index 13 in "browser-only-workspace-server.ts" (0, chai_1.expect)(results[0].ranges).to.deep.equal([{ offset: 13, length: 5 }]); }); function expectOrder(actual, expected) { (0, chai_1.expect)(actual.map(result => result.item)).to.be.deep.equal(expected); } function expectSearch(actual, expected) { (0, chai_1.expect)(actual).to.be.deep.equal(expected); } async function search(pattern, items) { return new fuzzy_search_1.FuzzySearch().filter({ items, pattern, transform: arg => arg }); } }); //# sourceMappingURL=fuzzy-search.spec.js.map