@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