@sc-voice/tools 3.2.0 → 3.4.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 +2 -0
- package/package.json +1 -1
- package/src/math/interval.mjs +105 -0
- package/src/text/unicode.mjs +3 -0
package/index.mjs
CHANGED
package/package.json
CHANGED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { Unicode } from '../text/unicode.mjs';
|
|
2
|
+
const { INFINITY } = Unicode;
|
|
3
|
+
|
|
4
|
+
const MINUS_INFINITY = `-${INFINITY}`;
|
|
5
|
+
const PLUS_INFINITY = `+${INFINITY}`;
|
|
6
|
+
|
|
7
|
+
export class Interval {
|
|
8
|
+
constructor(a, b) {
|
|
9
|
+
const msg = 'i6l.ctor';
|
|
10
|
+
let hi = null;
|
|
11
|
+
let lo = null;
|
|
12
|
+
const dbg = 0;
|
|
13
|
+
|
|
14
|
+
let an = typeof a === 'number' && !Number.isNaN(a);
|
|
15
|
+
let bn = typeof b === 'number' && !Number.isNaN(b);
|
|
16
|
+
|
|
17
|
+
let isClosed = true;
|
|
18
|
+
if (an && bn) {
|
|
19
|
+
dbg && console.log(msg, 'an bn');
|
|
20
|
+
lo = a;
|
|
21
|
+
hi = b;
|
|
22
|
+
isClosed = true;
|
|
23
|
+
} else if (an) {
|
|
24
|
+
dbg && console.log(msg, 'an');
|
|
25
|
+
lo = a;
|
|
26
|
+
hi = INFINITY;
|
|
27
|
+
isClosed = false;
|
|
28
|
+
} else if (bn) {
|
|
29
|
+
dbg && console.log(msg, 'bn');
|
|
30
|
+
lo = INFINITY;
|
|
31
|
+
hi = b;
|
|
32
|
+
isClosed = false;
|
|
33
|
+
} else {
|
|
34
|
+
dbg && console.log(msg, '!an !bn');
|
|
35
|
+
isClosed = false;
|
|
36
|
+
}
|
|
37
|
+
|
|
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
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
static get INFINITY() { return INFINITY; }
|
|
52
|
+
|
|
53
|
+
get isEmpty() {
|
|
54
|
+
if (this.lo === null && this.hi === null) {
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
if (
|
|
58
|
+
this.lo === INFINITY ||
|
|
59
|
+
this.hi === INFINITY
|
|
60
|
+
) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
return this.hi < this.lo;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
get infimum() {
|
|
67
|
+
return this.lo === INFINITY ? MINUS_INFINITY : this.lo;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
get supremum() {
|
|
71
|
+
return this.hi === INFINITY ? PLUS_INFINITY : this.hi;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
contains(num) {
|
|
75
|
+
if (typeof num !== 'number' || Number.isNaN(num)) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
let { lo, hi } = this;
|
|
79
|
+
if (lo === INFINITY) {
|
|
80
|
+
throw new Error(`${msg}TBD`);
|
|
81
|
+
}
|
|
82
|
+
if (hi === INFINITY) {
|
|
83
|
+
throw new Error(`${msg}TBD`);
|
|
84
|
+
}
|
|
85
|
+
if (lo < num && num < hi) {
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
if (num < lo || hi < num) {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
toString() {
|
|
96
|
+
let { lo, hi } = this;
|
|
97
|
+
return [
|
|
98
|
+
lo === INFINITY ? '(' : '[',
|
|
99
|
+
lo === INFINITY ? MINUS_INFINITY : lo,
|
|
100
|
+
lo === hi ? '' : ',',
|
|
101
|
+
hi === INFINITY ? PLUS_INFINITY : hi,
|
|
102
|
+
hi === INFINITY ? ')' : ']',
|
|
103
|
+
].join('');
|
|
104
|
+
}
|
|
105
|
+
}
|