1 |
|
2 | (function() {
|
3 | var Ratio;
|
4 |
|
5 | module.exports.Ratio = Ratio = (function() {
|
6 | function Ratio(numerator, denominator) {
|
7 | this.numerator = numerator;
|
8 | this.denominator = denominator;
|
9 | if (this.numerator == null) {
|
10 | throw new Error("Cannot create a ratio with an undefined numerator");
|
11 | }
|
12 | if (this.denominator == null) {
|
13 | throw new Error("Cannot create a ratio with an undefined denominator");
|
14 | }
|
15 | }
|
16 |
|
17 | Object.defineProperties(Ratio.prototype, {
|
18 | isRatio: {
|
19 | get: function() {
|
20 | return true;
|
21 | }
|
22 | }
|
23 | });
|
24 |
|
25 | Ratio.prototype.clone = function() {
|
26 | return new Ratio(this.numerator.clone(), this.denominator.clone());
|
27 | };
|
28 |
|
29 | Ratio.prototype.toString = function() {
|
30 | return (this.numerator.toString()) + " : " + (this.denominator.toString());
|
31 | };
|
32 |
|
33 | Ratio.prototype.equals = function(other) {
|
34 | var divided_other, divided_this;
|
35 | if (other != null ? other.isRatio : void 0) {
|
36 | divided_this = this.numerator.dividedBy(this.denominator);
|
37 | divided_other = other.numerator.dividedBy(other.denominator);
|
38 | return divided_this.equals(divided_other);
|
39 | } else {
|
40 | return false;
|
41 | }
|
42 | };
|
43 |
|
44 | Ratio.prototype.equivalent = function(other) {
|
45 | var equal;
|
46 | equal = this.equals(other);
|
47 | if (equal == null) {
|
48 | return false;
|
49 | }
|
50 | return equal;
|
51 | };
|
52 |
|
53 | return Ratio;
|
54 |
|
55 | })();
|
56 |
|
57 | }).call(this);
|
58 |
|
59 |
|