Navigation Menu

Skip to content

Commit

Permalink
Changing Number to include all of the Math functions
Browse files Browse the repository at this point in the history
  • Loading branch information
petebrowne committed Nov 30, 2010
1 parent 735bf54 commit 5a52a5a
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 58 deletions.
33 changes: 8 additions & 25 deletions dist/classified.js
Expand Up @@ -418,31 +418,14 @@ classify(Function, function() {
});

classify(Number, function() {
// Returns the absolute value of the number. Convenience method that simply
// calls `Math.abs` on this instance and returns the result.
def('abs', function() {
return Math.abs(this);
});

// Rounds the number to the nearest integer. Convenience method that simply
// calls `Math.round` on this instance and returns the result.
def('round', function() {
return Math.round(this);
});

// Returns the smallest integer greater than or equal to the number.
// Convenience method that simply calls `Math.ceil` on this instance and
// returns the result.
def('ceil', function() {
return Math.ceil(this);
});

// Returns the largest integer less than or equal to the number.
// Convenience method that simply calls `Math.floor` on this instance and
// returns the result.
def('floor', function() {
return Math.floor(this);
});
// Copy over each Math method into the Number prototype.
(function(math) {
math.each(function(name) {
def(name, function() {
return Math[name].apply(null, [ this ].concat(slice.call(arguments)));
});
});
})([ 'abs', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'exp', 'floor', 'log', 'max', 'min', 'pow', 'round', 'sin', 'sqrt', 'tan' ]);
});

extend(Object, function() {
Expand Down
2 changes: 1 addition & 1 deletion dist/classified.min.js

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

86 changes: 79 additions & 7 deletions spec/classified/numberSpec.js
@@ -1,25 +1,97 @@
describe('Number', function() {
describe('#abs', function() {
it('returns the absolute value of a number', function() {
expect((-1).abs()).toEqual(1);
it('returns the abs of a number', function() {
expect((-1).abs()).toEqual(Math.abs(-1));
});
});

describe('#acos', function() {
it('returns the acos of a number', function() {
expect((-1).acos()).toEqual(Math.acos(-1));
});
});

describe('#asin', function() {
it('returns the asin of a number', function() {
expect((-1).asin()).toEqual(Math.asin(-1));
});
});

describe('#atan', function() {
it('returns the atan of a number', function() {
expect((1).atan()).toEqual(Math.atan(1));
});
});

describe('#atan2', function() {
it('returns the atan2 of a number', function() {
expect((4).atan2(8)).toEqual(Math.atan2(4, 8));
});
});

describe('#ceil', function() {
it('returns the smallest integer greater than or equal to the number', function() {
expect((0.1).ceil()).toEqual(1);
it('returns the ceil of a number', function() {
expect((0.1).ceil()).toEqual(Math.ceil(0.1));
});
});

describe('#exp', function() {
it('returns the exp of a number', function() {
expect((5).exp()).toEqual(Math.exp(5));
});
});

describe('#floor', function() {
it('returns the largest integer less than or equal to the number.r', function() {
expect((1.9).floor()).toEqual(1);
it('returns the floor of a number', function() {
expect((1.9).floor()).toEqual(Math.floor(1.9));
});
});

describe('#log', function() {
it('returns the log of a number', function() {
expect((5).log()).toEqual(Math.log(5));
});
});

describe('#max', function() {
it('returns the max of a number', function() {
expect((25).max(10, 15, 0)).toEqual(Math.max(25, 10, 15, 0));
});
});

describe('#min', function() {
it('returns the min of a number', function() {
expect((5).min(10, 15)).toEqual(Math.min(5, 10, 15));
});
});

describe('#pow', function() {
it('returns the pow of a number', function() {
expect((5).pow(2)).toEqual(Math.pow(5, 2));
});
});

describe('#round', function() {
it('rounds the number to the nearest integer', function() {
expect((0.8).round()).toEqual(1);
expect((0.8).round()).toEqual(Math.round(0.8));
});
});

describe('#sin', function() {
it('returns the sin of a number', function() {
expect((1).sin()).toEqual(Math.sin(1));
});
});

describe('#sqrt', function() {
it('returns the sqrt of a number', function() {
expect((9).sqrt()).toEqual(Math.sqrt(9));
});
});

describe('#tan', function() {
it('returns the tan of a number', function() {
expect((1).tan()).toEqual(Math.tan(1));
});
});
});
33 changes: 8 additions & 25 deletions src/extensions/number.js
@@ -1,27 +1,10 @@
classify(Number, function() {
// Returns the absolute value of the number. Convenience method that simply
// calls `Math.abs` on this instance and returns the result.
def('abs', function() {
return Math.abs(this);
});

// Rounds the number to the nearest integer. Convenience method that simply
// calls `Math.round` on this instance and returns the result.
def('round', function() {
return Math.round(this);
});

// Returns the smallest integer greater than or equal to the number.
// Convenience method that simply calls `Math.ceil` on this instance and
// returns the result.
def('ceil', function() {
return Math.ceil(this);
});

// Returns the largest integer less than or equal to the number.
// Convenience method that simply calls `Math.floor` on this instance and
// returns the result.
def('floor', function() {
return Math.floor(this);
});
// Copy over each Math method into the Number prototype.
(function(math) {
math.each(function(name) {
def(name, function() {
return Math[name].apply(null, [ this ].concat(slice.call(arguments)));
});
});
})([ 'abs', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'exp', 'floor', 'log', 'max', 'min', 'pow', 'round', 'sin', 'sqrt', 'tan' ]);
});

0 comments on commit 5a52a5a

Please sign in to comment.