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

Fix filter-by not working when undefined is explicitly passed #439

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions addon/helpers/filter-by.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { helper } from '@ember/component/helper';
import { isArray as isEmberArray } from '@ember/array';
import { isEmpty, isPresent } from '@ember/utils';
import { isEmpty } from '@ember/utils';
import { get } from '@ember/object';
import isEqual from '../utils/is-equal';
import asArray from '../utils/as-array';

export function filterBy([byPath, value, array]) {

let isPresent = true;
if (!isEmberArray(array) && isEmberArray(value)) {
array = value;
value = undefined;
isPresent = false;
}

array = asArray(array);
Expand All @@ -20,7 +22,7 @@ export function filterBy([byPath, value, array]) {

let filterFn;

if (isPresent(value)) {
if (isPresent) {
if (typeof value === 'function') {
filterFn = (item) => value(get(item, byPath));
} else {
Expand Down
18 changes: 18 additions & 0 deletions tests/integration/helpers/filter-by-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ module('Integration | Helper | {{filter-by}}', function(hooks) {
assert.dom().hasText('ac', 'b is filtered out');
});

test('It filters by `undefined`', async function(assert) {
this.set('array', emberArray([
{ foo: true, name: 'a' },
{ foo: false, name: 'b' },
{ foo: undefined, name: 'c' },
{ foo: null, name: 'd' },
{ foo: 'x', name: 'e' },
]));

await render(hbs`
{{~#each (filter-by 'foo' undefined this.array) as |item|~}}
{{~item.name~}}
{{~/each~}}
`);

assert.dom().hasText('c', 'a, b, d and e are filtered out');
});

test('It filters by truthiness', async function(assert) {
this.set('array', emberArray([
{ foo: 'x', name: 'a' },
Expand Down