@sc-voice/tools 2.0.0 → 2.1.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 CHANGED
@@ -1,12 +1,37 @@
1
- # merkle-json
1
+ # @sc-voice/tools
2
+ Javascript libary for SC-Voice applications
3
+
4
+ * Math
5
+ * Text
6
+ * Graph
7
+
8
+ ## Math.Fraction
9
+ ```
10
+ let f = new Fraction(9, 240, 'segments');
11
+ console.log(f.value); // 0.375
12
+ console.log(f.numerator, f.denominator); // 9 240
13
+ console.log(f.n, f.d); // 9 240
14
+ console.log(f.toString()); // 1/2 segments
15
+ console.log(Fraction.gcd(9, 240)); // 3
16
+ console.log(f.difference); // -150
17
+ console.log(f.remainder); // 9
18
+ console.log(f.percent); // '4%'
19
+ console.log(f.add(new Fraction(1,80)); // new Fraction(4,80)
20
+ ```
21
+
22
+ ## Text.WordSpace
23
+ Used for text similarity comparison, WordSpace creates and compares
24
+ Vectors of words weighted by normalized frequency of occurrence.
25
+ Weights and scores are normalized to the interval [0..1].
26
+
27
+ ## Text.MerkleJson
2
28
  Computing the hash of JSON objects can be tricky because JSON.stringify()
3
29
  does not have a guaranteed string representation of a Javascript object.
4
30
  Specifically, the following are equivalent and valid outputs of JSON.stringify():
5
31
 
6
32
  ```js
7
33
  var json = "{size:{w:100,h:200}}";
8
- var json = "{size:{h:100,w:200}}";
9
- ```
34
+ var json = "{size:{h:100,w:200}}"; ```
10
35
 
11
36
  MerkleJson guarantees a unique hash code for any Javascript object.
12
37
  In addition, MerkleJson is efficient in that it only recalculates
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sc-voice/tools",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "Utilities for SC-Voice",
5
5
  "main": "index.mjs",
6
6
  "files": [
@@ -41,24 +41,51 @@ export class LogEntry {
41
41
  }
42
42
  }
43
43
 
44
+ const LEVEL_DEBUG = { id: 'D', priority: -1 };
45
+ const LEVEL_INFO = { id: 'I', priority: 0 };
46
+ const LEVEL_WARN = { id: 'W', priority: 1 };
47
+ const LEVEL_ERROR = { id: 'E', priority: 2 };
48
+ const LEVEL_LOG = { id: 'L', priority: 3 };
49
+
44
50
  export class Logger {
45
51
  constructor(opts = {}) {
46
- let { sink = console, msBase = Date.now() } = opts;
52
+ let {
53
+ sink = console,
54
+ msBase = Date.now(),
55
+ logLevel = Logger.LEVEL_WARN,
56
+ } = opts;
47
57
  Object.assign(this, {
48
58
  history: [],
49
59
  sink,
50
60
  msBase,
61
+ logLevel,
51
62
  });
52
63
  }
53
64
 
65
+ static get LEVEL_DEBUG() {
66
+ return LEVEL_DEBUG;
67
+ }
68
+ static get LEVEL_INFO() {
69
+ return LEVEL_INFO;
70
+ }
71
+ static get LEVEL_WARN() {
72
+ return LEVEL_WARN;
73
+ }
74
+ static get LEVEL_ERROR() {
75
+ return LEVEL_ERROR;
76
+ }
77
+ static get LEVEL_LOG() {
78
+ return LEVEL_LOG;
79
+ }
80
+
54
81
  addEntry(level, args, fSink) {
55
82
  const msg = 'l4r.addEntry;';
56
83
  const dbg = DBG.L4R_ADD_ENTRY;
57
- let { history, sink, msBase } = this;
84
+ let { logLevel, history, sink, msBase } = this;
58
85
  let ms = Date.now() - msBase;
59
86
  let entry = LogEntry.fromArgs(level, args, ms);
60
87
  history.push(entry);
61
- if (sink) {
88
+ if (sink && level.priority >= logLevel.priority) {
62
89
  dbg && console.log(msg, 'sink');
63
90
  fSink?.apply(sink, args);
64
91
  }
@@ -66,22 +93,23 @@ export class Logger {
66
93
  }
67
94
 
68
95
  debug(...args) {
69
- return this.addEntry('D', args, this.sink?.debug);
96
+ return this.addEntry(LEVEL_DEBUG, args, this.sink?.debug);
70
97
  }
71
98
 
72
99
  info(...args) {
73
- return this.addEntry('I', args, this.sink?.info);
74
- }
75
-
76
- log(...args) {
77
- return this.addEntry('L', args, this.sink?.log);
100
+ return this.addEntry(LEVEL_INFO, args, this.sink?.info);
78
101
  }
79
102
 
80
103
  warn(...args) {
81
- return this.addEntry('W', args, this.sink?.warn);
104
+ return this.addEntry(LEVEL_WARN, args, this.sink?.warn);
82
105
  }
83
106
 
84
107
  error(...args) {
85
- return this.addEntry('E', args, this.sink?.error);
108
+ return this.addEntry(LEVEL_ERROR, args, this.sink?.error);
86
109
  }
110
+
111
+ log(...args) {
112
+ return this.addEntry(LEVEL_INFO, args, this.sink?.log);
113
+ }
114
+
87
115
  }