@sc-voice/tools 3.13.0 → 3.14.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 +1 -1
- package/src/defines.mjs +2 -0
- package/src/math/interval.mjs +59 -17
- package/src/text/color-console.mjs +21 -14
- package/src/text/unicode.mjs +1 -1
package/package.json
CHANGED
package/src/defines.mjs
CHANGED
package/src/math/interval.mjs
CHANGED
|
@@ -1,20 +1,29 @@
|
|
|
1
1
|
import { Unicode } from '../text/unicode.mjs';
|
|
2
2
|
const { 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
|
}
|
|
@@ -120,14 +147,29 @@ export class Interval {
|
|
|
120
147
|
|
|
121
148
|
overlaps(iv2) {
|
|
122
149
|
const msg = 'i6l.overlaps';
|
|
123
|
-
|
|
124
|
-
let { lo:
|
|
150
|
+
const dbg = DBG.I6L_OVERLAPS;
|
|
151
|
+
let { lo: lo1, hi: hi1, leftOpen:lOpen1, rightOpen: rOpen1 } = this;
|
|
152
|
+
let { lo: lo2, hi: hi2, leftOpen:lOpen2, rightOpen: rOpen2 } = iv2;
|
|
125
153
|
|
|
126
154
|
//console.log(msg, {lo1, lo2, hi1, hi2});
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
)
|
|
155
|
+
if (!lOpen1 && iv2.contains(lo1)) {
|
|
156
|
+
dbg && cc.fyi1(msg+1, 'lo1');
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
if (!rOpen1 && iv2.contains(hi1)) {
|
|
160
|
+
dbg && cc.fyi1(msg+2, 'hi1');
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
if (!lOpen2 && this.contains(lo2)) {
|
|
164
|
+
dbg && cc.fyi1(msg+3, 'lo2');
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
if (!rOpen2 && this.contains(hi2)) {
|
|
168
|
+
dbg && cc.fyi1(msg+4, 'hi2');
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
dbg && cc.fyi1(msg+0.1, false);
|
|
173
|
+
return false;
|
|
132
174
|
}
|
|
133
175
|
}
|
|
@@ -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 =
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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;
|