Need private packages and team management tools?Check out npm Teams »

@mangar2/topicmatch

1.1.2 • Public • Published

Table of Contents

Meta

  • copyright: Copyright (c) 2020 Volker Böhm

  • author: Volker Böhm

  • license: This software is licensed under the GNU LESSER GENERAL PUBLIC LICENSE Version 3. It is furnished "as is", without any support, and with no warranty, express or implied, as to its usefulness for any purpose.

TopicMatch

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.

Parameters

  • pattern (Object | Array | string) pattern data structure {pattern: value, pattern: value, ...} (optional, default {})

Examples

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

addPattern

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

Parameters

  • pattern (string | Array | Object) single pattern or list of pattern

  • value any value associated with the pattern (optional, default 0)

  • Throws Error if pattern is not string or value is not provided

removePattern

Removes all pattern from the list matching the provided patterns. A "removePattern("#")" will for example remove all stored patterns

Parameters

  • pattern (Array | string) string or array of strings containing patterns to delete

changePattern

Sets, adds and/or removes pattern.

Parameters

  • patternCommand Object object with attributes "set, remove, add" and a pattern list

getAllMatchingPattern

Gets a list of matching pattern

Parameters

  • searchTopic string topic to search for

Returns Object all matching topics {topic1: value, topic2: value, ...}

getFirstMatch

Gets the value of the first matching pattern.

Parameters

  • searchTopic string topic to search for

Returns (any | undefined) value associated with the found topic or undefined, if nothing matches

getBestMatch

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

Parameters

  • searchTopic string topic to search for
  • isBetter function (newValue, currentValue) (a, b) function deciding, if "a" is better than "b"

Returns any value associated with the best found topic or undefined, if nothing matches

Install

npm i @mangar2/topicmatch

DownloadsWeekly Downloads

1

Version

1.1.2

License

LGPL-3.0-or-later

Unpacked Size

15.9 kB

Total Files

4

Last publish

Collaborators

  • avatar