From 12ce2e6ed96e7cc1f1dd983e8595c9e3b1ec2922 Mon Sep 17 00:00:00 2001 From: Donnie Date: Tue, 13 Apr 2021 11:41:58 -0700 Subject: [PATCH] CSS tweaks --- src/common/string/filter/sequence-matching.ts | 16 +++++---- src/dialogs/quick-bar/ha-quick-bar.ts | 6 ++-- .../common/string/sequence_matching.test.ts | 35 +++++++++++++------ 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/src/common/string/filter/sequence-matching.ts b/src/common/string/filter/sequence-matching.ts index 297de8854a..cc18217375 100644 --- a/src/common/string/filter/sequence-matching.ts +++ b/src/common/string/filter/sequence-matching.ts @@ -12,12 +12,14 @@ import { unsafeHTML } from "lit-html/directives/unsafe-html"; * @return {number} Score representing how well the word matches the filter. Return of 0 means no match. */ -export const fuzzySequentialMatch = ( +type FuzzySequentialMatcher = ( filter: string, item: ScorableTextItem -) => { +) => ScorableTextItem | undefined; + +export const fuzzySequentialMatch: FuzzySequentialMatcher = (filter, item) => { let topScore = Number.NEGATIVE_INFINITY; - const decoratedWords: TemplateResult[][] = []; + const decoratedStrings: TemplateResult[][] = []; for (const word of item.strings) { const scores = fuzzyScore( @@ -30,7 +32,7 @@ export const fuzzySequentialMatch = ( true ); - decoratedWords.push(decorateMatch(word, scores)); + decoratedStrings.push(decorateMatch(word, scores)); if (!scores) { continue; @@ -53,7 +55,7 @@ export const fuzzySequentialMatch = ( return { score: topScore, strings: item.strings, - decoratedWords, + decoratedStrings, }; }; @@ -73,7 +75,7 @@ export const fuzzySequentialMatch = ( export interface ScorableTextItem { score?: number; strings: string[]; - decoratedWords?: TemplateResult[][]; + decoratedStrings?: TemplateResult[][]; } type FuzzyFilterSort = ( @@ -87,7 +89,7 @@ export const fuzzyFilterSort: FuzzyFilterSort = (filter, items) => { const match = fuzzySequentialMatch(filter, item); item.score = match?.score; - item.decoratedWords = match?.decoratedWords; + item.decoratedStrings = match?.decoratedStrings; return item; }) diff --git a/src/dialogs/quick-bar/ha-quick-bar.ts b/src/dialogs/quick-bar/ha-quick-bar.ts index 75f79a0947..780e79ff62 100644 --- a/src/dialogs/quick-bar/ha-quick-bar.ts +++ b/src/dialogs/quick-bar/ha-quick-bar.ts @@ -258,7 +258,7 @@ export class QuickBar extends LitElement { class="entity" slot="graphic" >`} - ${item.decoratedWords ? item.decoratedWords[0] : item.primaryText} { + pattern: string; + expected: ScorableTextItem; +}; + +const createExpectation: CreateExpectation = ( + pattern, + score, + strings = [], + decoratedStrings = [] +) => ({ + pattern, + expected: { + score, + strings, + decoratedStrings, + }, +}); + describe("fuzzySequentialMatch", () => { const item: ScorableTextItem = { strings: ["automation.ticker", "Stocks"], }; - const createExpectation: ( - pattern, - expected - ) => { - pattern: string; - expected: string | number | undefined; - } = (pattern, expected) => ({ - pattern, - expected, - }); - const shouldMatchEntity = [ createExpectation("automation.ticker", 131), createExpectation("automation.ticke", 121),