From 078856e3045d83bb28f412ce13f873e410da6202 Mon Sep 17 00:00:00 2001 From: Matt Gibson Date: Thu, 13 Mar 2025 18:30:53 -0700 Subject: [PATCH] Improve match type UX --- libs/common/src/vault/search/parse.ts | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/libs/common/src/vault/search/parse.ts b/libs/common/src/vault/search/parse.ts index 3476a0f6f6f..b535a2d0afa 100644 --- a/libs/common/src/vault/search/parse.ts +++ b/libs/common/src/vault/search/parse.ts @@ -1,6 +1,6 @@ import { Parser, Grammar } from "nearley"; -import { UriMatchStrategy } from "../../models/domain/domain-service"; +import { UriMatchStrategy, UriMatchStrategySetting } from "../../models/domain/domain-service"; import { Utils } from "../../platform/misc/utils"; import { CardLinkedId, CipherType, FieldType, LinkedIdType, LoginLinkedId } from "../enums"; import { CipherView } from "../models/view/cipher.view"; @@ -349,16 +349,13 @@ function handleNode(node: AstNode): ProcessInstructions { }; } else if (isWebsiteMatchFilter(node)) { const websiteTest = termToRegexTest(node.website); - const matchTest = fieldNameToRegexTest(node.matchType); - const matchTypes = Object.keys(UriMatchStrategy) - .filter((key) => matchTest.test(key)) - .map((key) => UriMatchStrategy[key as keyof typeof UriMatchStrategy]); + const matchTest = termToRegexTest(node.matchType); return { filter: (context) => ({ ...context, ciphers: context.ciphers.filter((cipher) => cipher?.login?.uris?.some( - (uri) => matchTypes.includes(uri.match) && websiteTest.test(uri.uri), + (uri) => matchHostMatchType(uri.match, matchTest) && websiteTest.test(uri.uri), ), ), }), @@ -375,6 +372,21 @@ function handleNode(node: AstNode): ProcessInstructions { } } +function matchHostMatchType( + cipherVal: UriMatchStrategySetting | null, + queryMatch: RegExp, +): boolean { + if (queryMatch.test("default")) { + // default match type is stored as null + return cipherVal == null; + } + + const matchTypes = Object.keys(UriMatchStrategy) + .filter((key) => queryMatch.test(key)) + .map((key) => UriMatchStrategy[key as keyof typeof UriMatchStrategy]); + return cipherVal != null && matchTypes.includes(cipherVal); +} + /** * Match a string against an enum value. The matching string is sent in twice to match the enum in both directions, * number -> string and string -> number.