Skip to content

Changed from throwing strings to throwing custom subtypes of Error. #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions fraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

"use strict";

// Maximum search depth for cyclic rational numbers. 2000 should be more than enough.
// Maximum search depth for cyclic rational numbers. 2000 should be more than enough.
// Example: 1/7 = 0.(142857) has 6 repeating decimal places.
// If MAX_CYCLE_LEN gets reduced, long cycles will not be detected and toString() only gets the first 10 digits
var MAX_CYCLE_LEN = 2000;
Expand All @@ -53,6 +53,24 @@
"d": 1
};

function createError(name) {
var errorConstructor = function() {
var temp = Error.apply(this, arguments);
temp.name = this.name = name;
this.stack = temp.stack;
this.message = temp.message;
}

var IntermediateInheritor = function() {};
IntermediateInheritor.prototype = Error.prototype;
errorConstructor.prototype = new IntermediateInheritor();

return errorConstructor;
}

var DivisionByZero = Fraction['DivisionByZero'] = createError('DivisionByZero');
var InvalidParameter = Fraction['InvalidParameter'] = createError('InvalidParameter');

function assign(n, s) {

if (isNaN(n = parseInt(n, 10))) {
Expand All @@ -62,7 +80,7 @@
}

function throwInvalidParam() {
throw "Invalid Param";
throw new InvalidParameter();
}

var parse = function(p1, p2) {
Expand Down Expand Up @@ -221,7 +239,7 @@
}

if (d === 0) {
throw "DIV/0";
throw new DivisionByZero();
}

P["s"] = s < 0 ? -1 : 1;
Expand Down Expand Up @@ -253,7 +271,7 @@

// If we would like to compute really large numbers quicker, we could make use of Fermat's little theorem:
// 10^(d-1) % d == 1
// However, we don't need such large numbers and MAX_CYCLE_LEN should be the capstone,
// However, we don't need such large numbers and MAX_CYCLE_LEN should be the capstone,
// as we want to translate the numbers to strings.

var rem = 10 % d;
Expand Down Expand Up @@ -437,7 +455,7 @@

parse(a, b);
if (0 === P["n"] && 0 === this["d"]) {
Fraction(0, 0); // Throw div/0
Fraction(0, 0); // Throw DivisionByZero
}

/*
Expand Down Expand Up @@ -669,7 +687,7 @@

/**
* Returns an array of continued fraction elements
*
*
* Ex: new Fraction("7/8").toContinued() => [0,1,7]
*/
'toContinued': function() {
Expand Down
28 changes: 14 additions & 14 deletions fraction.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 27 additions & 30 deletions tests/fraction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ var Fraction = require('../fraction.min');

var tests = [{
set: "foo",
expect: "Invalid Param"
expectError: Fraction.InvalidParameter
}, {
set: " 123",
expect: "Invalid Param"
expectError: Fraction.InvalidParameter
}, {
set: 0,
expect: 0
Expand Down Expand Up @@ -37,7 +37,7 @@ var tests = [{
expect: "2.555"
}, {
set: " - ",
expect: "Invalid Param"
expectError: Fraction.InvalidParameter
}, {
set: ".5",
expect: "0.5"
Expand Down Expand Up @@ -76,10 +76,10 @@ var tests = [{
expect: "-123.(4)"
}, {
set: "0/0",
expect: "DIV/0"
expectError: Fraction.DivisionByZero
}, {
set: "9/0",
expect: "DIV/0"
expectError: Fraction.DivisionByZero
}, {
label: "0/1+0/1",
set: "0/1",
Expand All @@ -104,7 +104,7 @@ var tests = [{
expect: "-19.269(736842105263157894)"
}, {
set: "123.(22)123",
expect: "Invalid Param"
expectError: Fraction.InvalidParameter
}, {
set: "+33.3(3)",
expect: "33.(3)"
Expand All @@ -113,13 +113,13 @@ var tests = [{
expect: "3.(09009)"
}, {
set: "123.(((",
expect: "Invalid Param"
expectError: Fraction.InvalidParameter
}, {
set: "123.((",
expect: "Invalid Param"
expectError: Fraction.InvalidParameter
}, {
set: "123.()",
expect: "Invalid Param"
expectError: Fraction.InvalidParameter
}, {
set: null,
expect: "0" // I would say it's just fine
Expand Down Expand Up @@ -294,7 +294,7 @@ var tests = [{
set: 10,
fn: "div",
param: 0,
expect: "DIV/0"
expectError: Fraction.DivisionByZero
}, {
label: "-3 / 4",
set: [-3, 4],
Expand Down Expand Up @@ -336,7 +336,7 @@ var tests = [{
set: 0,
fn: "inverse",
param: null,
expect: "DIV/0"
expectError: Fraction.DivisionByZero
}, {
label: "abs(-100.25)",
set: -100.25,
Expand Down Expand Up @@ -1095,32 +1095,29 @@ var tests = [{
];

describe('Fraction', function() {

for (var i = 0; i < tests.length; i++) {

(function(i) {
var action;

if (tests[i].fn) {

it(tests[i].label || tests[i].set, function() {
try {
assert.equal(new Fraction(tests[i].set)[tests[i].fn](tests[i].param).toString(), tests[i].expect);
} catch (e) {
assert.equal(e.toString(), tests[i].expect);
}
});

action = function() {
return new Fraction(tests[i].set)[tests[i].fn](tests[i].param).toString();
};
} else {

it(tests[i].label || tests[i].set, function() {
try {
assert.equal(new Fraction(tests[i].set).toString(), tests[i].expect);
} catch (e) {
assert.equal(e.toString(), tests[i].expect);
}
});
action = function() {
return new Fraction(tests[i].set).toString();
};
}

it(tests[i].label || tests[i].set, function() {
if(tests[i].expectError) {
assert.throws(action, tests[i].expectError);
} else {
assert.equal(action(), tests[i].expect);
}
});

})(i);
}
});
Expand Down Expand Up @@ -1377,4 +1374,4 @@ describe('Fraction NaN', function() {

})(i);
}
});
});