Skip to content

Commit

Permalink
Remove forward slashes from slugified topic names
Browse files Browse the repository at this point in the history
  • Loading branch information
Jalle19 committed Oct 20, 2023
1 parent b4ec9eb commit db03b2c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/publisher/mqtt/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import slugify from 'slugify'

export const slugifyName = (name: string): string => {
return slugify(name, {
// We can't have "+" in MQTT topic names
remove: /[+]/,
// We can't have "+" and "/" in MQTT topic names
remove: /[+/]/,
// Since the rest of the topic name is lower-case, just lower-case everything
lower: true,
})
Expand Down
12 changes: 11 additions & 1 deletion tests/publisher/mqtt/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,27 @@ import { createCharacteristicsSensorTopicName, createPowerSensorTopicName } from
import { Circuit } from '../../../src/circuit'

test('creates correct topic names', () => {
// Strips spaces
const characteristics = {
name: 'Some characteristic',
} as unknown as Characteristics

let topic = createCharacteristicsSensorTopicName(characteristics, 'voltage')
expect(topic).toEqual('eachwatt/characteristic/some-characteristic/voltage')

const circuit = {
// Strips plus signs
let circuit = {
name: 'Some circuit + with plus sign',
} as unknown as Circuit

topic = createPowerSensorTopicName(circuit, 'power')
expect(topic).toEqual('eachwatt/circuit/some-circuit-with-plus-sign/power')

// Strips forward slashes
circuit = {
name: 'Some circuit / with a slash',
} as unknown as Circuit

topic = createPowerSensorTopicName(circuit, 'power')
expect(topic).toEqual('eachwatt/circuit/some-circuit-with-a-slash/power')
})

0 comments on commit db03b2c

Please sign in to comment.