Skip to content

Commit

Permalink
Prototype: Add Number.prototype.round/ceil/floor/abs as an aliases to…
Browse files Browse the repository at this point in the history
… the respective methods in Math. Refactor to seperate number extensions from base.js. [Thomas Fuchs]
  • Loading branch information
madrobby committed Jun 6, 2007
1 parent 0c7bac1 commit 86118c3
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 60 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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]

Expand Down
28 changes: 0 additions & 28 deletions src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) + '-' +
Expand Down
27 changes: 27 additions & 0 deletions src/number.js
Original file line number Diff line number Diff line change
@@ -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); }
});
2 changes: 1 addition & 1 deletion src/prototype.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' %>

Expand Down
29 changes: 0 additions & 29 deletions test/unit/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -218,35 +218,6 @@ <h1>Prototype Unit test file</h1>
}
},

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());
}},
Expand Down
69 changes: 69 additions & 0 deletions test/unit/number.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Prototype Unit test file</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script src="../../dist/prototype.js" type="text/javascript"></script>
<script src="../lib/unittest.js" type="text/javascript"></script>
<link rel="stylesheet" href="../test.css" type="text/css" />
<style type="text/css" media="screen">
/* <![CDATA[ */
#testcss1 { font-size:11px; color: #f00; }
#testcss2 { font-size:12px; color: #0f0; display: none; }
/* ]]> */
</style>
</head>
<body>
<h1>Prototype Unit test file</h1>
<p>
Test of utility functions in number.js
</p>

<!-- Log output -->
<div id="testlog"> </div>

<!-- Tests follow -->
<script type="text/javascript" language="javascript" charset="utf-8">
// <![CDATA[

new Test.Unit.Runner({

testNumberMathMethods: function() {with(this) {
assertEqual(1, (0.9).round());
assertEqual(-2, (-1.9).floor());
assertEqual(-1, (-1.9).ceil());

$w('abs floor round ceil').each( function(method){
assertEqual(Math[method](Math.PI), Math.PI[method]());
});
}},

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());
}}

}, 'testlog');

// ]]>
</script>
</body>
</html>

0 comments on commit 86118c3

Please sign in to comment.