@sc-voice/tools 3.13.0 → 3.15.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sc-voice/tools",
3
- "version": "3.13.0",
3
+ "version": "3.15.0",
4
4
  "description": "Utilities for SC-Voice",
5
5
  "main": "index.mjs",
6
6
  "files": [
package/src/defines.mjs CHANGED
@@ -1,6 +1,8 @@
1
1
  export const DBG = {
2
2
  ALIGN_ALL: 0,
3
3
  ALIGN_LINE: 0,
4
+ I6L_CONTAINS: 0,
5
+ I6L_OVERLAPS: 0,
4
6
  ML_DOC_VECTORS: 0, // 'mn8:3.4',
5
7
  MN8_MOHAN: 0,
6
8
  DEEPL_ADAPTER: 0,
@@ -1,20 +1,29 @@
1
1
  import { Unicode } from '../text/unicode.mjs';
2
- const { INFINITY } = Unicode;
2
+ const { EMPTY_SET, INFINITY } = Unicode;
3
+ import { ColorConsole } from '../text/color-console.mjs';
4
+ const { cc } = ColorConsole;
5
+ import { DBG } from '../defines.mjs';
3
6
 
4
7
  const MINUS_INFINITY = `-${INFINITY}`;
5
8
  const PLUS_INFINITY = `+${INFINITY}`;
6
9
 
7
10
  export class Interval {
8
- constructor(a, b) {
11
+ constructor(a, b, opts = {}) {
9
12
  const msg = 'i6l.ctor';
10
13
  let hi = null;
11
14
  let lo = null;
12
15
  const dbg = 0;
16
+ let { leftOpen = false, rightOpen = false } = opts;
17
+ if (a === INFINITY || a == null) {
18
+ leftOpen = true;
19
+ }
20
+ if (b === INFINITY || b == null) {
21
+ rightOpen = true;
22
+ }
13
23
 
14
24
  let an = typeof a === 'number' && !Number.isNaN(a);
15
25
  let bn = typeof b === 'number' && !Number.isNaN(b);
16
26
 
17
- let isClosed = true;
18
27
  if (an && bn) {
19
28
  if (b < a) {
20
29
  throw new Error(`${msg} invalid interval ${b}<${a}?`);
@@ -22,25 +31,18 @@ export class Interval {
22
31
  dbg && console.log(msg, 'an bn');
23
32
  lo = a;
24
33
  hi = b;
25
- isClosed = true;
26
34
  } else if (an) {
27
35
  dbg && console.log(msg, 'an');
28
36
  lo = a;
29
37
  hi = INFINITY;
30
- isClosed = false;
31
38
  } else if (bn) {
32
39
  dbg && console.log(msg, 'bn');
33
40
  lo = INFINITY;
34
41
  hi = b;
35
- isClosed = false;
36
42
  } else {
37
43
  dbg && console.log(msg, '!an !bn');
38
- isClosed = false;
39
44
  }
40
45
 
41
- Object.defineProperty(this, 'isClosed', {
42
- value: isClosed,
43
- });
44
46
  Object.defineProperty(this, 'lo', {
45
47
  enumerable: true,
46
48
  value: lo,
@@ -49,12 +51,28 @@ export class Interval {
49
51
  enumerable: true,
50
52
  value: hi,
51
53
  });
54
+ Object.defineProperty(this, 'leftOpen', {
55
+ enumerable: true,
56
+ value: leftOpen,
57
+ });
58
+ Object.defineProperty(this, 'rightOpen', {
59
+ enumerable: true,
60
+ value: rightOpen,
61
+ });
52
62
  }
53
63
 
54
64
  static get INFINITY() {
55
65
  return INFINITY;
56
66
  }
57
67
 
68
+ get isOpen() {
69
+ return this.leftOpen || this.rightOpen || this.isEmpty;
70
+ }
71
+
72
+ get isClosed() {
73
+ return (!this.leftOpen && !this.rightOpen) || this.isEmpty;
74
+ }
75
+
58
76
  get isEmpty() {
59
77
  if (this.lo === null && this.hi === null) {
60
78
  return true;
@@ -78,16 +96,25 @@ export class Interval {
78
96
  }
79
97
 
80
98
  contains(num) {
99
+ const msg = 'i6l.contains';
100
+ const dbg = DBG.I6L_CONTAINS;
81
101
  if (typeof num !== 'number' || Number.isNaN(num)) {
102
+ dbg && cc.fyi1(msg+0.1, false);
82
103
  return false;
83
104
  }
84
- let { lo, hi } = this;
105
+ let { lo, hi, leftOpen, rightOpen } = this;
85
106
  if (lo === INFINITY) {
86
107
  throw new Error(`${msg}TBD`);
87
108
  }
88
109
  if (hi === INFINITY) {
89
110
  throw new Error(`${msg}TBD`);
90
111
  }
112
+ if (lo === num && leftOpen) {
113
+ return false;
114
+ }
115
+ if (hi === num && rightOpen) {
116
+ return false;
117
+ }
91
118
  if (lo < num && num < hi) {
92
119
  return true;
93
120
  }
@@ -99,35 +126,53 @@ export class Interval {
99
126
  }
100
127
 
101
128
  toString() {
102
- let { lo, hi } = this;
129
+ let { lo, hi, leftOpen, rightOpen, isEmpty } = this;
130
+ if (isEmpty) {
131
+ return EMPTY_SET;
132
+ }
103
133
  if (lo === hi) {
104
134
  return [
105
- lo === INFINITY ? '(' : '[',
135
+ leftOpen ? '(' : '[',
106
136
  lo === INFINITY ? MINUS_INFINITY : lo,
107
137
  lo == null ? '' : ',',
108
138
  hi === INFINITY ? PLUS_INFINITY : hi,
109
- hi === INFINITY ? ')' : ']',
139
+ rightOpen ? ')' : ']',
110
140
  ].join('');
111
141
  }
112
142
  return [
113
- lo === INFINITY ? '(' : '[',
143
+ leftOpen ? '(' : '[',
114
144
  lo === INFINITY ? MINUS_INFINITY : lo,
115
145
  lo === hi ? '' : ',',
116
146
  hi === INFINITY ? PLUS_INFINITY : hi,
117
- hi === INFINITY ? ')' : ']',
147
+ rightOpen ? ')' : ']',
118
148
  ].join('');
119
149
  }
120
150
 
121
151
  overlaps(iv2) {
122
152
  const msg = 'i6l.overlaps';
123
- let { lo:lo1, hi:hi1 } = this;
124
- let { lo:lo2, hi:hi2 } = iv2;
153
+ const dbg = DBG.I6L_OVERLAPS;
154
+ let { lo: lo1, hi: hi1, leftOpen:lOpen1, rightOpen: rOpen1 } = this;
155
+ let { lo: lo2, hi: hi2, leftOpen:lOpen2, rightOpen: rOpen2 } = iv2;
125
156
 
126
157
  //console.log(msg, {lo1, lo2, hi1, hi2});
127
- return (
128
- (lo2 <= lo1 && lo1 <= hi2) ||
129
- (lo2 <= hi1 && hi1 <= hi2) ||
130
- (lo1 <= lo2 && lo2 <= hi1)
131
- );
158
+ if (!lOpen1 && iv2.contains(lo1)) {
159
+ dbg && cc.fyi1(msg+1, 'lo1');
160
+ return true;
161
+ }
162
+ if (!rOpen1 && iv2.contains(hi1)) {
163
+ dbg && cc.fyi1(msg+2, 'hi1');
164
+ return true;
165
+ }
166
+ if (!lOpen2 && this.contains(lo2)) {
167
+ dbg && cc.fyi1(msg+3, 'lo2');
168
+ return true;
169
+ }
170
+ if (!rOpen2 && this.contains(hi2)) {
171
+ dbg && cc.fyi1(msg+4, 'hi2');
172
+ return true;
173
+ }
174
+
175
+ dbg && cc.fyi1(msg+0.1, false);
176
+ return false;
132
177
  }
133
178
  }
@@ -20,6 +20,8 @@ const {
20
20
  NO_COLOR,
21
21
  } = Unicode.LINUX_COLOR;
22
22
 
23
+ let CC;
24
+
23
25
  export class ColorConsole {
24
26
  constructor(opts = {}) {
25
27
  let {
@@ -35,7 +37,6 @@ export class ColorConsole {
35
37
  colorTag4 = BRIGHT_MAGENTA,
36
38
  precision = 3,
37
39
  write = (...args) => console.log.call(null, ...args),
38
-
39
40
  } = opts;
40
41
 
41
42
  Object.assign(this, {
@@ -51,42 +52,48 @@ export class ColorConsole {
51
52
  colorTag4,
52
53
  precision,
53
54
  write,
54
-
55
55
  });
56
56
  }
57
57
 
58
+ static get cc() {
59
+ CC = CC || new ColorConsole();
60
+ return CC;
61
+ }
62
+
58
63
  color(color, ...things) {
59
64
  let { precision } = this;
60
65
  let label = '';
61
- return things.reduce((a,thing) => {
66
+ return things.reduce((a, thing) => {
62
67
  let newLabel = '';
63
68
  switch (typeof thing) {
64
- case 'object':
69
+ case 'object': {
65
70
  label && a.push(label);
66
- let s = thing &&
67
- (thing.constructor !== Object) &&
68
- (typeof thing.toString) === 'function'
69
- ? color+thing.toString()+NO_COLOR
70
- : thing;
71
+ let s =
72
+ thing &&
73
+ thing.constructor !== Object &&
74
+ typeof thing.toString === 'function'
75
+ ? color + thing.toString() + NO_COLOR
76
+ : thing;
71
77
  a.push(s);
72
78
  break;
79
+ }
73
80
  case 'string':
74
81
  if (thing.endsWith(':')) {
75
- newLabel = NO_COLOR+thing;
82
+ newLabel = NO_COLOR + thing;
76
83
  } else {
77
- a.push(label+color+thing+NO_COLOR);
84
+ a.push(label + color + thing + NO_COLOR);
78
85
  }
79
86
  break;
80
87
  case 'number': {
81
88
  let v = thing.toFixed(precision);
82
89
  if (thing === Number(v)) {
83
- v = v.replace(/\.?0+$/, '')
90
+ v = v.replace(/\.?0+$/, '');
84
91
  }
85
- a.push(label+GREEN+v+NO_COLOR);
92
+ a.push(label + GREEN + v + NO_COLOR);
86
93
  break;
87
94
  }
88
95
  default:
89
- a.push(label+color+JSON.stringify(thing)+NO_COLOR);
96
+ a.push(label + color + JSON.stringify(thing) + NO_COLOR);
90
97
  break;
91
98
  }
92
99
  label = newLabel;
@@ -220,6 +220,9 @@ export class Unicode {
220
220
  static get IMPLIES() {
221
221
  return '\u21D2';
222
222
  }
223
+ static get EMPTY_SET() {
224
+ return '\u2205';
225
+ }
223
226
  static get ELEMENT_OF() {
224
227
  return '\u2208';
225
228
  }
@@ -352,9 +355,12 @@ export class Unicode {
352
355
  get ELLIPSIS() {
353
356
  return Unicode.ELLIPSIS;
354
357
  }
355
- get IMPLIES(){
358
+ get IMPLIES() {
356
359
  return Unicode.IMPLIES;
357
360
  }
361
+ get EMPTY_SET() {
362
+ return Unicode.EMPTY_SET;
363
+ }
358
364
  get ELEMENT_OF() {
359
365
  return Unicode.ELEMENT_OF;
360
366
  }