Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalize _.compact #990

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 12 additions & 13 deletions index.html
Expand Up @@ -200,6 +200,7 @@
<li>- <a href="#reduceRight">reduceRight</a></li>
<li>- <a href="#find">find</a></li>
<li>- <a href="#filter">filter</a></li>
<li>- <a href="#compact">compact</a></li>
<li>- <a href="#where">where</a></li>
<li>- <a href="#findWhere">findWhere</a></li>
<li>- <a href="#reject">reject</a></li>
Expand All @@ -226,7 +227,6 @@
<li>- <a href="#initial">initial</a></li>
<li>- <a href="#last">last</a></li>
<li>- <a href="#rest">rest</a></li>
<li>- <a href="#compact">compact</a></li>
<li>- <a href="#flatten">flatten</a></li>
<li>- <a href="#without">without</a></li>
<li>- <a href="#union">union</a></li>
Expand Down Expand Up @@ -499,6 +499,17 @@ <h2 id="collections">Collection Functions (Arrays or Objects)</h2>
=&gt; [2, 4, 6]
</pre>

<p id="compact">
<b class="header">compact</b><code>_.compact(list)</code>
<br />
Returns a copy of the <b>list</b> with all falsy values removed.
In JavaScript, <i>false</i>, <i>null</i>, <i>0</i>, <i>""</i>,
<i>undefined</i> and <i>NaN</i> are all falsy.
</p>
<pre>
_.compact([0, 1, false, 2, '', 3]);
=&gt; [1, 2, 3]
</pre>
<p id="where">
<b class="header">where</b><code>_.where(list, properties)</code>
<br />
Expand Down Expand Up @@ -753,18 +764,6 @@ <h2 id="arrays">Array Functions</h2>
<pre>
_.rest([5, 4, 3, 2, 1]);
=&gt; [4, 3, 2, 1]
</pre>

<p id="compact">
<b class="header">compact</b><code>_.compact(array)</code>
<br />
Returns a copy of the <b>array</b> with all falsy values removed.
In JavaScript, <i>false</i>, <i>null</i>, <i>0</i>, <i>""</i>,
<i>undefined</i> and <i>NaN</i> are all falsy.
</p>
<pre>
_.compact([0, 1, false, 2, '', 3]);
=&gt; [1, 2, 3]
</pre>

<p id="flatten">
Expand Down
6 changes: 0 additions & 6 deletions test/arrays.js
Expand Up @@ -53,12 +53,6 @@ $(document).ready(function() {
equal(_.last(null), undefined, 'handles nulls');
});

test("compact", function() {
equal(_.compact([0, 1, false, 2, false, 3]).length, 3, 'can trim out all falsy values');
var result = (function(){ return _.compact(arguments).length; })(0, 1, false, 2, false, 3);
equal(result, 3, 'works on an arguments object');
});

test("flatten", function() {
var list = [1, [2], [3, [[[4]]]]];
deepEqual(_.flatten(list), [1,2,3,4], 'can flatten nested arrays');
Expand Down
7 changes: 7 additions & 0 deletions test/collections.js
Expand Up @@ -155,6 +155,13 @@ $(document).ready(function() {
strictEqual(_.find(array, function() { return false; }), void 0, 'should return `undefined` if `value` is not found');
});

test("compact", function() {
equal(_.compact([0, 1, false, 2, false, 3]).length, 3, 'can trim out all falsy values from an array');
deepEqual(_.compact({a: 0, b: 1, c: 2, d: false, e: 3, f: NaN}), {b: 1, c: 2, e: 3}, 'can trim out all falsy values from an object');
var result = (function(){ return _.compact(arguments).length; })(0, 1, false, 2, false, 3);
equal(result, 3, 'works on an arguments object');
});

test('detect', function() {
var result = _.detect([1, 2, 3], function(num){ return num * 2 == 4; });
equal(result, 2, 'found the first "2" and broke the loop');
Expand Down
23 changes: 18 additions & 5 deletions underscore.js
Expand Up @@ -176,6 +176,24 @@
return results;
};

// Trim out all falsy values from an array or object.
_.compact = function(obj) {
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; i++) {
if (keys[i] != i) {
var copy = {};
each(obj, function(value, key) {
if(value)
copy[key] = obj[key];
});
return copy;
}
}
if (_.isObject(obj))
obj = slice.call(obj);
return _.filter(obj, _.identity);
};

// Return all the elements for which a truth test fails.
_.reject = function(obj, iterator, context) {
return _.filter(obj, function(value, index, list) {
Expand Down Expand Up @@ -415,11 +433,6 @@
return slice.call(array, (n == null) || guard ? 1 : n);
};

// Trim out all falsy values from an array.
_.compact = function(array) {
return _.filter(array, _.identity);
};

// Internal implementation of a recursive `flatten` function.
var flatten = function(input, shallow, output) {
each(input, function(value) {
Expand Down