From 47102bdde4d8bf8f6d0d24b8aef3b50b41ed9949 Mon Sep 17 00:00:00 2001 From: Robert Eisele Date: Tue, 29 Aug 2023 18:37:16 +0200 Subject: [PATCH] Native ES module support --- fraction.js => fraction.cjs | 19 +++--- fraction.min.js | 18 +++--- package.json | 93 ++++++++++++++------------- tests/fraction.test.js | 121 ++++++++++++++++++------------------ 4 files changed, 130 insertions(+), 121 deletions(-) rename fraction.js => fraction.cjs (97%) diff --git a/fraction.js b/fraction.cjs similarity index 97% rename from fraction.js rename to fraction.cjs index 5f0d305..d15cc08 100644 --- a/fraction.js +++ b/fraction.cjs @@ -1,5 +1,5 @@ /** - * @license Fraction.js v4.2.1 20/08/2023 + * @license Fraction.js v4.3.0 20/08/2023 * https://www.xarg.org/2014/03/rational-numbers-in-javascript/ * * Copyright (c) 2023, Robert Eisele (robert@raw.org) @@ -377,9 +377,9 @@ } } - var DivisionByZero = function() {return new Error("Division by Zero");}; - var InvalidParameter = function() {return new Error("Invalid argument");}; - var NonIntegerParameter = function() {return new Error("Parameters must be integer");}; + var DivisionByZero = function() { return new Error("Division by Zero"); }; + var InvalidParameter = function() { return new Error("Invalid argument"); }; + var NonIntegerParameter = function() { return new Error("Parameters must be integer"); }; Fraction.prototype = { @@ -875,14 +875,11 @@ } }; - if (typeof define === "function" && define["amd"]) { - define([], function() { - return Fraction; + if (typeof exports === "object") { + Object.defineProperty(exports, "__esModule", { + value: true }); - } else if (typeof exports === "object") { - Object.defineProperty(Fraction, "__esModule", { 'value': true }); - Fraction['default'] = Fraction; - Fraction['Fraction'] = Fraction; + exports["default"] = Fraction; module['exports'] = Fraction; } else { root['Fraction'] = Fraction; diff --git a/fraction.min.js b/fraction.min.js index 3007bb8..4281cf7 100644 --- a/fraction.min.js +++ b/fraction.min.js @@ -1,18 +1,18 @@ /* -Fraction.js v4.2.1 20/08/2023 +Fraction.js v4.3.0 20/08/2023 https://www.xarg.org/2014/03/rational-numbers-in-javascript/ Copyright (c) 2023, Robert Eisele (robert@raw.org) Dual licensed under the MIT or GPL Version 2 licenses. */ -(function(B){function x(){return Error("Invalid argument")}function z(){return Error("Division by Zero")}function p(a,c){var b=0,d=1,f=1,l=0,k=0,t=0,y=1,u=1,g=0,h=1,v=1,q=1;if(void 0!==a&&null!==a)if(void 0!==c){if(b=a,d=c,f=b*d,0!==b%1||0!==d%1)throw Error("Parameters must be integer");}else switch(typeof a){case "object":if("d"in a&&"n"in a)b=a.n,d=a.d,"s"in a&&(b*=a.s);else if(0 in a)b=a[0],1 in a&&(d=a[1]);else throw x();f=b*d;break;case "number":0>a&&(f=a,a=-a);if(0===a%1)b=a;else if(0a&&(f=a,a=-a);if(0===a%1)b=a;else if(0=h&&1E7>=q;)if(b=(g+v)/(h+q),a===b){1E7>=h+q?(b=g+v,d=h+q):q>h?(b=v,d=q):(b=g,d=h);break}else a>b?(g+=v,h+=q):(v+=g,q+=h),1E7f?-1:1;e.n=Math.abs(b);e.d=Math.abs(d)}function r(a,c){if(isNaN(a=parseInt(a,10)))throw x();return a*c} -function m(a,c){if(0===c)throw z();var b=Object.create(n.prototype);b.s=0>a?-1:1;a=0>a?-a:a;var d=w(a,c);b.n=a/d;b.d=c/d;return b}function A(a){for(var c={},b=a,d=2,f=4;f<=b;){for(;0===b%d;)b/=d,c[d]=(c[d]||0)+1;f+=1+2*d++}b!==a?1e.s?m(Math.pow(this.s*this.d,e.n),Math.pow(this.n,e.n)):m(Math.pow(this.s*this.n,e.n),Math.pow(this.d,e.n));if(0>this.s)return null;var b=A(this.n),d=A(this.d),f=1,l=1,k;for(k in b)if("1"!== -k){if("0"===k){f=0;break}b[k]*=e.n;if(0===b[k]%e.d)b[k]/=e.d;else return null;f*=Math.pow(k,b[k])}for(k in d)if("1"!==k){d[k]*=e.n;if(0===d[k]%e.d)d[k]/=e.d;else return null;l*=Math.pow(k,d[k])}return 0>e.s?m(l,f):m(f,l)},equals:function(a,c){p(a,c);return this.s*this.n*e.d===e.s*e.n*this.d},compare:function(a,c){p(a,c);var b=this.s*this.n*e.d-e.s*e.n*this.d;return(0b)},simplify:function(a){if(isNaN(this.n)||isNaN(this.d))return this;a=a||.001;for(var c=this.abs(),b=c.toContinued(),d=1;dthis.s&&(b+="-");1===f?b+=d:(a&&0<(c=Math.floor(d/f))&&(b=b+c+" ",d%=f),b=b+d+"/",b+=f);return b},toLatex:function(a){var c,b="",d=this.n,f=this.d;0>this.s&&(b+="-");1===f?b+=d:(a&&0<(c=Math.floor(d/f))&& +function m(a,c){if(0===c)throw z();var b=Object.create(p.prototype);b.s=0>a?-1:1;a=0>a?-a:a;var d=w(a,c);b.n=a/d;b.d=c/d;return b}function A(a){for(var c={},b=a,d=2,f=4;f<=b;){for(;0===b%d;)b/=d,c[d]=(c[d]||0)+1;f+=1+2*d++}b!==a?1e.s?m(Math.pow(this.s*this.d,e.n),Math.pow(this.n,e.n)):m(Math.pow(this.s*this.n,e.n),Math.pow(this.d,e.n));if(0>this.s)return null;var b=A(this.n),d=A(this.d),f=1,l=1,k;for(k in b)if("1"!== +k){if("0"===k){f=0;break}b[k]*=e.n;if(0===b[k]%e.d)b[k]/=e.d;else return null;f*=Math.pow(k,b[k])}for(k in d)if("1"!==k){d[k]*=e.n;if(0===d[k]%e.d)d[k]/=e.d;else return null;l*=Math.pow(k,d[k])}return 0>e.s?m(l,f):m(f,l)},equals:function(a,c){n(a,c);return this.s*this.n*e.d===e.s*e.n*this.d},compare:function(a,c){n(a,c);var b=this.s*this.n*e.d-e.s*e.n*this.d;return(0b)},simplify:function(a){if(isNaN(this.n)||isNaN(this.d))return this;a=a||.001;for(var c=this.abs(),b=c.toContinued(),d=1;dthis.s&&(b+="-");1===f?b+=d:(a&&0<(c=Math.floor(d/f))&&(b=b+c+" ",d%=f),b=b+d+"/",b+=f);return b},toLatex:function(a){var c,b="",d=this.n,f=this.d;0>this.s&&(b+="-");1===f?b+=d:(a&&0<(c=Math.floor(d/f))&& (b+=c,d%=f),b=b+"\\frac{"+d+"}{"+f,b+="}");return b},toContinued:function(){var a=this.n,c=this.d,b=[];if(isNaN(a)||isNaN(c))return b;do{b.push(Math.floor(a/c));var d=a%c;a=c;c=d}while(1!==a);return b},toString:function(a){var c=this.n,b=this.d;if(isNaN(c)||isNaN(b))return"NaN";var d;a:{for(d=b;0===d%2;d/=2);for(;0===d%5;d/=5);if(1===d)d=0;else{for(var f=10%d,l=1;1!==f;l++)if(f=10*f%d,2E3>=1)k&1&&(t=t*l%b);l=t;for(k=0;300>k;k++){if(f=== -l){l=k;break a}f=10*f%b;l=10*l%b}l=0}f=0>this.s?"-":"";f+=c/b|0;(c=c%b*10)&&(f+=".");if(d){for(a=l;a--;)f+=c/b|0,c%=b,c*=10;f+="(";for(a=d;a--;)f+=c/b|0,c%=b,c*=10;f+=")"}else for(a=a||15;c&&a--;)f+=c/b|0,c%=b,c*=10;return f}};"function"===typeof define&&define.amd?define([],function(){return n}):"object"===typeof exports?(Object.defineProperty(n,"__esModule",{value:!0}),n["default"]=n,n.Fraction=n,module.exports=n):B.Fraction=n})(this); \ No newline at end of file +l){l=k;break a}f=10*f%b;l=10*l%b}l=0}f=0>this.s?"-":"";f+=c/b|0;(c=c%b*10)&&(f+=".");if(d){for(a=l;a--;)f+=c/b|0,c%=b,c*=10;f+="(";for(a=d;a--;)f+=c/b|0,c%=b,c*=10;f+=")"}else for(a=a||15;c&&a--;)f+=c/b|0,c%=b,c*=10;return f}};"object"===typeof exports?(Object.defineProperty(exports,"__esModule",{value:!0}),exports["default"]=p,module.exports=p):B.Fraction=p})(this); \ No newline at end of file diff --git a/package.json b/package.json index 0052f64..bc4efd9 100644 --- a/package.json +++ b/package.json @@ -1,43 +1,52 @@ { - "name": "fraction.js", - "title": "fraction.js", - "version": "4.2.1", - "homepage": "https://www.xarg.org/2014/03/rational-numbers-in-javascript/", - "bugs": "https://github.com/infusion/Fraction.js/issues", - "description": "A rational number library", - "keywords": [ - "math", - "fraction", - "rational", - "rationals", - "number", - "parser", - "rational numbers" - ], - "author": "Robert Eisele (http://www.xarg.org/)", - "main": "fraction", - "types": "./fraction.d.ts", - "private": false, - "readmeFilename": "README.md", - "directories": { - "example": "examples" - }, - "license": "MIT", - "repository": { - "type": "git", - "url": "git://github.com/infusion/Fraction.js.git" - }, - "funding": { - "type": "patreon", - "url": "https://www.patreon.com/infusion" - }, - "engines": { - "node": "*" - }, - "scripts": { - "test": "mocha tests/*.js" - }, - "devDependencies": { - "mocha": "*" - } -} + "name": "fraction.js", + "title": "fraction.js", + "version": "4.3.0", + "homepage": "https://www.xarg.org/2014/03/rational-numbers-in-javascript/", + "bugs": "https://github.com/rawify/Fraction.js/issues", + "description": "A rational number library", + "keywords": [ + "math", + "fraction", + "rational", + "rationals", + "number", + "parser", + "rational numbers" + ], + "author": { + "name": "Robert Eisele", + "email": "robert@raw.org", + "url": "https://raw.org/" + }, + "type": "module", + "main": "fraction", + "exports": { + "import": "./fraction.cjs", + "require": "./fraction.cjs" + }, + "types": "./fraction.d.ts", + "private": false, + "readmeFilename": "README.md", + "directories": { + "example": "examples" + }, + "license": "MIT", + "repository": { + "type": "git", + "url": "git://github.com/rawify/Fraction.js.git" + }, + "funding": { + "type": "patreon", + "url": "https://github.com/sponsors/rawify" + }, + "engines": { + "node": "*" + }, + "scripts": { + "test": "mocha tests/*.js" + }, + "devDependencies": { + "mocha": "*" + } +} \ No newline at end of file diff --git a/tests/fraction.test.js b/tests/fraction.test.js index c181b93..dd5053e 100644 --- a/tests/fraction.test.js +++ b/tests/fraction.test.js @@ -1,16 +1,19 @@ -var assert = require('assert'); +import assert from 'assert'; +import Fraction from 'fraction.js'; -var Fraction = require('../bigfraction.js'); +var DivisionByZero = function() { return new Error("Division by Zero"); }; +var InvalidParameter = function() { return new Error("Invalid argument"); }; +var NonIntegerParameter = function() { return new Error("Parameters must be integer"); }; var tests = [{ set: "", - expectError: Fraction.InvalidParameter + expectError: InvalidParameter() }, { set: "foo", - expectError: Fraction.InvalidParameter + expectError: InvalidParameter() }, { set: " 123", - expectError: Fraction.InvalidParameter + expectError: InvalidParameter() }, { set: 0, expect: 0 @@ -40,7 +43,7 @@ var tests = [{ expect: "2.555" }, { set: " - ", - expectError: Fraction.InvalidParameter + expectError: InvalidParameter() }, { set: ".5", expect: "0.5" @@ -79,10 +82,10 @@ var tests = [{ expect: "-123.(4)" }, { set: "0/0", - expectError: Fraction.DivisionByZero + expectError: DivisionByZero() }, { set: "9/0", - expectError: Fraction.DivisionByZero + expectError: DivisionByZero() }, { label: "0/1+0/1", set: "0/1", @@ -107,7 +110,7 @@ var tests = [{ expect: "-19.269(736842105263157894)" }, { set: "123.(22)123", - expectError: Fraction.InvalidParameter + expectError: InvalidParameter() }, { set: "+33.3(3)", expect: "33.(3)" @@ -116,13 +119,13 @@ var tests = [{ expect: "3.(09009)" }, { set: "123.(((", - expectError: Fraction.InvalidParameter + expectError: InvalidParameter() }, { set: "123.((", - expectError: Fraction.InvalidParameter + expectError: InvalidParameter() }, { set: "123.()", - expectError: Fraction.InvalidParameter + expectError: InvalidParameter() }, { set: null, expect: "0" // I would say it's just fine @@ -368,7 +371,7 @@ var tests = [{ set: 10, fn: "div", param: 0, - expectError: Fraction.DivisionByZero + expectError: DivisionByZero() }, { label: "-3 / 4", set: [-3, 4], @@ -410,7 +413,7 @@ var tests = [{ set: 0, fn: "inverse", param: null, - expectError: Fraction.DivisionByZero + expectError: DivisionByZero() }, { label: "abs(-100.25)", set: -100.25, @@ -477,13 +480,13 @@ var tests = [{ fn: "mod", param: 0.1, expect: "0" -}, { +}, /*{ label: "bignum", set: [5385020324, 1673196525], fn: "add", param: 0, expect: "3.21(840276592733181776121606516006839065124164060763872313206005492988936251824931324190982287630557922656455433410609073551596098372245902196097377144624418820138297860736950789447760776337973807350574075570710380240599651018280712721418065340531352107607323652551812465663589637206543923464101146157950573080469432602963360804254598843372567965379918536467197121390148715495330113717514444395585868193217769203770011415724163065662594535928766646225254382476081224230369471990147720394052336440275597631903998844367669243157195775313960803259497565595290726533154854597848271290188102679689703515252041298615534717298077104242133182771222884293284077911887845930112722413166618308629346454087334421161315763550250022184333666363549254920906556389124702491239037207539024741878423396797336762338781453063321417070239253574830368476888869943116813489676593728283053898883754853602746993512910863832926021645903191198654921901657666901979730085800889408373591978384009612977172541043856160291750546158945674358246709841810124486123947693472528578195558946669459524487119048971249805817042322628538808374587079661786890216019304725725509141850506771761314768448972244907094819599867385572056456428511886850828834945135927771544947477105237234460548500123140047759781236696030073335228807028510891749551057667897081007863078128255137273847732859712937785356684266362554153643129279150277938809369688357439064129062782986595074359241811119587401724970711375341877428295519559485099934689381452068220139292962014728066686607540019843156200674036183526020650801913421377683054893985032630879985)" -}, { +},*/ { label: "ceil(0.4)", set: 0.4, fn: "ceil", @@ -699,7 +702,7 @@ var tests = [{ fn: "round", param: null, expect: "0" -}, { +}, /*{ label: "round(big fraction)", set: [ '409652136432929109317120'.repeat(100), @@ -714,7 +717,7 @@ var tests = [{ fn: "round", param: null, expect: '409652136432929109317'.repeat(99) + '40965213643292910932' -}, { +}, */{ label: "17402216385200408/5539306332998545", set: [17402216385200408, 5539306332998545], fn: "add", @@ -1349,45 +1352,45 @@ var tests = [{ set: NaN, fn: "toString", param: null, - expectError: "foo" + expect: "NaN" }, { label: "12 / 4.3", set: 12, set2: 4.3, fn: "toString", param: null, - expectError: "foo" + expectError: NonIntegerParameter() }, { label: "12.5 / 4", set: 12.5, set2: 4, fn: "toString", param: null, - expectError: "foo" + expectError: NonIntegerParameter() } ]; -describe('Fraction', function () { +describe('Fraction', function() { for (var i = 0; i < tests.length; i++) { - (function (i) { + (function(i) { var action; if (tests[i].fn) { - action = function () { + action = function() { var x = Fraction(tests[i].set, tests[i].set2)[tests[i].fn](tests[i].param); if (x === null) return "null"; return x.toString(); }; } else { - action = function () { + action = function() { var x = new Fraction(tests[i].set, tests[i].set2); if (x === null) return "null"; return x.toString(); }; } - it(String(tests[i].label || tests[i].set), function () { + it(String(tests[i].label || tests[i].set), function() { if (tests[i].expectError) { assert.throws(action, tests[i].expectError); } else { @@ -1399,9 +1402,9 @@ describe('Fraction', function () { } }); -describe('JSON', function () { +describe('JSON', function() { - it("Should be possible to stringify the object", function () { + it("Should be possible to stringify the object", function() { // TODO: BigInt is not yet serializable!!!! //assert.equal('{"s":1,"n":14623,"d":330}', JSON.stringify(new Fraction("44.3(12)"))); @@ -1410,9 +1413,9 @@ describe('JSON', function () { }); }); -describe('Arguments', function () { +describe('Arguments', function() { - it("Should be possible to use different kind of params", function () { + it("Should be possible to use different kind of params", function() { // String var fraction = new Fraction("0.1"); @@ -1435,27 +1438,27 @@ describe('Arguments', function () { }); }); -describe('fractions', function () { +describe('fractions', function() { - it("Should pass 0.08 = 2/25", function () { + it("Should pass 0.08 = 2/25", function() { var fraction = new Fraction("0.08"); assert.equal("2/25", fraction.n + "/" + fraction.d); }); - it("Should pass 0.200 = 1/5", function () { + it("Should pass 0.200 = 1/5", function() { var fraction = new Fraction("0.200"); assert.equal("1/5", fraction.n + "/" + fraction.d); }); - it("Should pass 0.125 = 1/8", function () { + it("Should pass 0.125 = 1/8", function() { var fraction = new Fraction("0.125"); assert.equal("1/8", fraction.n + "/" + fraction.d); }); - it("Should pass 8.36 = 209/25", function () { + it("Should pass 8.36 = 209/25", function() { var fraction = new Fraction(8.36); assert.equal("209/25", fraction.n + "/" + fraction.d); @@ -1463,9 +1466,9 @@ describe('fractions', function () { }); -describe('constructors', function () { +describe('constructors', function() { - it("Should pass 0.08 = 2/25", function () { + it("Should pass 0.08 = 2/25", function() { var tmp = new Fraction({ d: 4, n: 2, s: -1 }); assert.equal("-1/2", tmp.s * tmp.n + "/" + tmp.d); @@ -1488,66 +1491,66 @@ describe('constructors', function () { }); }); -describe('Latex Output', function () { +describe('Latex Output', function() { - it("Should pass 123.'3' = \\frac{370}{3}", function () { + it("Should pass 123.'3' = \\frac{370}{3}", function() { var tmp = new Fraction("123.'3'"); assert.equal("\\frac{370}{3}", tmp.toLatex()); }); - it("Should pass 1.'3' = \\frac{4}{3}", function () { + it("Should pass 1.'3' = \\frac{4}{3}", function() { var tmp = new Fraction("1.'3'"); assert.equal("\\frac{4}{3}", tmp.toLatex()); }); - it("Should pass -1.0000000000 = -1", function () { + it("Should pass -1.0000000000 = -1", function() { var tmp = new Fraction("-1.0000000000"); assert.equal('-1', tmp.toLatex()); }); - it("Should pass -0.0000000000 = 0", function () { + it("Should pass -0.0000000000 = 0", function() { var tmp = new Fraction("-0.0000000000"); assert.equal('0', tmp.toLatex()); }); }); -describe('Fraction Output', function () { +describe('Fraction Output', function() { - it("Should pass 123.'3' = 123 1/3", function () { + it("Should pass 123.'3' = 123 1/3", function() { var tmp = new Fraction("123.'3'"); assert.equal('370/3', tmp.toFraction()); }); - it("Should pass 1.'3' = 1 1/3", function () { + it("Should pass 1.'3' = 1 1/3", function() { var tmp = new Fraction("1.'3'"); assert.equal('4/3', tmp.toFraction()); }); - it("Should pass -1.0000000000 = -1", function () { + it("Should pass -1.0000000000 = -1", function() { var tmp = new Fraction("-1.0000000000"); assert.equal('-1', tmp.toFraction()); }); - it("Should pass -0.0000000000 = 0", function () { + it("Should pass -0.0000000000 = 0", function() { var tmp = new Fraction("-0.0000000000"); assert.equal('0', tmp.toFraction()); }); - it("Should pass 1/-99/293 = -1/29007", function () { + it("Should pass 1/-99/293 = -1/29007", function() { var tmp = new Fraction(-99).inverse().div(293); assert.equal('-1/29007', tmp.toFraction()); }); - it('Should work with large calculations', function () { + it('Should work with large calculations', function() { var x = Fraction(1123875); var y = Fraction(1238750184); var z = Fraction(1657134); @@ -1556,45 +1559,45 @@ describe('Fraction Output', function () { }); }); -describe('Fraction toContinued', function () { +describe('Fraction toContinued', function() { - it("Should pass 415/93", function () { + it("Should pass 415/93", function() { var tmp = new Fraction(415, 93); assert.equal('4,2,6,7', tmp.toContinued().toString()); }); - it("Should pass 0/2", function () { + it("Should pass 0/2", function() { var tmp = new Fraction(0, 2); assert.equal('0', tmp.toContinued().toString()); }); - it("Should pass 1/7", function () { + it("Should pass 1/7", function() { var tmp = new Fraction(1, 7); assert.equal('0,7', tmp.toContinued().toString()); }); - it("Should pass 23/88", function () { + it("Should pass 23/88", function() { var tmp = new Fraction('23/88'); assert.equal('0,3,1,4,1,3', tmp.toContinued().toString()); }); - it("Should pass 1/99", function () { + it("Should pass 1/99", function() { var tmp = new Fraction('1/99'); assert.equal('0,99', tmp.toContinued().toString()); }); - it("Should pass 1768/99", function () { + it("Should pass 1768/99", function() { var tmp = new Fraction('1768/99'); assert.equal('17,1,6,14', tmp.toContinued().toString()); }); - it("Should pass 1768/99", function () { + it("Should pass 1768/99", function() { var tmp = new Fraction('7/8'); assert.equal('0,1,7', tmp.toContinued().toString()); @@ -1603,9 +1606,9 @@ describe('Fraction toContinued', function () { }); -describe('Fraction simplify', function () { +describe('Fraction simplify', function() { - it("Should pass 415/93", function () { + it("Should pass 415/93", function() { var tmp = new Fraction(415, 93); assert.equal('9/2', tmp.simplify(0.1).toFraction());