Rush StackShopBlogEvents
Skip to main content

Home > @rushstack/lookup-by-path > IReadonlyLookupByPath > findLongestPrefixMatch

IReadonlyLookupByPath.findLongestPrefixMatch() method

This API is provided as a beta preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.

Searches for the item for which the recorded prefix is the longest matching prefix of query. Obtains both the item and the length of the matched prefix, so that the remainder of the path can be extracted.

Signature:

findLongestPrefixMatch(query: string, delimiter?: string): IPrefixMatch<TItem> | undefined;

Parameters

Parameter

Type

Description

query

string

delimiter

string

(Optional)

Returns:

IPrefixMatch<TItem> | undefined

the found item and the length of the matched prefix, or undefined if no item was found

Example

const trie = new LookupByPath([['foo', 1], ['foo/bar', 2]]);
trie.findLongestPrefixMatch('foo/baz'); // returns { item: 1, index: 3 }
trie.findLongestPrefixMatch('foo/bar/baz'); // returns { item: 2, index: 7 }