@signpostmarv/intermediary-number 0.5.2 → 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/lib/IntermediaryNumber.d.ts +11 -1
- package/lib/IntermediaryNumber.js +96 -3
- package/package.json +1 -1
|
@@ -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;
|
|
@@ -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;
|
|
@@ -811,6 +901,9 @@ export class TokenScan {
|
|
|
811
901
|
for (const entry of value.matchAll(/(\))/g)) {
|
|
812
902
|
tokens.push(new TokenSpan(entry.index, entry.index + entry[0].length, 'nesting_close'));
|
|
813
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
|
+
}
|
|
814
907
|
tokens = tokens.sort((a, b) => {
|
|
815
908
|
return a.from - b.from;
|
|
816
909
|
});
|
|
@@ -974,7 +1067,7 @@ export class TokenScan {
|
|
|
974
1067
|
}
|
|
975
1068
|
}
|
|
976
1069
|
}
|
|
977
|
-
else if (is_numeric(is)) {
|
|
1070
|
+
else if (is_numeric(is) || is_infinity(is)) {
|
|
978
1071
|
if ('left' === was.operand_mode) {
|
|
979
1072
|
was.left_operand = IntermediaryNumber.create(value.substring(is.from, is.to));
|
|
980
1073
|
was.operand_mode = 'right';
|
package/package.json
CHANGED