@seriousme/opifex 1.9.0 → 1.9.3

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.
@@ -1,4 +1,13 @@
1
+ /**
2
+ * @module trie
3
+ * @description A Trie data structure implementation that supports wildcard matching
4
+ * @template T The type of values stored in the trie
5
+ */
1
6
  type Parts = string[];
7
+ /**
8
+ * Trie class for storing and matching hierarchical key-value pairs with wildcard support
9
+ * @template T The type of values stored in the trie
10
+ */
2
11
  export declare class Trie<T> {
3
12
  #private;
4
13
  private separator;
@@ -6,15 +15,65 @@ export declare class Trie<T> {
6
15
  private wildcardSubtree;
7
16
  private reservedPrefix;
8
17
  private looseCompare;
18
+ /**
19
+ * Creates a new Trie instance
20
+ * @param looseCompare Whether to use loose comparison for object values
21
+ */
9
22
  constructor(looseCompare?: boolean);
23
+ /**
24
+ * Matches a child node with the given key parts
25
+ * @param child The child node key to match
26
+ * @param parts Remaining parts of the key to match
27
+ * @returns Array of matched values
28
+ */
10
29
  matchChild(child: string, parts: Parts): Array<T>;
30
+ /**
31
+ * Matches a key against the trie
32
+ * @param key The key to match
33
+ * @returns Array of matched values
34
+ */
11
35
  match(key: string): T[];
36
+ /**
37
+ * Internal method to match reserved prefix keys
38
+ * @param parts Parts of the key to match
39
+ * @returns Array of matched values
40
+ */
12
41
  private _matchPrefix;
42
+ /**
43
+ * Internal method to match keys with wildcard support
44
+ * @param parts Parts of the key to match
45
+ * @returns Array of matched values
46
+ */
13
47
  private _match;
48
+ /**
49
+ * Adds a value to the trie at the specified key
50
+ * @param key The key to add the value at
51
+ * @param value The value to add
52
+ */
14
53
  add(key: string, value: T): void;
54
+ /**
55
+ * Internal method to recursively add a value
56
+ * @param parts Parts of the key to add at
57
+ * @param value The value to add
58
+ */
15
59
  private _add;
60
+ /**
61
+ * Removes a value from the trie at the specified key
62
+ * @param key The key to remove the value from
63
+ * @param value The value to remove
64
+ */
16
65
  remove(key: string, value: T): void;
66
+ /**
67
+ * Internal method to recursively remove a value
68
+ * @param parts Parts of the key to remove from
69
+ * @param value The value to remove
70
+ */
17
71
  private _remove;
72
+ /**
73
+ * Creates a filter function for removing values
74
+ * @param value The value to filter against
75
+ * @returns Filter function
76
+ */
18
77
  private filter;
19
78
  }
20
79
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"trie.d.ts","sourceRoot":"","sources":["../../trie/trie.ts"],"names":[],"mappings":"AAAA,KAAK,KAAK,GAAG,MAAM,EAAE,CAAC;AAEtB,qBAAa,IAAI,CAAC,CAAC;;IAGjB,OAAO,CAAC,SAAS,CAAO;IACxB,OAAO,CAAC,WAAW,CAAO;IAC1B,OAAO,CAAC,eAAe,CAAO;IAC9B,OAAO,CAAC,cAAc,CAAO;IAC7B,OAAO,CAAC,YAAY,CAAU;gBAElB,YAAY,UAAQ;IAMhC,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,GAAG,KAAK,CAAC,CAAC,CAAC;IAKjD,KAAK,CAAC,GAAG,EAAE,MAAM;IASjB,OAAO,CAAC,YAAY;IAQpB,OAAO,CAAC,MAAM;IAYd,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;IAIzB,OAAO,CAAC,IAAI;IAiBZ,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;IAI5B,OAAO,CAAC,OAAO;IAgBf,OAAO,CAAC,MAAM;CAaf"}
1
+ {"version":3,"file":"trie.d.ts","sourceRoot":"","sources":["../../trie/trie.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,KAAK,KAAK,GAAG,MAAM,EAAE,CAAC;AAEtB;;;GAGG;AACH,qBAAa,IAAI,CAAC,CAAC;;IAGjB,OAAO,CAAC,SAAS,CAAO;IACxB,OAAO,CAAC,WAAW,CAAO;IAC1B,OAAO,CAAC,eAAe,CAAO;IAC9B,OAAO,CAAC,cAAc,CAAO;IAC7B,OAAO,CAAC,YAAY,CAAU;IAE9B;;;OAGG;gBACS,YAAY,UAAQ;IAMhC;;;;;OAKG;IACH,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,GAAG,KAAK,CAAC,CAAC,CAAC;IAKjD;;;;OAIG;IACH,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,CAAC,EAAE;IAQvB;;;;OAIG;IACH,OAAO,CAAC,YAAY;IAQpB;;;;OAIG;IACH,OAAO,CAAC,MAAM;IAYd;;;;OAIG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI;IAIhC;;;;OAIG;IACH,OAAO,CAAC,IAAI;IAiBZ;;;;OAIG;IACH,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI;IAInC;;;;OAIG;IACH,OAAO,CAAC,OAAO;IAgBf;;;;OAIG;IACH,OAAO,CAAC,MAAM;CAaf"}
package/dist/trie/trie.js CHANGED
@@ -1,3 +1,7 @@
1
+ /**
2
+ * Trie class for storing and matching hierarchical key-value pairs with wildcard support
3
+ * @template T The type of values stored in the trie
4
+ */
1
5
  export class Trie {
2
6
  #value;
3
7
  #children;
@@ -6,23 +10,42 @@ export class Trie {
6
10
  wildcardSubtree = "#";
7
11
  reservedPrefix = "$";
8
12
  looseCompare;
13
+ /**
14
+ * Creates a new Trie instance
15
+ * @param looseCompare Whether to use loose comparison for object values
16
+ */
9
17
  constructor(looseCompare = false) {
10
18
  this.#value = [];
11
19
  this.#children = new Map();
12
20
  this.looseCompare = looseCompare;
13
21
  }
22
+ /**
23
+ * Matches a child node with the given key parts
24
+ * @param child The child node key to match
25
+ * @param parts Remaining parts of the key to match
26
+ * @returns Array of matched values
27
+ */
14
28
  matchChild(child, parts) {
15
29
  const childNode = this.#children.get(child);
16
30
  return childNode ? childNode._match(parts) : [];
17
31
  }
32
+ /**
33
+ * Matches a key against the trie
34
+ * @param key The key to match
35
+ * @returns Array of matched values
36
+ */
18
37
  match(key) {
19
38
  const parts = key.split(this.separator);
20
- // toplevel wildcards are not allowed to match toplevel reserved topics
21
39
  if (parts.length > 0 && parts[0].charAt(0) === this.reservedPrefix) {
22
40
  return this._matchPrefix(parts);
23
41
  }
24
42
  return this._match(parts);
25
43
  }
44
+ /**
45
+ * Internal method to match reserved prefix keys
46
+ * @param parts Parts of the key to match
47
+ * @returns Array of matched values
48
+ */
26
49
  _matchPrefix(parts) {
27
50
  if (parts.length === 0) {
28
51
  return this.#value ? this.#value : [];
@@ -30,6 +53,11 @@ export class Trie {
30
53
  const [first, ...rest] = parts;
31
54
  return this.matchChild(first, rest);
32
55
  }
56
+ /**
57
+ * Internal method to match keys with wildcard support
58
+ * @param parts Parts of the key to match
59
+ * @returns Array of matched values
60
+ */
33
61
  _match(parts) {
34
62
  if (parts.length === 0) {
35
63
  return this.#value ? this.#value : [];
@@ -41,9 +69,19 @@ export class Trie {
41
69
  const results = exact.concat(single, subtree);
42
70
  return results;
43
71
  }
72
+ /**
73
+ * Adds a value to the trie at the specified key
74
+ * @param key The key to add the value at
75
+ * @param value The value to add
76
+ */
44
77
  add(key, value) {
45
78
  return this._add(key.split(this.separator), value);
46
79
  }
80
+ /**
81
+ * Internal method to recursively add a value
82
+ * @param parts Parts of the key to add at
83
+ * @param value The value to add
84
+ */
47
85
  _add(parts, value) {
48
86
  if (parts.length === 0) {
49
87
  this.#value = this.#value.concat(value);
@@ -61,9 +99,19 @@ export class Trie {
61
99
  }
62
100
  return;
63
101
  }
102
+ /**
103
+ * Removes a value from the trie at the specified key
104
+ * @param key The key to remove the value from
105
+ * @param value The value to remove
106
+ */
64
107
  remove(key, value) {
65
108
  return this._remove(key.split(this.separator), value);
66
109
  }
110
+ /**
111
+ * Internal method to recursively remove a value
112
+ * @param parts Parts of the key to remove from
113
+ * @param value The value to remove
114
+ */
67
115
  _remove(parts, value) {
68
116
  if (parts.length === 0) {
69
117
  const arr = this.#value || [];
@@ -79,6 +127,11 @@ export class Trie {
79
127
  }
80
128
  }
81
129
  }
130
+ /**
131
+ * Creates a filter function for removing values
132
+ * @param value The value to filter against
133
+ * @returns Filter function
134
+ */
82
135
  filter(value) {
83
136
  if (this.looseCompare && typeof value === "object") {
84
137
  return (item) => {
@@ -1 +1 @@
1
- {"version":3,"file":"trie.js","sourceRoot":"","sources":["../../trie/trie.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,IAAI;IACf,MAAM,CAAW;IACjB,SAAS,CAAuB;IACxB,SAAS,GAAG,GAAG,CAAC;IAChB,WAAW,GAAG,GAAG,CAAC;IAClB,eAAe,GAAG,GAAG,CAAC;IACtB,cAAc,GAAG,GAAG,CAAC;IACrB,YAAY,CAAU;IAE9B,YAAY,YAAY,GAAG,KAAK;QAC9B,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC;QACjB,IAAI,CAAC,SAAS,GAAG,IAAI,GAAG,EAAE,CAAC;QAC3B,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;IACnC,CAAC;IAED,UAAU,CAAC,KAAa,EAAE,KAAY;QACpC,MAAM,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QAC5C,OAAO,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IAClD,CAAC;IAED,KAAK,CAAC,GAAW;QACf,MAAM,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QACxC,uEAAuE;QACvE,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,cAAc,EAAE,CAAC;YACnE,OAAO,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;QAClC,CAAC;QACD,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC5B,CAAC;IAEO,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AK,CAAC;YACf,CAAC,CAAC;QACJ,CAAC;QACD,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,KAAK,KAAK,CAAC;IAClC,CAAC;CACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seriousme/opifex",
3
- "version": "1.9.0",
3
+ "version": "1.9.3",
4
4
  "description": "MQTT client & server for Deno & NodeJS",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -9,8 +9,7 @@
9
9
  "build": "npx -p typescript tsc --build",
10
10
  "format": "deno fmt",
11
11
  "preversion": "npm run test",
12
- "postversion": "jq '( { name:.name , version:.version, license:.license } + .deno)' package.json > deno.json",
13
- "postpostversion": "deno install"
12
+ "version": "jq '( { name:.name , version:.version, license:.license } + .deno)' package.json > deno.json && deno install && git add deno.json"
14
13
  },
15
14
  "keywords": [
16
15
  "MQTT",
package/trie/trie.ts CHANGED
@@ -1,5 +1,14 @@
1
+ /**
2
+ * @module trie
3
+ * @description A Trie data structure implementation that supports wildcard matching
4
+ * @template T The type of values stored in the trie
5
+ */
1
6
  type Parts = string[];
2
7
 
8
+ /**
9
+ * Trie class for storing and matching hierarchical key-value pairs with wildcard support
10
+ * @template T The type of values stored in the trie
11
+ */
3
12
  export class Trie<T> {
4
13
  #value: Array<T>;
5
14
  #children: Map<string, Trie<T>>;
@@ -9,26 +18,45 @@ export class Trie<T> {
9
18
  private reservedPrefix = "$";
10
19
  private looseCompare: boolean;
11
20
 
21
+ /**
22
+ * Creates a new Trie instance
23
+ * @param looseCompare Whether to use loose comparison for object values
24
+ */
12
25
  constructor(looseCompare = false) {
13
26
  this.#value = [];
14
27
  this.#children = new Map();
15
28
  this.looseCompare = looseCompare;
16
29
  }
17
30
 
31
+ /**
32
+ * Matches a child node with the given key parts
33
+ * @param child The child node key to match
34
+ * @param parts Remaining parts of the key to match
35
+ * @returns Array of matched values
36
+ */
18
37
  matchChild(child: string, parts: Parts): Array<T> {
19
38
  const childNode = this.#children.get(child);
20
39
  return childNode ? childNode._match(parts) : [];
21
40
  }
22
41
 
23
- match(key: string) {
42
+ /**
43
+ * Matches a key against the trie
44
+ * @param key The key to match
45
+ * @returns Array of matched values
46
+ */
47
+ match(key: string): T[] {
24
48
  const parts = key.split(this.separator);
25
- // toplevel wildcards are not allowed to match toplevel reserved topics
26
49
  if (parts.length > 0 && parts[0].charAt(0) === this.reservedPrefix) {
27
50
  return this._matchPrefix(parts);
28
51
  }
29
52
  return this._match(parts);
30
53
  }
31
54
 
55
+ /**
56
+ * Internal method to match reserved prefix keys
57
+ * @param parts Parts of the key to match
58
+ * @returns Array of matched values
59
+ */
32
60
  private _matchPrefix(parts: Parts): Array<T> {
33
61
  if (parts.length === 0) {
34
62
  return this.#value ? this.#value : [];
@@ -37,6 +65,11 @@ export class Trie<T> {
37
65
  return this.matchChild(first, rest);
38
66
  }
39
67
 
68
+ /**
69
+ * Internal method to match keys with wildcard support
70
+ * @param parts Parts of the key to match
71
+ * @returns Array of matched values
72
+ */
40
73
  private _match(parts: Parts): Array<T> {
41
74
  if (parts.length === 0) {
42
75
  return this.#value ? this.#value : [];
@@ -49,10 +82,20 @@ export class Trie<T> {
49
82
  return results;
50
83
  }
51
84
 
52
- add(key: string, value: T) {
85
+ /**
86
+ * Adds a value to the trie at the specified key
87
+ * @param key The key to add the value at
88
+ * @param value The value to add
89
+ */
90
+ add(key: string, value: T): void {
53
91
  return this._add(key.split(this.separator), value);
54
92
  }
55
93
 
94
+ /**
95
+ * Internal method to recursively add a value
96
+ * @param parts Parts of the key to add at
97
+ * @param value The value to add
98
+ */
56
99
  private _add(parts: Parts, value: T) {
57
100
  if (parts.length === 0) {
58
101
  this.#value = this.#value.concat(value);
@@ -70,10 +113,20 @@ export class Trie<T> {
70
113
  return;
71
114
  }
72
115
 
73
- remove(key: string, value: T) {
116
+ /**
117
+ * Removes a value from the trie at the specified key
118
+ * @param key The key to remove the value from
119
+ * @param value The value to remove
120
+ */
121
+ remove(key: string, value: T): void {
74
122
  return this._remove(key.split(this.separator), value);
75
123
  }
76
124
 
125
+ /**
126
+ * Internal method to recursively remove a value
127
+ * @param parts Parts of the key to remove from
128
+ * @param value The value to remove
129
+ */
77
130
  private _remove(parts: Parts, value: T): void {
78
131
  if (parts.length === 0) {
79
132
  const arr = this.#value || [];
@@ -90,6 +143,11 @@ export class Trie<T> {
90
143
  }
91
144
  }
92
145
 
146
+ /**
147
+ * Creates a filter function for removing values
148
+ * @param value The value to filter against
149
+ * @returns Filter function
150
+ */
93
151
  private filter(value: T): (value: T, index: number, array: T[]) => boolean {
94
152
  if (this.looseCompare && typeof value === "object") {
95
153
  return (item) => {