npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mangar2/topicmatch

v2.1.2

Published

Checks for pattern matching a MQTT style topic

Downloads

5

Readme

Abstract

Contents

Meta

Global functions

_deletePatternRec

_deletePatternRec (topicChunks, node, index)

Recursively removes a pattern ( exact match ) from the tree

_deletePatternRec Parameters

| Name | Type | Attribute | Default | Description | | ---------- | ------------ | ------------ | ------------ | ----------------- | | topicChunks | Array.<string> | | | topic chunks to search for | | | node | Object | | | current node in the pattern tree | | | index | number | optional | 0 | current index ( depth ) in the topic array | |

_getBestMatchRec

_getBestMatchRec (isBetter, topicChunks, node, index)

Searches for a topic in a pattern tree , returns the best match ( match with highest value )

_getBestMatchRec Parameters

| Name | Type | Attribute | Default | Description | | ---------- | ------------ | ------------ | ------------ | ----------------- | | isBetter | function(a, b | | | returns true , if a is better than b | | | topicChunks | Array.<string> | | | topic chunks to search for | | | node | Object | | | current node in the pattern tree | | | index | number | optional | 0 | current index ( depth ) in the topic array | |

_getFirstMatchRec

_getFirstMatchRec (topicChunks, node, index)

Searches for a topic in a pattern tree , returns the first match

_getFirstMatchRec Parameters

| Name | Type | Attribute | Default | Description | | ---------- | ------------ | ------------ | ------------ | ----------------- | | topicChunks | Array.<string> | | | topic chunks to search for | | | node | Object | | | current node in the pattern tree | | | index | number | optional | 0 | current index ( depth ) in the topic array | |

_max

_max (isBetter, a, b)

Gets the maximum of two elements that are either a number or undefined . Undefined values are ignored _max ( undefined , undefined ) = undefined _max ( undefined , b ) = b

_max Parameters

| Name | Type | Description | | ---------- | ------------ | ----------------- | | isBetter | function(a, b | returns true , if a is better than b | | | a | number, undefined | first element | | | b | number, undefined | second element | |

addPattern

addPattern (pattern, value)

Adds a pattern to a pattern tree . The pattern has '/' as separator

addPattern Parameters

| Name | Type | Attribute | Default | Description | | ---------- | ------------ | ------------ | ------------ | ----------------- | | pattern | string | | | pattern string with '/' as separator | | | value | number | optional | 0 | pattern value | |

deletePattern

deletePattern (pattern)

Delete a pattern from the tree

deletePattern Parameters

| Name | Type | Description | | ---------- | ------------ | ----------------- | | pattern | string | pattern to delete | |

getBestMatch

getBestMatch (topic, isBetter)

Gets the first match of a topic

getBestMatch Parameters

| Name | Type | Description | | ---------- | ------------ | ----------------- | | topic | string | topic to search for | | | isBetter | function(a, b | returns true , if a is better than b | |

getFirstMatch

getFirstMatch (topic)

Gets the first match of a topic

getFirstMatch Parameters

| Name | Type | Description | | ---------- | ------------ | ----------------- | | topic | string | topic to search for | |

Class TopicMatch

new TopicMatch(pattern)

Contructs a new class Checks , if a topic matches a list of patterns . The pattern is bases on the MQTT pattern design . Every pattern has an associated value ( used for QoS in MQTT ) Topic structure slashes with strings in between : /string1/string2/string3/ . . . Pattern is like topic with the following whildcards : "+" and "#" . The "+" matches any string , the "#" matches the rest of the topic .

Example

const topicMatch = new TopicMatch()
topicMatch.addPattern('#', 0)
topicMatch.getFirstMatch('hello') // returns 0
topicMatch.removePattern('#')
topicMatch.addPattern('/+', 1)
topicMatch.getFirstMatch('hello') // returns undefined
topicMatch.getFirstMatch('/hello') // returns 1
topicMatch.getFirstMatch('/hello/world') // returns undefined

TopicMatch Parameters

| Name | Type | Attribute | Default | Description | | ---------- | ------------ | ------------ | ------------ | ----------------- | | pattern | Object, Array, string | optional | { } | pattern data structure { pattern : value , pattern : value , . . . } | |

TopicMatch Members

| Name | Type | description | | ------------ | ------------ | ------------ | | topicPatternList | @type | List containing all active pattern { pattern : QoS , . . . } |

TopicMatch Methods

addPattern

addPattern (pattern, value)

Adds a pattern string to the patternlist ( if not already included )

addPattern Parameters

| Name | Type | Attribute | Default | Description | | ---------- | ------------ | ------------ | ------------ | ----------------- | | pattern | string, Array, Object | | | single pattern or list of pattern | | | value | any | optional | 0 | value associated with the pattern | |

addPattern throws

| Type | Description | | ---- | ----------- | | Error | if pattern is not string or value is not provided |

changePattern

changePattern (patternCommand)

Sets , adds and/or removes pattern .

changePattern Parameters

| Name | Type | Description | | ---------- | ------------ | ----------------- | | patternCommand | Object | object with attributes "set , remove , add" and a pattern list | |

clear

clear ()

Clears the pattern tree

deletePattern

deletePattern (patternList)

Delete a pattern from the tree

deletePattern Parameters

| Name | Type | Description | | ---------- | ------------ | ----------------- | | patternList | Array, string | string or array of strings containing patterns to delete | |

getBestMatch

getBestMatch (searchTopic, isBetter) => {any}

Gets the best value of all matching pattern according the compare function . If no compare function is provided , the values of the patterns are compared with the ">" operator

getBestMatch Parameters

| Name | Type | Description | | ---------- | ------------ | ----------------- | | searchTopic | string | topic to search for | | | isBetter | function(newValue, currentValue | ( a , b ) function deciding , if "a" is better than "b" | |

getBestMatch returns

| Type | Description | | ---- | ----------- | | any | value associated with the best found topic or undefined , if nothing matches |

getFirstMatch

getFirstMatch (searchTopic) => {any, undefined}

Gets the value of the first matching pattern .

getFirstMatch Parameters

| Name | Type | Description | | ---------- | ------------ | ----------------- | | searchTopic | string | topic to search for | |

getFirstMatch returns

| Type | Description | | ---- | ----------- | | any, undefined | value associated with the found topic or undefined , if nothing matches |

toJSON

toJSON ()

hide the _tree from the JSON formatting