Skip to content

Commit

Permalink
Fixes jashkenas#1503. Stops the bikeshedding.
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Mar 5, 2014
1 parent ce44751 commit d7b0c1a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
11 changes: 5 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@
<li>- <a href="#once">once</a></li>
<li>- <a href="#after">after</a></li>
<li>- <a href="#wrap">wrap</a></li>
<li>- <a href="#complement">complement</a></li>
<li>- <a href="#negate">negate</a></li>
<li>- <a href="#compose">compose</a></li>
</ul>

Expand Down Expand Up @@ -1223,14 +1223,13 @@ <h2 id="functions">Function (uh, ahem) Functions</h2>
=&gt; 'before, hello: moe, after'
</pre>

<p id="complement">
<b class="header">complement</b><code>_.complement(predicate)</code>
<p id="negate">
<b class="header">negate</b><code>_.negate(predicate)</code>
<br />
Returns a function that takes the same arguments as <b>predicate</b>,
has the same effects, if any, and returns the opposite truth value.
Returns a new negated version of the <b>predicate</b> function.
</p>
<pre>
var isFalsy = _.complement(Boolean);
var isFalsy = _.negate(Boolean);
_.find([-2, -1, 0, 1, 2], isFalsy);
=&gt; 0
</pre>
Expand Down
6 changes: 3 additions & 3 deletions test/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,10 @@
deepEqual(ret, [noop, ['whats', 'your'], 'vector', 'victor']);
});

test('complement', function() {
test('negate', function() {
var isOdd = function(n){ return (n & 1) == 1; };
equal(_.complement(isOdd)(2), true, 'should return the complement of the given function');
equal(_.complement(isOdd)(3), false, 'should return the complement of the given function');
equal(_.negate(isOdd)(2), true, 'should return the complement of the given function');
equal(_.negate(isOdd)(3), false, 'should return the complement of the given function');
});

test('compose', function() {
Expand Down
7 changes: 3 additions & 4 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@

// Return all the elements for which a truth test fails.
_.reject = function(obj, predicate, context) {
return _.filter(obj, _.complement(predicate), context);
return _.filter(obj, _.negate(predicate), context);
};

// Determine whether all of the elements match a truth test.
Expand Down Expand Up @@ -782,9 +782,8 @@
return _.partial(wrapper, func);
};

// Returns a function that takes the same arguments as the given predicate,
// has the same effects, if any, and returns the opposite truth value.
_.complement = function(predicate) {
// Returns a negated version of the passed-in predicate.
_.negate = function(predicate) {
return function() {
return !predicate.apply(this, arguments);
};
Expand Down

0 comments on commit d7b0c1a

Please sign in to comment.