-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
9d3ea1e
commit 4b1ab63
Showing
5 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default, compact } from 'ember-composable-helpers/helpers/compact'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); |