RangerMauve / mqtt-regex

Converts an MQTT topic with parameters into a regular expression.
MIT License
20 stars 1 forks source link

Add new 'format' function to build MQTT topic from a topic pattern and a set of values #5

Closed mcfitz2 closed 3 years ago

mcfitz2 commented 3 years ago

This PR adds a new method to the mqtt_regex result that allows the user to build a valid MQTT topic using a topic pattern and a set of values.

Usage is as follows:

var topic = mqtt_regex("/feeder/+feederId/needInit")
var params = {"feederId":"feeder-1"}
topic.format(params)

This should return a formatted MQTT topic that looks like /feeder/feeder-1/needInit

RangerMauve commented 3 years ago

Released in 1.1.0. Also added a thing to expand the array for the # params if one exists.

e.g.

const {format} = mqtt_pattern('foo/#bar')

format({bar: [1,2,3]}) === 'foo/1/2/3'