From 86118c3cb52700cd7c1c134379e305359319ae9f Mon Sep 17 00:00:00 2001 From: Thomas Fuchs Date: Wed, 6 Jun 2007 16:10:16 +0000 Subject: [PATCH] Prototype: Add Number.prototype.round/ceil/floor/abs as an aliases to the respective methods in Math. Refactor to seperate number extensions from base.js. [Thomas Fuchs] --- CHANGELOG | 4 +-- src/base.js | 28 ------------------ src/number.js | 27 +++++++++++++++++ src/prototype.js | 2 +- test/unit/base.html | 29 ------------------ test/unit/number.html | 69 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 99 insertions(+), 60 deletions(-) create mode 100644 src/number.js create mode 100644 test/unit/number.html diff --git a/CHANGELOG b/CHANGELOG index 72d3b8b..14c5c6d 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,8 +1,8 @@ *SVN* -* Make Element#absolutize and Element#relativize properly use Element#getStyle. Closes #8580. [Christophe Porteneuve] +* Add Number.prototype.round/ceil/floor/abs as an aliases to the respective methods in Math. Refactor to seperate number extensions from base.js. [Thomas Fuchs] -* Add Number.prototype.round as an alias to Math.round(). [Thomas Fuchs] +* Make Element#absolutize and Element#relativize properly use Element#getStyle. Closes #8580. [Christophe Porteneuve] * Test library fixes: make rake dist work on Windows, only teardown if a browser is supported. Closes #8463, #8498. [Mislav Marohnić, grant] diff --git a/src/base.js b/src/base.js index ddef1c5..e38522f 100644 --- a/src/base.js +++ b/src/base.js @@ -113,34 +113,6 @@ Object.extend(Function.prototype, { Function.prototype.defer = Function.prototype.delay.curry(0.01); -Object.extend(Number.prototype, { - round: function() { - return Math.round(this); - }, - - toColorPart: function() { - return this.toPaddedString(2, 16); - }, - - succ: function() { - return this + 1; - }, - - times: function(iterator) { - $R(0, this, true).each(iterator); - return this; - }, - - toPaddedString: function(length, radix) { - var string = this.toString(radix || 10); - return '0'.times(length - string.length) + string; - }, - - toJSON: function() { - return isFinite(this) ? this.toString() : 'null'; - } -}); - Date.prototype.toJSON = function() { return '"' + this.getFullYear() + '-' + (this.getMonth() + 1).toPaddedString(2) + '-' + diff --git a/src/number.js b/src/number.js new file mode 100644 index 0000000..74becf2 --- /dev/null +++ b/src/number.js @@ -0,0 +1,27 @@ +Object.extend(Number.prototype, { + toColorPart: function() { + return this.toPaddedString(2, 16); + }, + + succ: function() { + return this + 1; + }, + + times: function(iterator) { + $R(0, this, true).each(iterator); + return this; + }, + + toPaddedString: function(length, radix) { + var string = this.toString(radix || 10); + return '0'.times(length - string.length) + string; + }, + + toJSON: function() { + return isFinite(this) ? this.toString() : 'null'; + } +}); + +$w('abs round ceil floor').each(function(method){ + Number.prototype[method] = function(){ return Math[method](this); } +}); \ No newline at end of file diff --git a/src/prototype.js b/src/prototype.js index b11be88..ab95f77 100644 --- a/src/prototype.js +++ b/src/prototype.js @@ -27,7 +27,7 @@ var Prototype = { <%= include 'base.js', 'string.js' %> -<%= include 'enumerable.js', 'array.js', 'hash.js', 'range.js' %> +<%= include 'enumerable.js', 'array.js', 'number.js', 'hash.js', 'range.js' %> <%= include 'ajax.js', 'dom.js', 'selector.js', 'form.js', 'event.js', 'deprecated.js' %> diff --git a/test/unit/base.html b/test/unit/base.html index c464b27..413de9b 100644 --- a/test/unit/base.html +++ b/test/unit/base.html @@ -218,35 +218,6 @@ } }, - testNumberRound: function() {with(this) { - assertEqual(1, (0.9).round()); - assertEqual(-2, (-1.9).round()); - - assertEqual(Math.round(Math.PI), Math.PI.round()); - }}, - - testNumberToColorPart: function() {with(this) { - assertEqual('00', (0).toColorPart()); - assertEqual('0a', (10).toColorPart()); - assertEqual('ff', (255).toColorPart()); - }}, - - testNumberToPaddedString: function() {with(this) { - assertEqual('00', (0).toPaddedString(2, 16)); - assertEqual('0a', (10).toPaddedString(2, 16)); - assertEqual('ff', (255).toPaddedString(2, 16)); - assertEqual('000', (0).toPaddedString(3)); - assertEqual('010', (10).toPaddedString(3)); - assertEqual('100', (100).toPaddedString(3)); - assertEqual('1000', (1000).toPaddedString(3)); - }}, - - testNumberToJSON: function() {with(this) { - assertEqual('null', Number.NaN.toJSON()); - assertEqual('0', (0).toJSON()); - assertEqual('-293', (-293).toJSON()); - }}, - testDateToJSON: function() {with(this) { assertEqual('\"1969-12-31T19:00:00\"', new Date(1969, 11, 31, 19).toJSON()); }}, diff --git a/test/unit/number.html b/test/unit/number.html new file mode 100644 index 0000000..b2cce08 --- /dev/null +++ b/test/unit/number.html @@ -0,0 +1,69 @@ + + + + Prototype Unit test file + + + + + + + +

Prototype Unit test file

+

+ Test of utility functions in number.js +

+ + +
+ + + + +