Skip to content

Commit

Permalink
Supper zero length topics
Browse files Browse the repository at this point in the history
  • Loading branch information
RangerMauve committed Feb 27, 2018
1 parent a8f5ede commit 284944b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
9 changes: 3 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ function matches(pattern, topic) {
var patternSegments = pattern.split(SEPARATOR);
var topicSegments = topic.split(SEPARATOR);

if (!patternSegments[0])
patternSegments = patternSegments.slice(1);

if (!topicSegments[0])
topicSegments = topicSegments.slice(1);

var patternLength = patternSegments.length;
var topicLength = topicSegments.length;
var lastIndex = patternLength - 1;
Expand All @@ -33,6 +27,9 @@ function matches(pattern, topic) {
var patternChar = currentPattern[0];
var currentTopic = topicSegments[i];

if(!currentTopic && !currentPattern)
return true;

if(!currentTopic && currentPattern !== ALL) return false;

// Only allow # at end
Expand Down
7 changes: 3 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,9 @@ test("matches() supports named wildcards", function (t) {
t.ok(MQTTPattern.matches("foo/+something/#else", "foo/bar/baz"), "Matched topic");
});

test("matches() ignores leading '/' characters in topics and wildcards", function (t) {
t.plan(2);
t.ok(MQTTPattern.matches("/foo/bar", "foo/bar"), "Matched pattern with leading slash");
t.ok(MQTTPattern.matches("foo/bar", "/foo/bar"), "Matched topic with leading slash");
test("matches() supports leading slashes", function (t){
t.plan(1);
t.ok(MQTTPattern.matches("/foo/bar", "/foo/bar"), "Matched topic");
});

test("extract() returns empty object of there's nothing to extract", function (t) {
Expand Down

0 comments on commit 284944b

Please sign in to comment.