From ad979c8680ec035e70ce6f80076147cda8edd8e0 Mon Sep 17 00:00:00 2001 From: "Felix W. Dekker" Date: Sat, 8 May 2021 16:59:42 +0200 Subject: [PATCH] Reorder some code and add new documentation --- LICENSE | 2 +- package-lock.json | Bin 234763 -> 233147 bytes package.json | 10 ++-- src/main/index.html | 2 +- src/main/js/main.js | 127 +++++++++++++++++++++++--------------------- 5 files changed, 72 insertions(+), 69 deletions(-) diff --git a/LICENSE b/LICENSE index 2544f7d..9b08106 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2017 Felix W. Dekker +Copyright (c) 2017 F.W. Dekker Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/package-lock.json b/package-lock.json index 894ee219f99a064aeb420fb844ec52026666c180..6ddb36dac98d3743eedeec95034d9910b17f7ff4 100644 GIT binary patch delta 1706 zcmchW+iN3b6u|jrT-&a;4ZUn@E7De0H#@eO+@_PIi<_po%p{ZRB$;$Yn!$hoH>f0JPG;dw1!AE6bafeM{cEUZJ44AQby4k*F(7%jvZ>q?#FgG{5y<>Fk1 z=bE-nr%xzFf;GP_S}G*PhHPM0(vG4MjTdOUyXvZ@b>^skgIbRiL?;vgsB~uJwpl zutlvFugP0Z`kQK@o+>D|O37`Pwo+L^4yufs&2Pk2&L5N6)kf55_Az*W$A{mXKY+FC z#D&hPgLrpA1MmGsP+}u3>RctBQaw?pL*B#%@F4aCA#^sH~{b#QG0J%QI=7{<5eIeZxFMh)~I zp}E#QoqnM8&V}!mS{Jttc41#4S$K8|jl%IIqzxO&O{!{hRU@^4&motLnVLb@aQI4c zAm3z!s90Y$FB?rc#bT2zmW)p{G?`d3yOj}F9Ch#z{)=Y@CCpnj7ro;^2 z)S3B2A|LFW-;4eI1MS;~=>93R2&G?<72e+_$2u6sm(GV5*WG1{o;BzyPC4tyQ;pMYDH@M5JF509^US6WIb%yqYjZD_6R857vkV`2H6*XJk z8^v7r>0Y>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+GU0JqO_olt^T+G$Nm2^Il=ypQ+gg2zn+?GoQ9D(P) z0ZEv915E0si6QvJcVHB*oksUU^eyCXq2EuVd*H7>f^itX4!E&D-7|gMH2vhjHggmN zSHWce&FesWY@5L2gu9K15nh=@_G}0q(POiRwv9fPaU?jPk7;=rIS8*#BIfPI5Il1a zIQxV8e;K(Ch@-FXBKyA`LImsmK^nL|fd>DHnuGAEGBVZY63BxuY!hb=K*K|b3BGRx zrUBw6`zyDQ@T>m<9Pa-B4t(|=$n*^wiVbZ)SAzdFQ_NuS<9QgqX9T - + diff --git a/src/main/js/main.js b/src/main/js/main.js index a0bccfd..dda9943 100644 --- a/src/main/js/main.js +++ b/src/main/js/main.js @@ -4,67 +4,6 @@ import katex from "katex"; import "katex/dist/katex.min.css"; -/** - * A fraction that can be simplified. - */ -class Fraction { - constructor(numerator, denominator) { - if (!isInt(numerator) || !isInt(denominator)) - throw new Error("Numerator and denominator must be integer-like."); - - this.sign = numerator < 0 !== denominator < 0 ? -1 : 1; - this.numerator = Math.abs(+numerator); - this.denominator = Math.abs(+denominator); - } - - - /** - * Returns a new fraction such that the gcd of the numerator and denominator is 1. - * - * @returns {Fraction} a new fraction such that the gcd of the numerator and denominator is 1 - */ - simplify() { - const common = gcd(this.numerator, this.denominator); - return new Fraction(this.sign * this.numerator / common, this.denominator / common); - } - - /** - * Returns the LaTeX string representation of this fraction. - * - * @returns {string} the LaTeX string representation of this fraction - */ - toString() { - let frac = `\\frac{${this.numerator}}{${this.denominator}}`; - if (this.sign === -1) - frac = `-${frac}`; - - return frac; - } - - /** - * Returns the LaTeX string representation of this fraction, or of the numerator if the denominator is 1. - * - * @returns {string} the LaTeX string representation of this fraction, or of the numerator if the denominator - * is 1. - */ - toReducedString() { - if (this.numerator === 0) - return "0"; - - let frac; - if (this.denominator === 1) - frac = `${this.numerator}`; - else - frac = `\\frac{${this.numerator}}{${this.denominator}}`; - - if (this.sign === -1) - frac = `-${frac}`; - - return frac; - } -} - - // noinspection EqualityComparisonWithCoercionJS /** * Returns `true` if and only if `n` is an integer. @@ -98,6 +37,68 @@ const gcd = (a, b) => { }; +/** + * A fraction that can be simplified. + */ +class Fraction { + constructor(numerator, denominator) { + if (!isInt(numerator) || !isInt(denominator)) + throw new Error("Numerator and denominator must be integer-like."); + + this.sign = numerator < 0 !== denominator < 0 ? -1 : 1; + this.numerator = Math.abs(+numerator); + this.denominator = Math.abs(+denominator); + } + + + /** + * Returns a new fraction such that the gcd of the numerator and denominator is 1. + * + * @returns {Fraction} a new fraction such that the gcd of the numerator and denominator is 1 + */ + simplifyGcd() { + const common = gcd(this.numerator, this.denominator); + return new Fraction(this.sign * this.numerator / common, this.denominator / common); + } + + /** + * Returns the LaTeX string representation of this fraction. + * + * Unlike `#toReducedString`, this method never simplifies the output to an integer. + * + * @returns {string} the LaTeX string representation of this fraction + */ + toString() { + let frac = `\\frac{${this.numerator}}{${this.denominator}}`; + if (this.sign === -1) + frac = `-${frac}`; + + return frac; + } + + /** + * Returns the LaTeX string representation of this fraction, or of the numerator if the denominator is 1. + * + * @returns {string} the string representation of the reduced form of this fraction + */ + toReducedString() { + if (this.numerator === 0) + return "0"; + + let frac; + if (this.denominator === 1) + frac = `${this.numerator}`; + else + frac = `\\frac{${this.numerator}}{${this.denominator}}`; + + if (this.sign === -1) + frac = `-${frac}`; + + return frac; + } +} + + doAfterLoad(() => { $("#nav").appendChild(nav("/Tools/Simplify Fractions/")); $("#header").appendChild(header({ @@ -108,7 +109,9 @@ doAfterLoad(() => { vcsURL: "https://git.fwdekker.com/FWDekker/simplify-fractions/", version: "v%%VERSION_NUMBER%%" })); + $("main").classList.remove("hidden"); + $("[autofocus]").focus() }); doAfterLoad(() => { @@ -156,7 +159,7 @@ doAfterLoad(() => { const fraction = new Fraction(numeratorInput.value, denominatorInput.value); outputField.innerHTML = katex.renderToString( - fraction.toString() + " = " + fraction.simplify().toReducedString(), + fraction.toString() + " = " + fraction.simplifyGcd().toReducedString(), { displayMode: true, throwOnError: false