-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (39 loc) · 1.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/* global define, window */
(function () {
'use strict';
function mqttWildcard(topic, wildcard) {
if (topic === wildcard) {
return [];
} else if (wildcard === '#') {
return [topic];
}
var res = [];
var t = String(topic).split('/');
var w = String(wildcard).split('/');
var i = 0;
for (var lt = t.length; i < lt; i++) {
if (w[i] === '+') {
res.push(t[i]);
} else if (w[i] === '#') {
res.push(t.slice(i).join('/'));
return res;
} else if (w[i] !== t[i]) {
return null;
}
}
if (w[i] === '#') {
i += 1;
}
return (i === w.length) ? res : null;
}
/* istanbul ignore next */
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = mqttWildcard;
} else if (typeof define === 'function' && define.amd) {
define([], function () {
return mqttWildcard();
});
} else {
window.mqttWildcard = mqttWildcard;
}
})();