@sc-voice/tools 3.3.0 → 3.5.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/index.mjs CHANGED
@@ -18,9 +18,11 @@ import { WordSpace } from './src/text/word-space.mjs';
18
18
  import { WordVector } from './src/text/word-vector.mjs';
19
19
  import { TfidfSpace } from './src/text/tfidf-space.mjs';
20
20
  import { LogEntry, Logger } from './src/text/logger.mjs';
21
+ import { ColorConsole} from './src/text/color-console.mjs';
21
22
 
22
23
  export const Text = {
23
24
  BilaraPath,
25
+ ColorConsole,
24
26
  Corpus,
25
27
  EbtDoc,
26
28
  LegacyDoc,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sc-voice/tools",
3
- "version": "3.3.0",
3
+ "version": "3.5.0",
4
4
  "description": "Utilities for SC-Voice",
5
5
  "main": "index.mjs",
6
6
  "files": [
@@ -1,4 +1,8 @@
1
1
  import { Unicode } from '../text/unicode.mjs';
2
+ const { INFINITY } = Unicode;
3
+
4
+ const MINUS_INFINITY = `-${INFINITY}`;
5
+ const PLUS_INFINITY = `+${INFINITY}`;
2
6
 
3
7
  export class Interval {
4
8
  constructor(a, b) {
@@ -10,57 +14,60 @@ export class Interval {
10
14
  let an = typeof a === 'number' && !Number.isNaN(a);
11
15
  let bn = typeof b === 'number' && !Number.isNaN(b);
12
16
 
17
+ let isClosed = true;
13
18
  if (an && bn) {
14
19
  dbg && console.log(msg, 'an bn');
15
20
  lo = a;
16
21
  hi = b;
17
- this.isClosed = true;
22
+ isClosed = true;
18
23
  } else if (an) {
19
24
  dbg && console.log(msg, 'an');
20
25
  lo = a;
21
- hi = Interval.INFINITY;
22
- this.isClosed = false;
26
+ hi = INFINITY;
27
+ isClosed = false;
23
28
  } else if (bn) {
24
29
  dbg && console.log(msg, 'bn');
25
- lo = Interval.INFINITY;
30
+ lo = INFINITY;
26
31
  hi = b;
27
- this.isClosed = false;
32
+ isClosed = false;
28
33
  } else {
29
34
  dbg && console.log(msg, '!an !bn');
30
- this.isClosed = false;
35
+ isClosed = false;
31
36
  }
32
37
 
33
- Object.defineProperty(this, 'lo', { value: lo });
34
- Object.defineProperty(this, 'hi', { value: hi });
38
+ Object.defineProperty(this, 'isClosed', {
39
+ value: isClosed,
40
+ });
41
+ Object.defineProperty(this, 'lo', {
42
+ enumerable: true,
43
+ value: lo,
44
+ });
45
+ Object.defineProperty(this, 'hi', {
46
+ enumerable: true,
47
+ value: hi,
48
+ });
35
49
  }
36
50
 
37
51
  static get INFINITY() {
38
- return Unicode.INFINITY;
52
+ return INFINITY;
39
53
  }
40
54
 
41
55
  get isEmpty() {
42
56
  if (this.lo === null && this.hi === null) {
43
57
  return true;
44
58
  }
45
- if (
46
- this.lo === Interval.INFINITY ||
47
- this.hi === Interval.INFINITY
48
- ) {
59
+ if (this.lo === INFINITY || this.hi === INFINITY) {
49
60
  return false;
50
61
  }
51
62
  return this.hi < this.lo;
52
63
  }
53
64
 
54
65
  get infimum() {
55
- return this.lo === Interval.INFINITY
56
- ? '-' + Interval.INFINITY
57
- : this.lo;
66
+ return this.lo === INFINITY ? MINUS_INFINITY : this.lo;
58
67
  }
59
68
 
60
69
  get supremum() {
61
- return this.hi === Interval.INFINITY
62
- ? '+' + Interval.INFINITY
63
- : this.hi;
70
+ return this.hi === INFINITY ? PLUS_INFINITY : this.hi;
64
71
  }
65
72
 
66
73
  contains(num) {
@@ -68,10 +75,10 @@ export class Interval {
68
75
  return false;
69
76
  }
70
77
  let { lo, hi } = this;
71
- if (lo === Interval.INFINITY) {
78
+ if (lo === INFINITY) {
72
79
  throw new Error(`${msg}TBD`);
73
80
  }
74
- if (hi === Interval.INFINITY) {
81
+ if (hi === INFINITY) {
75
82
  throw new Error(`${msg}TBD`);
76
83
  }
77
84
  if (lo < num && num < hi) {
@@ -80,5 +87,18 @@ export class Interval {
80
87
  if (num < lo || hi < num) {
81
88
  return false;
82
89
  }
90
+
91
+ return true;
92
+ }
93
+
94
+ toString() {
95
+ let { lo, hi } = this;
96
+ return [
97
+ lo === INFINITY ? '(' : '[',
98
+ lo === INFINITY ? MINUS_INFINITY : lo,
99
+ lo === hi ? '' : ',',
100
+ hi === INFINITY ? PLUS_INFINITY : hi,
101
+ hi === INFINITY ? ')' : ']',
102
+ ].join('');
83
103
  }
84
104
  }
@@ -0,0 +1,111 @@
1
+ import { Unicode } from './unicode.mjs';
2
+
3
+ const {
4
+ BLACK,
5
+ WHITE,
6
+ RED,
7
+ GREEN,
8
+ BLUE,
9
+ CYAN,
10
+ MAGENTA,
11
+ YELLOW,
12
+ BRIGHT_BLACK,
13
+ BRIGHT_WHITE,
14
+ BRIGHT_RED,
15
+ BRIGHT_GREEN,
16
+ BRIGHT_BLUE,
17
+ BRIGHT_CYAN,
18
+ BRIGHT_MAGENTA,
19
+ BRIGHT_YELLOW,
20
+ NO_COLOR,
21
+ } = Unicode.LINUX_COLOR;
22
+
23
+ export class ColorConsole {
24
+ constructor(opts = {}) {
25
+ let {
26
+ write = (...args) => console.log.call(null, ...args),
27
+ colorOk1 = BRIGHT_GREEN,
28
+ colorOk2 = GREEN,
29
+ colorFYI1 = BRIGHT_BLACK,
30
+ colorBad1 = BRIGHT_RED,
31
+ colorBad2 = RED,
32
+ colorTag1 = CYAN,
33
+ colorTag2 = BRIGHT_CYAN,
34
+ colorTag3 = MAGENTA,
35
+ colorTag4 = BRIGHT_MAGENTA,
36
+ } = opts;
37
+
38
+ Object.assign(this, {
39
+ write,
40
+ colorOk1,
41
+ colorOk2,
42
+ colorFYI1,
43
+ colorBad1,
44
+ colorBad2,
45
+ colorTag1,
46
+ colorTag2,
47
+ colorTag3,
48
+ colorTag4,
49
+ });
50
+ }
51
+
52
+ color(color, ...things) {
53
+ return things.map((thing) => {
54
+ switch (typeof thing) {
55
+ case 'object':
56
+ // TODO: pretty objects like console
57
+ return thing;
58
+ case 'string':
59
+ return `${color}${thing}${NO_COLOR}`;
60
+ case 'number':
61
+ return `${GREEN}${thing}${NO_COLOR}`;
62
+ default:
63
+ return `${color}${JSON.stringify(thing)}${NO_COLOR}`;
64
+ }
65
+ });
66
+ }
67
+
68
+ msgLvl(color, msg, lvl, rest) {
69
+ return this.color(color, msg + (lvl ? `@${lvl}` : ''), ...rest);
70
+ }
71
+
72
+ ok1(msg, lvl, ...rest) {
73
+ let color = this.colorOk1;
74
+ this.write(...this.msgLvl(color, msg, lvl, rest));
75
+ }
76
+
77
+ ok2(msg, lvl, ...rest) {
78
+ let color = this.colorOk2;
79
+ this.write(...this.msgLvl(color, msg, lvl, rest));
80
+ }
81
+
82
+ bad1(msg, lvl, ...rest) {
83
+ let color = this.colorBad1;
84
+ this.write(...this.msgLvl(color, msg, lvl, rest));
85
+ }
86
+
87
+ bad2(msg, lvl, ...rest) {
88
+ let color = this.colorBad2;
89
+ this.write(...this.msgLvl(color, msg, lvl, rest));
90
+ }
91
+
92
+ tag1(msg, lvl, ...rest) {
93
+ let color = this.colorTag1;
94
+ this.write(...this.msgLvl(color, msg, lvl, rest));
95
+ }
96
+
97
+ tag2(msg, lvl, ...rest) {
98
+ let color = this.colorTag2;
99
+ this.write(...this.msgLvl(color, msg, lvl, rest));
100
+ }
101
+
102
+ tag3(msg, lvl, ...rest) {
103
+ let color = this.colorTag3;
104
+ this.write(...this.msgLvl(color, msg, lvl, rest));
105
+ }
106
+
107
+ tag4(msg, lvl, ...rest) {
108
+ let color = this.colorTag4;
109
+ this.write(...this.msgLvl(color, msg, lvl, rest));
110
+ }
111
+ }