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.
Files changed (4) hide show
  1. package/README.md +3 -0
  2. package/index.js +24 -0
  3. package/package.json +1 -1
  4. package/test.js +11 -1
package/README.md CHANGED
@@ -25,6 +25,9 @@ var filled = MQTTPattern.fill(pattern, params);
25
25
  // filled will be
26
26
  "device/fitbit/undefined/rate/bmp"
27
27
 
28
+ MQTTPattern.clean("hello/+param1/world/#param2");
29
+ // hello/+/world/#
30
+
28
31
  ```
29
32
 
30
33
  ## Installing
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mqtt-pattern",
3
- "version": "1.1.3",
3
+ "version": "1.2.0",
4
4
  "description": "Fast library for matching MQTT patterns with named wildcards",
5
5
  "main": "index.js",
6
6
  "scripts": {
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
+ });