diff --git a/dist/classified.js b/dist/classified.js index a8d3ca7..ec1db4f 100644 --- a/dist/classified.js +++ b/dist/classified.js @@ -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() { diff --git a/dist/classified.min.js b/dist/classified.min.js index 236147b..693637f 100644 --- a/dist/classified.min.js +++ b/dist/classified.min.js @@ -9,4 +9,4 @@ // - classify 0.10.4 http://github.com/petebrowne/classify // //-------------------------------------------------------------------------- -(function(){var l='undefined',h=this,d=h.def,i=h.classify,n=h.module,o=h.include,p=h.extend,g=h.alias,m=Array.prototype.slice,j=h.$break={};n('Enumerable',function(){var k=function(a){return a};d('each',function(b,c){var e=0;try{this.__each__(function(a){b.call(c,a,e++)})}catch(error){if(error!=j){throw error;}}return this});d('all',function(c,e){c=c||k;var f=true;this.each(function(a,b){f=f&&!!c.call(e,a,b);if(!f){throw j;}});return f});g('every','all');d('any',function(c,e){c=c||k;var f=false;this.each(function(a,b){if(f=!!c.call(e,a,b)){throw j;}});return f});g('some','any');d('collect',function(e,f){e=e||k;return this.inject([],function(a,b,c){a.push(e.call(f,b,c));return a})});g('map','collect');d('detect',function(c,e){var f=null;this.each(function(a,b){if(c.call(e,a,b)){f=a;throw j;}});return f});g('find','detect');d('include',function(b){if(typeof this.indexOf==='function'){return this.indexOf(b)!=-1}var c=false;this.each(function(a){if(typeof a.value!==l){a=a.value}if(a==b){c=true;throw j;}});return c});g('contains','include');d('inject',function(c,e,f){this.each(function(a,b){c=e.call(f,c,a,b)});return c});g('reduce','inject');d('invoke',function(b){var c=m.call(arguments,1);return this.map(function(a){return a[b].apply(a,c)})});d('pluck',function(b){return this.map(function(a){return a[b]})});d('reject',function(e,f){return this.inject([],function(a,b,c){if(!e.call(f,b,c)){a.push(b)}return a})});g('not','reject');d('select',function(e,f){return this.inject([],function(a,b,c){if(e.call(f,b,c)){a.push(b)}return a})});g('findAll','select');g('filter','select')});n('Events',function(){d('bind',function(a,b){var c=this.__events__||(this.__events__={}),e=c[a]||(c[a]=[]);e.push(b);return this});d('unbind',function(a,b){var c,e;if(!a){delete this.__events__}else if(c=this.__events__){if(!b){delete c[a]}else if(e=c[a]){for(var f=0,k=e.length;f0&&a=0&&this.indexOf(a,b)===b});d('include',function(a){return this.indexOf(a)>-1});g('contains','include');d('isBlank',function(){return/^\s*$/.test(this)});d('isEmpty',function(){return this==''});d('startsWith',function(a){return this.lastIndexOf(a,0)===0});d('strip',function(){return this.replace(/^\s+/,'').replace(/\s+$/,'')});d('camelize',function(){return this.replace(/[-_]+(.)?/g,function(a,b){return b?b.toUpperCase():''})});d('capitalize',function(){return this.charAt(0).toUpperCase()+this.substring(1)});d('dasherize',function(){return this.replace(/([A-Z]+)([A-Z][a-z])/g,'$1-$2').replace(/([a-z\d])([A-Z])/g,'$1-$2').replace(/_/g,'-').toLowerCase()});d('humanize',function(){return this.replace(/[_|-]+/g,' ').capitalize()});d('titleize',function(){return this.underscore().humanize().replace(/\b('?[a-z])/g,function(a,b){return b.capitalize()})});g('toTitleCase','titleize');d('underscore',function(){return this.replace(/([A-Z]+)([A-Z][a-z])/g,'$1_$2').replace(/([a-z\d])([A-Z])/g,'$1_$2').replace(/-/g,'_').toLowerCase()})})})(); \ No newline at end of file +(function(){var l='undefined',h=this,d=h.def,i=h.classify,n=h.module,o=h.include,p=h.extend,g=h.alias,m=Array.prototype.slice,j=h.$break={};n('Enumerable',function(){var k=function(a){return a};d('each',function(b,c){var e=0;try{this.__each__(function(a){b.call(c,a,e++)})}catch(error){if(error!=j){throw error;}}return this});d('all',function(c,e){c=c||k;var f=true;this.each(function(a,b){f=f&&!!c.call(e,a,b);if(!f){throw j;}});return f});g('every','all');d('any',function(c,e){c=c||k;var f=false;this.each(function(a,b){if(f=!!c.call(e,a,b)){throw j;}});return f});g('some','any');d('collect',function(e,f){e=e||k;return this.inject([],function(a,b,c){a.push(e.call(f,b,c));return a})});g('map','collect');d('detect',function(c,e){var f=null;this.each(function(a,b){if(c.call(e,a,b)){f=a;throw j;}});return f});g('find','detect');d('include',function(b){if(typeof this.indexOf==='function'){return this.indexOf(b)!=-1}var c=false;this.each(function(a){if(typeof a.value!==l){a=a.value}if(a==b){c=true;throw j;}});return c});g('contains','include');d('inject',function(c,e,f){this.each(function(a,b){c=e.call(f,c,a,b)});return c});g('reduce','inject');d('invoke',function(b){var c=m.call(arguments,1);return this.map(function(a){return a[b].apply(a,c)})});d('pluck',function(b){return this.map(function(a){return a[b]})});d('reject',function(e,f){return this.inject([],function(a,b,c){if(!e.call(f,b,c)){a.push(b)}return a})});g('not','reject');d('select',function(e,f){return this.inject([],function(a,b,c){if(e.call(f,b,c)){a.push(b)}return a})});g('findAll','select');g('filter','select')});n('Events',function(){d('bind',function(a,b){var c=this.__events__||(this.__events__={}),e=c[a]||(c[a]=[]);e.push(b);return this});d('unbind',function(a,b){var c,e;if(!a){delete this.__events__}else if(c=this.__events__){if(!b){delete c[a]}else if(e=c[a]){for(var f=0,k=e.length;f0&&a=0&&this.indexOf(a,b)===b});d('include',function(a){return this.indexOf(a)>-1});g('contains','include');d('isBlank',function(){return/^\s*$/.test(this)});d('isEmpty',function(){return this==''});d('startsWith',function(a){return this.lastIndexOf(a,0)===0});d('strip',function(){return this.replace(/^\s+/,'').replace(/\s+$/,'')});d('camelize',function(){return this.replace(/[-_]+(.)?/g,function(a,b){return b?b.toUpperCase():''})});d('capitalize',function(){return this.charAt(0).toUpperCase()+this.substring(1)});d('dasherize',function(){return this.replace(/([A-Z]+)([A-Z][a-z])/g,'$1-$2').replace(/([a-z\d])([A-Z])/g,'$1-$2').replace(/_/g,'-').toLowerCase()});d('humanize',function(){return this.replace(/[_|-]+/g,' ').capitalize()});d('titleize',function(){return this.underscore().humanize().replace(/\b('?[a-z])/g,function(a,b){return b.capitalize()})});g('toTitleCase','titleize');d('underscore',function(){return this.replace(/([A-Z]+)([A-Z][a-z])/g,'$1_$2').replace(/([a-z\d])([A-Z])/g,'$1_$2').replace(/-/g,'_').toLowerCase()})})})(); \ No newline at end of file diff --git a/spec/classified/numberSpec.js b/spec/classified/numberSpec.js index bbdb5a3..36152b5 100755 --- a/spec/classified/numberSpec.js +++ b/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)); }); }); }); diff --git a/src/extensions/number.js b/src/extensions/number.js index ec29225..d8671b7 100755 --- a/src/extensions/number.js +++ b/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' ]); });