Reorder some code and add new documentation

This commit is contained in:
Florine W. Dekker 2021-05-08 16:59:42 +02:00
parent 26bad7b2fc
commit ad979c8680
Signed by: FWDekker
GPG Key ID: 78B3EAF58145AF25
5 changed files with 72 additions and 69 deletions

View File

@ -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

BIN
package-lock.json generated

Binary file not shown.

View File

@ -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"
}
}

View File

@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="author" content="Felix W. Dekker" />
<meta name="author" content="F.W. Dekker" />
<meta name="application-name" content="Simplify fractions" />
<meta name="description" content="Simplify fractions." />
<meta name="theme-color" content="#0033cc" />

View File

@ -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