@sc-voice/tools 3.12.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 +63 -17
- package/src/text/color-console.mjs +22 -11
- 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;
|
|
@@ -73,17 +91,30 @@ export class Interval {
|
|
|
73
91
|
return this.hi === INFINITY ? PLUS_INFINITY : this.hi;
|
|
74
92
|
}
|
|
75
93
|
|
|
94
|
+
get degenerate() {
|
|
95
|
+
return this.hi === this.lo;
|
|
96
|
+
}
|
|
97
|
+
|
|
76
98
|
contains(num) {
|
|
99
|
+
const msg = 'i6l.contains';
|
|
100
|
+
const dbg = DBG.I6L_CONTAINS;
|
|
77
101
|
if (typeof num !== 'number' || Number.isNaN(num)) {
|
|
102
|
+
dbg && cc.fyi1(msg+0.1, false);
|
|
78
103
|
return false;
|
|
79
104
|
}
|
|
80
|
-
let { lo, hi } = this;
|
|
105
|
+
let { lo, hi, leftOpen, rightOpen } = this;
|
|
81
106
|
if (lo === INFINITY) {
|
|
82
107
|
throw new Error(`${msg}TBD`);
|
|
83
108
|
}
|
|
84
109
|
if (hi === INFINITY) {
|
|
85
110
|
throw new Error(`${msg}TBD`);
|
|
86
111
|
}
|
|
112
|
+
if (lo === num && leftOpen) {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
if (hi === num && rightOpen) {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
87
118
|
if (lo < num && num < hi) {
|
|
88
119
|
return true;
|
|
89
120
|
}
|
|
@@ -116,14 +147,29 @@ export class Interval {
|
|
|
116
147
|
|
|
117
148
|
overlaps(iv2) {
|
|
118
149
|
const msg = 'i6l.overlaps';
|
|
119
|
-
|
|
120
|
-
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;
|
|
121
153
|
|
|
122
154
|
//console.log(msg, {lo1, lo2, hi1, hi2});
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
)
|
|
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;
|
|
128
174
|
}
|
|
129
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,38 +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':
|
|
65
|
-
// TODO: pretty objects like console
|
|
69
|
+
case 'object': {
|
|
66
70
|
label && a.push(label);
|
|
67
|
-
|
|
71
|
+
let s =
|
|
72
|
+
thing &&
|
|
73
|
+
thing.constructor !== Object &&
|
|
74
|
+
typeof thing.toString === 'function'
|
|
75
|
+
? color + thing.toString() + NO_COLOR
|
|
76
|
+
: thing;
|
|
77
|
+
a.push(s);
|
|
68
78
|
break;
|
|
79
|
+
}
|
|
69
80
|
case 'string':
|
|
70
81
|
if (thing.endsWith(':')) {
|
|
71
|
-
newLabel = NO_COLOR+thing;
|
|
82
|
+
newLabel = NO_COLOR + thing;
|
|
72
83
|
} else {
|
|
73
|
-
a.push(label+color+thing+NO_COLOR);
|
|
84
|
+
a.push(label + color + thing + NO_COLOR);
|
|
74
85
|
}
|
|
75
86
|
break;
|
|
76
87
|
case 'number': {
|
|
77
88
|
let v = thing.toFixed(precision);
|
|
78
89
|
if (thing === Number(v)) {
|
|
79
|
-
v = v.replace(/\.?0+$/, '')
|
|
90
|
+
v = v.replace(/\.?0+$/, '');
|
|
80
91
|
}
|
|
81
|
-
a.push(label+GREEN+v+NO_COLOR);
|
|
92
|
+
a.push(label + GREEN + v + NO_COLOR);
|
|
82
93
|
break;
|
|
83
94
|
}
|
|
84
95
|
default:
|
|
85
|
-
a.push(label+color+JSON.stringify(thing)+NO_COLOR);
|
|
96
|
+
a.push(label + color + JSON.stringify(thing) + NO_COLOR);
|
|
86
97
|
break;
|
|
87
98
|
}
|
|
88
99
|
label = newLabel;
|