2.1.2 • Published 3 years ago

@mangar2/topicmatch v2.1.2

Weekly downloads
11
License
LGPL-3.0-or-later
Repository
github
Last release
3 years ago

Abstract

Contents

Meta

Global functions

_deletePatternRec

_deletePatternRec (topicChunks, node, index)

Recursively removes a pattern ( exact match ) from the tree

_deletePatternRec Parameters

NameTypeAttributeDefaultDescription
topicChunksArray.<string>topic chunks to search for
nodeObjectcurrent node in the pattern tree
indexnumberoptional0current 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

NameTypeAttributeDefaultDescription
isBetterfunction(a, breturns true , if a is better than b
topicChunksArray.<string>topic chunks to search for
nodeObjectcurrent node in the pattern tree
indexnumberoptional0current 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

NameTypeAttributeDefaultDescription
topicChunksArray.<string>topic chunks to search for
nodeObjectcurrent node in the pattern tree
indexnumberoptional0current 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

NameTypeDescription
isBetterfunction(a, breturns true , if a is better than b
anumber, undefinedfirst element
bnumber, undefinedsecond element

addPattern

addPattern (pattern, value)

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

addPattern Parameters

NameTypeAttributeDefaultDescription
patternstringpattern string with '/' as separator
valuenumberoptional0pattern value

deletePattern

deletePattern (pattern)

Delete a pattern from the tree

deletePattern Parameters

NameTypeDescription
patternstringpattern to delete

getBestMatch

getBestMatch (topic, isBetter)

Gets the first match of a topic

getBestMatch Parameters

NameTypeDescription
topicstringtopic to search for
isBetterfunction(a, breturns true , if a is better than b

getFirstMatch

getFirstMatch (topic)

Gets the first match of a topic

getFirstMatch Parameters

NameTypeDescription
topicstringtopic 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

NameTypeAttributeDefaultDescription
patternObject, Array, stringoptional{ }pattern data structure { pattern : value , pattern : value , . . . }

TopicMatch Members

NameTypedescription
topicPatternList@typeList 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
NameTypeAttributeDefaultDescription
patternstring, Array, Objectsingle pattern or list of pattern
valueanyoptional0value associated with the pattern
addPattern throws
TypeDescription
Errorif pattern is not string or value is not provided

changePattern

changePattern (patternCommand)

Sets , adds and/or removes pattern .

changePattern Parameters
NameTypeDescription
patternCommandObjectobject 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
NameTypeDescription
patternListArray, stringstring 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
NameTypeDescription
searchTopicstringtopic to search for
isBetterfunction(newValue, currentValue( a , b ) function deciding , if "a" is better than "b"
getBestMatch returns
TypeDescription
anyvalue 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
NameTypeDescription
searchTopicstringtopic to search for
getFirstMatch returns
TypeDescription
any, undefinedvalue associated with the found topic or undefined , if nothing matches

toJSON

toJSON ()

hide the _tree from the JSON formatting