Skip to content

Commit

Permalink
Use the equals() method where applicable in Enumerable search methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoglan committed Jun 4, 2009
1 parent 6b12ca5 commit 3fac332
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
6 changes: 3 additions & 3 deletions site/src/pages/enumerable.haml
Expand Up @@ -305,11 +305,11 @@
h3. @member(needle)@

Returns @true@ iff the collection contains any members equal to @needle@. Items are checked for
equality (@==@) rather than identity (@===@).
identity (@===@), or using the @equals()@ method if the objects implement it.

<pre class="prettyprint"> var list = new Collection(3,7,4,8,2);
list.member('7') // -> true
list.member('foo') // -> false</pre>
list.member('7') // -> false
list.member(7) // -> true</pre>

h3. @min(block, context)@

Expand Down
12 changes: 9 additions & 3 deletions source/enumerable.js
Expand Up @@ -13,6 +13,12 @@ JS.Enumerable = new JS.Module('Enumerable', {
return list.all(function(item) { return JS.isFn(item.compareTo) });
},

areEqual: function(one, another) {
return one.equals
? one.equals(another)
: (one === another);
},

Collection: new JS.Class({
initialize: function(array) {
this.length = 0;
Expand Down Expand Up @@ -47,7 +53,7 @@ JS.Enumerable = new JS.Module('Enumerable', {
var count = 0, object = block;

if (block && !JS.isFn(block))
block = function(x) { return x === object };
block = function(x) { return JS.Enumerable.areEqual(x, object) };

this.forEach(function() {
if (!block || block.apply(context || null, arguments))
Expand Down Expand Up @@ -163,7 +169,7 @@ JS.Enumerable = new JS.Module('Enumerable', {

this.forEachWithIndex(function(item, i) {
if (index !== null) return;
if (needle === item || (block && needle.apply(context || null, arguments)))
if (JS.Enumerable.areEqual(needle, item) || (block && needle.apply(context || null, arguments)))
index = i;
});
return index;
Expand Down Expand Up @@ -246,7 +252,7 @@ JS.Enumerable = new JS.Module('Enumerable', {
},

member: function(needle) {
return this.any(function(item) { return item === needle; });
return this.any(function(item) { return JS.Enumerable.areEqual(item, needle) });
},

min: function(block, context) {
Expand Down
10 changes: 2 additions & 8 deletions source/set.js
Expand Up @@ -7,12 +7,6 @@ JS.Set = new JS.Class('Set', {
if (list[i] !== undefined)
block.call(context || null, list[i], i);
}
},

areEqual: function(one, another) {
return one.equals
? one.equals(another)
: (one === another);
}
},

Expand Down Expand Up @@ -220,7 +214,7 @@ JS.Set = new JS.Class('Set', {

_indexOf: function(item) {
var i = this._members.length,
equal = this.klass.areEqual;
equal = JS.Enumerable.areEqual;

while (i--) {
if (equal(item, this._members[i])) return i;
Expand Down Expand Up @@ -257,7 +251,7 @@ JS.SortedSet = new JS.Class('SortedSet', JS.Set, {
i = 0,
d = n,
compare = this.klass.compare,
equal = this.klass.areEqual,
equal = JS.Enumerable.areEqual,
found;

if (n === 0) return insertionPoint ? 0 : -1;
Expand Down

0 comments on commit 3fac332

Please sign in to comment.