mqtt-pattern 1.1.3 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/index.js +24 -0
- package/package.json +1 -1
- package/test.js +11 -1
package/README.md
CHANGED
package/index.js
CHANGED
|
@@ -8,6 +8,7 @@ module.exports = {
|
|
|
8
8
|
extract: extract,
|
|
9
9
|
exec: exec,
|
|
10
10
|
fill: fill,
|
|
11
|
+
clean: clean
|
|
11
12
|
};
|
|
12
13
|
|
|
13
14
|
function exec(pattern, topic) {
|
|
@@ -95,3 +96,26 @@ function extract(pattern, topic) {
|
|
|
95
96
|
|
|
96
97
|
return params;
|
|
97
98
|
}
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
function clean(pattern) {
|
|
102
|
+
var patternSegments = pattern.split(SEPARATOR);
|
|
103
|
+
var patternLength = patternSegments.length;
|
|
104
|
+
|
|
105
|
+
var cleanedSegments = [];
|
|
106
|
+
|
|
107
|
+
for(var i = 0; i < patternLength; i++){
|
|
108
|
+
var currentPattern = patternSegments[i];
|
|
109
|
+
var patternChar = currentPattern[0];
|
|
110
|
+
|
|
111
|
+
if(patternChar === ALL){
|
|
112
|
+
cleanedSegments.push(ALL);
|
|
113
|
+
} else if(patternChar === SINGLE){
|
|
114
|
+
cleanedSegments.push(SINGLE);
|
|
115
|
+
} else {
|
|
116
|
+
cleanedSegments.push(currentPattern);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return cleanedSegments.join('/');
|
|
121
|
+
}
|
package/package.json
CHANGED
package/test.js
CHANGED
|
@@ -154,4 +154,14 @@ test("fill() uses `undefined` for non-named + params", function(t){
|
|
|
154
154
|
"foo/undefined",
|
|
155
155
|
"Filled in params"
|
|
156
156
|
);
|
|
157
|
-
});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test("clean() removes parameter names", function(t){
|
|
160
|
+
t.plan(1);
|
|
161
|
+
t.equal(MQTTPattern.clean("hello/+param1/world/#param2"), "hello/+/world/#", "Got hello/+/world/#");
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
test("clean() works when there aren't any parameter names", function(t){
|
|
165
|
+
t.plan(1);
|
|
166
|
+
t.equal(MQTTPattern.clean("hello/+/world/#"), "hello/+/world/#", "Got hello/+/world/#");
|
|
167
|
+
});
|