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 894ee21..6ddb36d 100644 Binary files a/package-lock.json and b/package-lock.json differ diff --git a/package.json b/package.json index 775e216..aef1b82 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "simplify-fractions", - "version": "1.2.5", + "version": "1.2.6", "description": "Simple web tool for simplifying fractions", - "author": "Felix W. Dekker", + "author": "F.W. Dekker", "browser": "dist/bundle.js", "repository": { "type": "git", @@ -16,7 +16,7 @@ "deploy": "grunt deploy" }, "dependencies": { - "katex": "^0.13.3" + "katex": "^0.13.9" }, "devDependencies": { "css-loader": "^5.2.4", @@ -30,7 +30,7 @@ "grunt-text-replace": "^0.4.0", "grunt-webpack": "^4.0.3", "style-loader": "^2.0.0", - "webpack": "^5.36.0", - "webpack-cli": "^4.6.0" + "webpack": "^5.36.2", + "webpack-cli": "^4.7.0" } } diff --git a/src/main/index.html b/src/main/index.html index e4aa293..3889a04 100644 --- a/src/main/index.html +++ b/src/main/index.html @@ -3,7 +3,7 @@ - + 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