@signpostmarv/intermediary-number 0.5.1 → 0.6.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.js +0 -1
- package/lib/Docs.json.js +0 -1
- package/lib/IntermediaryNumber.d.ts +12 -1
- package/lib/IntermediaryNumber.js +105 -10
- package/lib/IntermediaryNumberTypes.js +0 -1
- package/lib/NumberStrings.js +0 -1
- package/lib/Numbers.js +0 -1
- package/package.json +6 -6
- package/eslint.config.mjs +0 -20
package/index.js
CHANGED
package/lib/Docs.json.js
CHANGED
|
@@ -21,7 +21,7 @@ export type CanConvertTypeJson = {
|
|
|
21
21
|
};
|
|
22
22
|
export type CanDoMathWithDispose_operator_types = 'divide' | 'minus' | 'modulo' | 'plus' | 'times';
|
|
23
23
|
export type operand_types = IntermediaryNumber | IntermediaryCalculation | TokenScan;
|
|
24
|
-
type TokenSpan_types = 'ignore' | 'nesting_open' | 'nesting_close' | 'numeric' | 'operation';
|
|
24
|
+
type TokenSpan_types = 'ignore' | 'nesting_open' | 'nesting_close' | 'numeric' | 'operation' | 'Infinity';
|
|
25
25
|
type TokenSpan_types_part_baked = Exclude<TokenSpan_types, 'ignore'>;
|
|
26
26
|
type TokenScan_internals = {
|
|
27
27
|
parsed: IntermediaryNumber | IntermediaryCalculation | undefined;
|
|
@@ -111,6 +111,15 @@ export declare class IntermediaryNumber implements CanDoMathWithDispose {
|
|
|
111
111
|
static fromJson(json: CanConvertTypeJson): CanDoMath_result_types;
|
|
112
112
|
static reuse_or_create(input: operand_types | input_types): operand_types;
|
|
113
113
|
}
|
|
114
|
+
export declare class IntermediaryNumberInfinity extends IntermediaryNumber {
|
|
115
|
+
static readonly One: IntermediaryNumberInfinity;
|
|
116
|
+
static readonly Zero: IntermediaryNumberInfinity;
|
|
117
|
+
isOne(): boolean;
|
|
118
|
+
isZero(): boolean;
|
|
119
|
+
toFraction(): Fraction;
|
|
120
|
+
toString(): string;
|
|
121
|
+
toStringCalculation(): string;
|
|
122
|
+
}
|
|
114
123
|
export declare class NotValid extends Error {
|
|
115
124
|
readonly reason: unknown;
|
|
116
125
|
readonly value: string;
|
|
@@ -121,6 +130,7 @@ export declare class IntermediaryCalculation implements CanResolveMathWithDispos
|
|
|
121
130
|
readonly operation: operation_types;
|
|
122
131
|
readonly right_operand: operand_types;
|
|
123
132
|
constructor(left: operand_types, operation: operation_types, right: operand_types);
|
|
133
|
+
get has_infinity(): boolean;
|
|
124
134
|
get left_type(): operand_type_property_types;
|
|
125
135
|
get resolve_type(): string;
|
|
126
136
|
get right_type(): operand_type_property_types;
|
|
@@ -169,6 +179,7 @@ export declare class TokenScanParseError extends Error {
|
|
|
169
179
|
constructor(message: string, scan: TokenScan_parsing_value, state: TokenScan_tokenizer, current?: TokenSpan<TokenSpan_types>);
|
|
170
180
|
}
|
|
171
181
|
export declare class TokenScan implements CanResolveMathWithDispose {
|
|
182
|
+
#private;
|
|
172
183
|
private readonly internal;
|
|
173
184
|
readonly value: string | [TokenScan, operation_types, math_types];
|
|
174
185
|
private constructor();
|
|
@@ -115,6 +115,9 @@ function is_nesting_close(maybe) {
|
|
|
115
115
|
function is_numeric(maybe) {
|
|
116
116
|
return 'numeric' === maybe.type;
|
|
117
117
|
}
|
|
118
|
+
function is_infinity(maybe) {
|
|
119
|
+
return 'Infinity' === maybe.type;
|
|
120
|
+
}
|
|
118
121
|
export function is_operation_value(maybe) {
|
|
119
122
|
if (!(maybe.length === 1
|
|
120
123
|
&& '+-/x*%'.includes(maybe))) {
|
|
@@ -317,6 +320,9 @@ export class IntermediaryNumber {
|
|
|
317
320
|
}
|
|
318
321
|
return new this(new Fraction(input));
|
|
319
322
|
}
|
|
323
|
+
else if ('Infinity' === input) {
|
|
324
|
+
return new IntermediaryNumberInfinity(new BigNumber('Infinity'));
|
|
325
|
+
}
|
|
320
326
|
throw new Error('Unsupported argument specified!');
|
|
321
327
|
}
|
|
322
328
|
static create_if_valid(input) {
|
|
@@ -358,6 +364,27 @@ export class IntermediaryNumber {
|
|
|
358
364
|
}
|
|
359
365
|
}
|
|
360
366
|
//#endregion
|
|
367
|
+
//#region IntermediaryNumberInfinity
|
|
368
|
+
export class IntermediaryNumberInfinity extends IntermediaryNumber {
|
|
369
|
+
static One = new this(new BigNumber('Infinity'));
|
|
370
|
+
static Zero = new this(new BigNumber('Infinity'));
|
|
371
|
+
isOne() {
|
|
372
|
+
return false;
|
|
373
|
+
}
|
|
374
|
+
isZero() {
|
|
375
|
+
return false;
|
|
376
|
+
}
|
|
377
|
+
toFraction() {
|
|
378
|
+
throw new Error('Cannot convert infinity to Fraction');
|
|
379
|
+
}
|
|
380
|
+
toString() {
|
|
381
|
+
return 'Infinity';
|
|
382
|
+
}
|
|
383
|
+
toStringCalculation() {
|
|
384
|
+
return 'Infinity';
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
//#endregion
|
|
361
388
|
//#region IntermediaryCalculation
|
|
362
389
|
export class NotValid extends Error {
|
|
363
390
|
reason;
|
|
@@ -392,6 +419,10 @@ export class IntermediaryCalculation {
|
|
|
392
419
|
this.operation = operation;
|
|
393
420
|
this.right_operand = right;
|
|
394
421
|
}
|
|
422
|
+
get has_infinity() {
|
|
423
|
+
return (this.left_operand instanceof IntermediaryNumberInfinity
|
|
424
|
+
|| this.right_operand instanceof IntermediaryNumberInfinity);
|
|
425
|
+
}
|
|
395
426
|
get left_type() {
|
|
396
427
|
if (this.left_operand instanceof IntermediaryCalculation) {
|
|
397
428
|
return 'IntermediaryCalculation';
|
|
@@ -457,6 +488,10 @@ export class IntermediaryCalculation {
|
|
|
457
488
|
return do_math(this, '+', value);
|
|
458
489
|
}
|
|
459
490
|
resolve() {
|
|
491
|
+
const reduced = IntermediaryCalculation.maybe_short_circuit(this.left_operand, this.operation, this.right_operand);
|
|
492
|
+
if (reduced instanceof IntermediaryNumber) {
|
|
493
|
+
return reduced;
|
|
494
|
+
}
|
|
460
495
|
const left_operand = this.operand_to_IntermediaryNumber(this.left_operand);
|
|
461
496
|
const right_operand = this.operand_to_IntermediaryNumber(this.right_operand);
|
|
462
497
|
const left = left_operand.toBigNumberOrFraction();
|
|
@@ -506,8 +541,14 @@ export class IntermediaryCalculation {
|
|
|
506
541
|
return value;
|
|
507
542
|
}
|
|
508
543
|
toJSON() {
|
|
509
|
-
const left =
|
|
510
|
-
|
|
544
|
+
const left = (this.left_operand instanceof IntermediaryCalculation
|
|
545
|
+
&& this.left_operand.has_infinity)
|
|
546
|
+
? this.left_operand
|
|
547
|
+
: this.operand_to_IntermediaryNumber(this.left_operand);
|
|
548
|
+
const right = (this.right_operand instanceof IntermediaryCalculation
|
|
549
|
+
&& this.right_operand.has_infinity)
|
|
550
|
+
? this.right_operand
|
|
551
|
+
: this.operand_to_IntermediaryNumber(this.right_operand);
|
|
511
552
|
const maybe = IntermediaryCalculation.maybe_short_circuit(left, this.operation, right);
|
|
512
553
|
if (maybe) {
|
|
513
554
|
return maybe.toJSON();
|
|
@@ -571,6 +612,55 @@ export class IntermediaryCalculation {
|
|
|
571
612
|
}
|
|
572
613
|
static maybe_short_circuit(left, operation, right) {
|
|
573
614
|
let value = undefined;
|
|
615
|
+
if (left instanceof IntermediaryNumberInfinity
|
|
616
|
+
&& right instanceof IntermediaryNumberInfinity) {
|
|
617
|
+
if ('+' === operation
|
|
618
|
+
|| '*' === operation) {
|
|
619
|
+
// infinity plus or multiplied by infintiy is infinity
|
|
620
|
+
return left;
|
|
621
|
+
}
|
|
622
|
+
else if ('-' === operation) {
|
|
623
|
+
return IntermediaryNumber.Zero;
|
|
624
|
+
}
|
|
625
|
+
else if ('/' === operation) {
|
|
626
|
+
return IntermediaryNumber.One;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
if (left instanceof IntermediaryCalculation
|
|
630
|
+
&& left.has_infinity
|
|
631
|
+
&& (('+' === left.operation
|
|
632
|
+
&& '-' === operation)
|
|
633
|
+
|| ('-' === left.operation
|
|
634
|
+
&& '+' === operation))
|
|
635
|
+
&& (right instanceof IntermediaryNumberInfinity)
|
|
636
|
+
&& (!(left.left_operand instanceof IntermediaryNumberInfinity)
|
|
637
|
+
|| !(left.right_operand instanceof IntermediaryNumberInfinity))) {
|
|
638
|
+
return (left.left_operand instanceof IntermediaryNumberInfinity
|
|
639
|
+
? left.right_operand
|
|
640
|
+
: left.left_operand);
|
|
641
|
+
}
|
|
642
|
+
else if (right instanceof IntermediaryCalculation
|
|
643
|
+
&& right.has_infinity
|
|
644
|
+
&& (('+' === right.operation
|
|
645
|
+
&& '-' === operation)
|
|
646
|
+
|| ('-' === right.operation
|
|
647
|
+
&& '+' === operation))
|
|
648
|
+
&& (left instanceof IntermediaryNumberInfinity)
|
|
649
|
+
&& (!(right.left_operand instanceof IntermediaryNumberInfinity)
|
|
650
|
+
|| !(right.right_operand instanceof IntermediaryNumberInfinity))) {
|
|
651
|
+
return (right.left_operand instanceof IntermediaryNumberInfinity
|
|
652
|
+
? right.right_operand
|
|
653
|
+
: right.left_operand);
|
|
654
|
+
}
|
|
655
|
+
else if ('/' === operation
|
|
656
|
+
&& !right.isOne()
|
|
657
|
+
&& left instanceof IntermediaryCalculation
|
|
658
|
+
&& left.has_infinity
|
|
659
|
+
&& !(left.left_operand instanceof IntermediaryNumberInfinity)
|
|
660
|
+
&& '*x'.includes(left.operation)
|
|
661
|
+
&& right instanceof IntermediaryNumberInfinity) {
|
|
662
|
+
return left.left_operand;
|
|
663
|
+
}
|
|
574
664
|
if ('+' === operation) {
|
|
575
665
|
if (left.isZero()) {
|
|
576
666
|
value = right;
|
|
@@ -633,10 +723,7 @@ export class TokenScan {
|
|
|
633
723
|
this.value = value;
|
|
634
724
|
}
|
|
635
725
|
get parsed() {
|
|
636
|
-
|
|
637
|
-
this.internal.parsed = TokenScan.parse_scan(this);
|
|
638
|
-
}
|
|
639
|
-
return this.internal.parsed;
|
|
726
|
+
return this.#parse();
|
|
640
727
|
}
|
|
641
728
|
get resolve_type() {
|
|
642
729
|
return this.parsed.resolve_type;
|
|
@@ -653,15 +740,21 @@ export class TokenScan {
|
|
|
653
740
|
get valid() {
|
|
654
741
|
if (undefined === this.internal.valid) {
|
|
655
742
|
try {
|
|
656
|
-
this
|
|
743
|
+
this.#parse();
|
|
657
744
|
this.internal.valid = true;
|
|
658
745
|
}
|
|
659
|
-
catch
|
|
746
|
+
catch {
|
|
660
747
|
this.internal.valid = false;
|
|
661
748
|
}
|
|
662
749
|
}
|
|
663
750
|
return this.internal.valid;
|
|
664
751
|
}
|
|
752
|
+
#parse() {
|
|
753
|
+
if (undefined === this.internal.parsed) {
|
|
754
|
+
this.internal.parsed = TokenScan.parse_scan(this);
|
|
755
|
+
}
|
|
756
|
+
return this.internal.parsed;
|
|
757
|
+
}
|
|
665
758
|
abs() {
|
|
666
759
|
return this.parsed.abs();
|
|
667
760
|
}
|
|
@@ -808,6 +901,9 @@ export class TokenScan {
|
|
|
808
901
|
for (const entry of value.matchAll(/(\))/g)) {
|
|
809
902
|
tokens.push(new TokenSpan(entry.index, entry.index + entry[0].length, 'nesting_close'));
|
|
810
903
|
}
|
|
904
|
+
for (const entry of value.matchAll(/(\bInfinity\b)/g)) {
|
|
905
|
+
tokens.push(new TokenSpan(entry.index, entry.index + entry[0].length, 'Infinity'));
|
|
906
|
+
}
|
|
811
907
|
tokens = tokens.sort((a, b) => {
|
|
812
908
|
return a.from - b.from;
|
|
813
909
|
});
|
|
@@ -971,7 +1067,7 @@ export class TokenScan {
|
|
|
971
1067
|
}
|
|
972
1068
|
}
|
|
973
1069
|
}
|
|
974
|
-
else if (is_numeric(is)) {
|
|
1070
|
+
else if (is_numeric(is) || is_infinity(is)) {
|
|
975
1071
|
if ('left' === was.operand_mode) {
|
|
976
1072
|
was.left_operand = IntermediaryNumber.create(value.substring(is.from, is.to));
|
|
977
1073
|
was.operand_mode = 'right';
|
|
@@ -1012,4 +1108,3 @@ export class TokenScan {
|
|
|
1012
1108
|
}
|
|
1013
1109
|
}
|
|
1014
1110
|
//#endregion
|
|
1015
|
-
//# sourceMappingURL=IntermediaryNumber.js.map
|
package/lib/NumberStrings.js
CHANGED
package/lib/Numbers.js
CHANGED
package/package.json
CHANGED
|
@@ -11,21 +11,21 @@
|
|
|
11
11
|
"exports": "./index.js",
|
|
12
12
|
"types": "./index.d.ts",
|
|
13
13
|
"devDependencies": {
|
|
14
|
-
"@
|
|
15
|
-
"@
|
|
14
|
+
"@satisfactory-dev/custom-assert": "^0.2.3",
|
|
15
|
+
"@signpostmarv/eslint-config": "^0.2.0",
|
|
16
|
+
"@types/eslint": "^9.6.0",
|
|
16
17
|
"@types/node": "^20.14.2",
|
|
17
18
|
"c8": "^10.1.2",
|
|
18
|
-
"eslint": "^
|
|
19
|
+
"eslint": "^9.9.1",
|
|
19
20
|
"glob": "^10.4.1",
|
|
20
21
|
"prettier": "^3.3.2",
|
|
21
22
|
"ts-node": "^10.9.2",
|
|
22
23
|
"typescript": "~5.5.2",
|
|
23
|
-
"typescript-eslint": "^
|
|
24
|
+
"typescript-eslint": "^8.2.0"
|
|
24
25
|
},
|
|
25
26
|
"dependencies": {
|
|
26
|
-
"@satisfactory-dev/custom-assert": "^0.2.2",
|
|
27
27
|
"bignumber.js": "^9.1.2",
|
|
28
28
|
"fraction.js": "^4.3.7"
|
|
29
29
|
},
|
|
30
|
-
"version": "0.
|
|
30
|
+
"version": "0.6.0"
|
|
31
31
|
}
|
package/eslint.config.mjs
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import config from '@signpostmarv/eslint-config';
|
|
2
|
-
import parser from '@typescript-eslint/parser';
|
|
3
|
-
|
|
4
|
-
export default [
|
|
5
|
-
{
|
|
6
|
-
languageOptions: {
|
|
7
|
-
parser,
|
|
8
|
-
parserOptions: {
|
|
9
|
-
project: ['./tsconfig.eslint.json'],
|
|
10
|
-
},
|
|
11
|
-
},
|
|
12
|
-
},
|
|
13
|
-
...config,
|
|
14
|
-
{
|
|
15
|
-
ignores: [
|
|
16
|
-
'**/*.js',
|
|
17
|
-
'**/*.d.ts',
|
|
18
|
-
]
|
|
19
|
-
}
|
|
20
|
-
];
|