# @mangar2/topicmatch

> Checks for pattern matching a MQTT style topic

Latest version **2.1.2** (published 2020-12-11) · LGPL-3.0-or-later license · 0 weekly downloads

## Install

```sh
npm install @mangar2/topicmatch
pnpm add @mangar2/topicmatch
yarn add @mangar2/topicmatch
bun add @mangar2/topicmatch
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.2 |
| Published | 2020-12-11 |
| First published | 2019-09-22 |
| Weekly downloads | 0 |
| License | LGPL-3.0-or-later |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 49.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Volker Böhm |
| Maintainers | mangar2 |
| Keywords | MQTT, topic, pattern, match |

## Links

- npm: https://www.npmjs.com/package/@mangar2/topicmatch
- Repository: https://github.com/Mangar2/yaha/tree/master/node_modules/@mangar2/topicmatch
- Homepage: https://github.com/Mangar2/yaha.git
- npm.io page: https://npm.io/package/@mangar2/topicmatch

## Dependencies (1)

- [@mangar2/types](https://npm.io/package/@mangar2/types.md) latest

## Recent versions

- 2.1.2 (latest) — 2020-12-11
- 2.1.1 — 2020-12-10
- 2.1.0 — 2020-12-10
- 1.1.2 — 2020-02-24
- 1.1.1 — 2020-02-24
- 1.1.0 — 2020-01-16
- 1.0.4 — 2020-01-14
- 1.0.3 — 2019-10-15
- 1.0.2 — 2019-10-06
- 1.0.1 — 2019-09-22
- 1.0.0 — 2019-09-22

## README

<!-- This file is generated by jsmddoc version 0.1 -->

# Abstract

## Contents

- [Meta](#Meta)
- [Global Functions](#Global-functions)
  - [_deletePatternRec](#_deletePatternRec)
  - [_getBestMatchRec](#_getBestMatchRec)
  - [_getFirstMatchRec](#_getFirstMatchRec)
  - [_max](#_max)
  - [addPattern](#addPattern)
  - [deletePattern](#deletePattern)
  - [getBestMatch](#getBestMatch)
  - [getFirstMatch](#getFirstMatch)
- [Class TopicMatch](#Class-TopicMatch)
  - [Parameters](#TopicMatch-Parameters)
  - [Members](#TopicMatch-Members)
  - [Methods](#TopicMatch-Methods)
    - [addPattern](#addPattern)
    - [changePattern](#changePattern)
    - [clear](#clear)
    - [deletePattern](#deletePattern)
    - [getBestMatch](#getBestMatch)
    - [getFirstMatch](#getFirstMatch)
    - [toJSON](#toJSON)

## 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

```javascript
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

---
_Source: https://npm.io/package/@mangar2/topicmatch · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
