@dra2020/baseclient 1.0.19 → 1.0.22

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.
@@ -0,0 +1 @@
1
+ export * from './emit';
@@ -0,0 +1,7 @@
1
+ export declare class Emit {
2
+ onList: any;
3
+ constructor();
4
+ on(eventName: string, cb: any): void;
5
+ emit(eventName: string, arg1?: any, arg2?: any, arg3?: any): void;
6
+ off(eventName: string, cb: any): void;
7
+ }
@@ -69,8 +69,14 @@ export declare class BinTrie {
69
69
  constructor(coder: Coder);
70
70
  static fromBuffer(coder: Coder, ab: ArrayBuffer): BinTrie;
71
71
  get(key: string): string;
72
+ forEach(cb: (k: string, v: string) => void): void;
73
+ }
74
+ export interface BinTrieOptions {
75
+ dedup?: boolean;
76
+ verbose?: boolean;
72
77
  }
73
78
  export declare class BinTrieBuilder {
79
+ options: BinTrieOptions;
74
80
  coder: Coder;
75
81
  root: UnpackedNode;
76
82
  vtb: ValueTableBuilder;
@@ -78,7 +84,7 @@ export declare class BinTrieBuilder {
78
84
  ab: ArrayBuffer;
79
85
  u8: Uint8Array;
80
86
  i32: Int32Array;
81
- constructor(coder: Coder);
87
+ constructor(coder: Coder, options?: BinTrieOptions);
82
88
  nodeCount(node: UnpackedNode): number;
83
89
  nodeBranching(node: UnpackedNode, counts: number[]): number;
84
90
  nodeValueCount(node: UnpackedNode): number;
@@ -88,6 +94,6 @@ export declare class BinTrieBuilder {
88
94
  getUnpacked(key: string): string;
89
95
  packNode(node: UnpackedNode, byteOffset: number): number;
90
96
  toBinary(): void;
91
- static fromMap(coder: Coder, o: StringMap): BinTrie;
97
+ static fromMap(coder: Coder, o: StringMap, options?: BinTrieOptions): BinTrie;
92
98
  }
93
99
  export {};
package/lib/all/all.ts CHANGED
@@ -19,3 +19,5 @@ import { FilterExpr } from '../filterexpr/all';
19
19
  export { FilterExpr }
20
20
  import * as G from '../geo/all';
21
21
  export { G };
22
+ import * as Emit from '../emit/all';
23
+ export { Emit };
@@ -0,0 +1 @@
1
+ export * from './emit';
@@ -0,0 +1,40 @@
1
+ export class Emit
2
+ {
3
+ onList: any;
4
+
5
+ constructor()
6
+ {
7
+ this.onList = {};
8
+ }
9
+
10
+ on(eventName: string, cb: any): void
11
+ {
12
+ let aCB: any = this.onList[eventName];
13
+ if (aCB === undefined)
14
+ {
15
+ aCB = [];
16
+ this.onList[eventName] = aCB;
17
+ }
18
+ aCB.push(cb);
19
+ }
20
+
21
+ emit(eventName: string, arg1?: any, arg2?: any, arg3?: any): void
22
+ {
23
+ let aCB: any[] = this.onList[eventName];
24
+ if (aCB !== undefined)
25
+ for (let i: number = 0; i < aCB.length; i++)
26
+ (aCB[i])(arg1, arg2, arg3);
27
+ }
28
+
29
+ off(eventName: string, cb: any): void
30
+ {
31
+ let aCB: any = this.onList[eventName];
32
+ if (aCB !== undefined)
33
+ for (let i: number = 0; i < aCB.length; i++)
34
+ if (aCB[i] === cb)
35
+ {
36
+ aCB.splice(i, 1);
37
+ break;
38
+ }
39
+ }
40
+ }
@@ -1,3 +1,5 @@
1
+ import * as U from './util';
2
+
1
3
  //
2
4
  // Packed format Trie for mapping string to string
3
5
  // Assumptions:
@@ -55,6 +57,8 @@ Util.setCoder({ encoder: new u.TextEncoder(), decoder: new u.TextDecoder('utf-8'
55
57
  Util.setCoder({ encoder: new TextEncoder(), decoder: new TextDecoder('utf-8') });
56
58
  */
57
59
 
60
+ const MaxKeyLength = 128;
61
+
58
62
  export function s2u8(coder: Coder, s: string): Uint8Array
59
63
  {
60
64
  return coder.encoder.encode(s);
@@ -341,10 +345,35 @@ export class BinTrie
341
345
  }
342
346
  return undefined;
343
347
  }
348
+
349
+ forEach(cb: (k: string, v: string) => void)
350
+ {
351
+ let keybuf = new Uint8Array(MaxKeyLength);
352
+ let keylen = 0;
353
+
354
+ let processNode = (len: number, byteOffset: number) => {
355
+ let iOffset = byteOffset >> 2;
356
+ let n = this.i32[iOffset];
357
+ byteOffset += 4;
358
+ for (let j = 0; j < n; j++)
359
+ {
360
+ keybuf[keylen++] = this.u8[byteOffset+j];
361
+ }
362
+ };
363
+ processNode(0, 4);
364
+ }
344
365
  }
345
366
 
367
+ export interface BinTrieOptions
368
+ {
369
+ dedup?: boolean,
370
+ verbose?: boolean,
371
+ }
372
+ let DefaultOptions: BinTrieOptions = { dedup: true, verbose: false };
373
+
346
374
  export class BinTrieBuilder
347
375
  {
376
+ options: BinTrieOptions;
348
377
  coder: Coder;
349
378
  root: UnpackedNode;
350
379
  vtb: ValueTableBuilder;
@@ -353,9 +382,10 @@ export class BinTrieBuilder
353
382
  u8: Uint8Array;
354
383
  i32: Int32Array;
355
384
 
356
- constructor(coder: Coder)
385
+ constructor(coder: Coder, options?: BinTrieOptions)
357
386
  {
358
387
  this.coder = coder;
388
+ this.options = U.shallowAssignImmutable(DefaultOptions, options);
359
389
  }
360
390
 
361
391
  // Building
@@ -542,9 +572,9 @@ export class BinTrieBuilder
542
572
  }
543
573
 
544
574
  // Building
545
- static fromMap(coder: Coder, o: StringMap): BinTrie
575
+ static fromMap(coder: Coder, o: StringMap, options?: BinTrieOptions): BinTrie
546
576
  {
547
- let btb = new BinTrieBuilder(coder);
577
+ let btb = new BinTrieBuilder(coder, options);
548
578
  btb.vtb = ValueTableBuilder.fromStrings(coder, Object.values(o));
549
579
  btb.vt = ValueTable.fromBuffer(coder, btb.vtb.ab, 0, btb.vtb.ab.byteLength);
550
580
  let keys = Object.keys(o);
@@ -563,7 +593,8 @@ export class BinTrieBuilder
563
593
  });
564
594
 
565
595
  // dedup (collapse branches pointing to same value)
566
- btb.dedup(btb.root);
596
+ if (btb.options.dedup)
597
+ btb.dedup(btb.root);
567
598
 
568
599
  // validate
569
600
  keys.forEach(k => {
@@ -596,7 +627,8 @@ export class BinTrieBuilder
596
627
  }
597
628
  });
598
629
 
599
- console.log(`bintrie: total size: ${btb.u8.length}, value table size: ${btb.vtb.u8.length}`);
630
+ if (btb.options.verbose)
631
+ console.log(`bintrie: total size: ${btb.u8.length}, value table size: ${btb.vtb.u8.length}`);
600
632
 
601
633
  return bt;
602
634
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dra2020/baseclient",
3
- "version": "1.0.19",
3
+ "version": "1.0.22",
4
4
  "description": "Utility functions for Javascript projects.",
5
5
  "main": "dist/baseclient.js",
6
6
  "types": "./dist/all/all.d.ts",