Skip to content

Commit

Permalink
adds compacy helper
Browse files Browse the repository at this point in the history
JSCS Errors

Fixes error on Ember 1.13

Use isPresent

adds compact to index

one liner

guard for non-arrays

add to the readme

Guard before setting the array

props first; simplicy filter CP
  • Loading branch information
spencer516 committed Feb 22, 2016
1 parent 9d3ea1e commit 4b1ab63
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ only: ['pipe'] // imports only `pipe`
+ [`repeat`](#repeat)
+ [`range`](#range)
+ [`join`](#join)
+ [`compact`](#compact)
* [Object](#object-helpers)
+ [`group-by`](#group-by)
* [Math](#math-helpers)
Expand Down Expand Up @@ -286,6 +287,17 @@ Joins the given array with an optional separator into a string.
**[⬆️ back to top](#available-helpers)**
#### `compact`
Removes blank items from an array.
```hbs
{{#each (compact arrayWithBlanks) as |notBlank|}}
{{notBlank}} is most definitely not blank!
{{/each}}
```
**[⬆️ back to top](#available-helpers)**
---
### Object helpers
Expand Down
32 changes: 32 additions & 0 deletions addon/helpers/compact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Ember from 'ember';

const {
Helper,
get,
set,
observer,
isPresent,
isArray,
A: emberArray,
computed: { filter }
} = Ember;

export default Helper.extend({
array: null,

compute([array]) {
if (!isArray(array)) {
return emberArray([array]);
}

set(this, 'array', array);

return get(this, 'content');
},

content: filter('array', isPresent),

contentDidChange: observer('content', function() {
this.recompute();
})
});
1 change: 1 addition & 0 deletions addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export { default as DecHelper } from './helpers/dec';
export { default as ToggleHelper } from './helpers/toggle';
export { default as JoinHelper } from './helpers/join';
export { default as WHelper } from './helpers/w';
export { default as CompactHelper } from './helpers/compacy';
1 change: 1 addition & 0 deletions app/helpers/compact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default, compact } from 'ember-composable-helpers/helpers/compact';
62 changes: 62 additions & 0 deletions tests/integration/helpers/compact-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import Ember from 'ember';

const { A: emberArray, run } = Ember;

moduleForComponent('compact', 'Integration | Helper | {{compact}}', {
integration: true
});

test('Removes empty values in standard arrays', function(assert) {
this.set('array', emberArray([1, 2, null, 3, false]));
this.render(hbs`
{{~#each (compact array) as |value|~}}
{{value}}
{{~/each~}}
`);

assert.equal(this.$().text().trim(), '123false', 'null is removed');
});

test('It graceully handles non-array values', function(assert) {
this.set('notArray', 1);
this.render(hbs`
{{~#each (compact notArray) as |value|~}}
{{value}}
{{~/each~}}
`);

assert.equal(this.$().text().trim(), '1', 'the non array value is rendered');
});

test('It recomputes the filter if the array changes', function(assert) {
this.set('array', emberArray([1, 2, null, 3]));
this.render(hbs`
{{~#each (compact array) as |value|~}}
{{value}}
{{~/each~}}
`);

assert.equal(this.$().text().trim(), '123', 'null is removed');

this.set('array', emberArray([1, null, null, 3]));

assert.equal(this.$().text().trim(), '13', 'null is removed');
});

test('It recomputes the filter if an item in the array changes', function(assert) {
let array = emberArray([1, 2, null, 3]);
this.set('array', array);
this.render(hbs`
{{~#each (compact array) as |value|~}}
{{value}}
{{~/each~}}
`);

assert.equal(this.$().text().trim(), '123', 'null is removed');

run(() => array.replace(2, 1, [5]));

assert.equal(this.$().text().trim(), '1253', 'null is removed');
});

0 comments on commit 4b1ab63

Please sign in to comment.