UNPKG

@ndn/packet

Version:

NDNts: Network Layer Packets

26 lines (25 loc) 881 B
import { toHex } from "@ndn/util"; /** * Perform name longest prefix match on a container of entries. * @typeParam T - Entry type, which must not be `undefined`. * @param name - Lookup target name. * @param get - Callback function to retrieve entry by name prefix TLV-VALUE in hexadecimal format. * @returns Matched entries. * The first result is the longest prefix match. Subsequent results are matches on successively * shorter prefixes. The caller may early-return the iterator to ignore subsequent results. */ export function* lpm(name, get) { const prefixes = [""]; let s = ""; for (const comp of name.comps) { s += toHex(comp.tlv); prefixes.push(s); } let prefix; while ((prefix = prefixes.pop()) !== undefined) { const entry = get(prefix); if (entry !== undefined) { yield entry; } } }