UNPKG

@panyam/tsutils

Version:

Some basic TS utils for personal use

208 lines 5.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Frac = exports.Fraction = void 0; exports.range = range; exports.gcdof = gcdof; function range(start, end = null, incr = 1) { if (end == null) { const absStart = Math.abs(start); const arr = Array.from({ length: absStart }); if (start >= 0) { return arr.map((x, i) => i); } else { return arr.map((x, i) => i - (absStart - 1)); } } const out = []; if (incr == null) { incr = 1; } incr = Math.abs(incr); if (start !== end) { if (start < end) { for (let i = start; i <= end; i += incr) { out.push(i); } } else { for (let i = start; i >= end; i -= incr) { out.push(i); } } } return out; } function gcdof(x, y) { x = Math.abs(x); y = Math.abs(y); while (y > 0) { const t = y; y = x % y; x = t; } return x; } class Fraction { constructor(num = 0, den = 1, factorized = false) { if (isNaN(num) || isNaN(den)) { throw new Error(`Invalid numerator(${num}) or denminator(${den})`); } if (factorized) { const gcd = gcdof(num, den); num /= gcd; den /= gcd; } this.num = num; this.den = den; } static parse(val, factorized = false) { const parts = val .trim() .split("/") .map((x) => x.trim()); let num = 1; let den = 1; if (parts.length == 1) num = parseInt(parts[0]); else if (parts.length != 2) { throw new Error("Invalid fraction string: " + val); } else { if (parts[0].length > 0) { num = parseInt(parts[0]); } if (parts[1].length > 0) { den = parseInt(parts[1]); } } if (isNaN(num) || isNaN(den)) { throw new Error("Invalid fraction string: " + val); } return new Fraction(num, den, factorized); } get isWhole() { return this.num % this.den == 0; } get isZero() { return this.num == 0; } get isInfinity() { return this.den == 0; } get isOne() { return this.num == this.den; } get ceil() { if (this.num % this.den == 0) { return this.num / this.den; } else { return 1 + Math.floor(this.num / this.den); } } get floor() { if (this.num % this.den == 0) { return this.num / this.den; } else { return Math.floor(this.num / this.den); } } plus(another, factorized = false) { return new Fraction(this.num * another.den + this.den * another.num, this.den * another.den, factorized); } plusNum(another, factorized = false) { return new Fraction(this.num + this.den * another, this.den, factorized); } minus(another, factorized = false) { return new Fraction(this.num * another.den - this.den * another.num, this.den * another.den, factorized); } minusNum(another, factorized = false) { return new Fraction(this.num - this.den * another, this.den, factorized); } times(another, factorized = false) { return new Fraction(this.num * another.num, this.den * another.den, factorized); } timesNum(another, factorized = false) { return new Fraction(this.num * another, this.den, factorized); } divby(another, factorized = false) { return new Fraction(this.num * another.den, this.den * another.num, factorized); } divbyNum(another, factorized = false) { return new Fraction(this.num, this.den * another, factorized); } numDivby(another, factorized = false) { return new Fraction(this.den * another, this.num, factorized); } mod(another) { const d = this.divby(another); const floorOfD = Math.floor(d.num / d.den); return this.minus(another.timesNum(floorOfD)); } modNum(another) { const d = this.divbyNum(another); const floorOfD = Math.floor(d.num / d.den); return this.minusNum(another * floorOfD); } get inverse() { return new Fraction(this.den, this.num); } get factorized() { const gcd = gcdof(this.num, this.den); return new Fraction(this.num / gcd, this.den / gcd); } equals(another) { return this.num * another.den == this.den * another.num; } equalsNum(another) { return this.num == this.den * another; } cmp(another) { return this.num * another.den - this.den * another.num; } cmpNum(another) { return this.num - this.den * another; } isLT(another) { return this.cmp(another) < 0; } isLTE(another) { return this.cmp(another) <= 0; } isLTNum(another) { return this.cmpNum(another) < 0; } isLTENum(another) { return this.cmpNum(another) <= 0; } isGT(another) { return this.cmp(another) > 0; } isGTE(another) { return this.cmp(another) >= 0; } isGTNum(another) { return this.cmpNum(another) > 0; } isGTENum(another) { return this.cmpNum(another) >= 0; } toString() { return this.num + "/" + this.den; } static max(f1, f2) { return f1.cmp(f2) > 0 ? f1 : f2; } static min(f1, f2) { return f1.cmp(f2) < 0 ? f1 : f2; } } exports.Fraction = Fraction; Fraction.ZERO = new Fraction(); Fraction.ONE = new Fraction(1, 1); Fraction.INFINITY = new Fraction(1, 0); const Frac = (a = 0, b = 1, factorized = false) => new Fraction(a, b, factorized); exports.Frac = Frac; //# sourceMappingURL=numberutils.js.map