Skip to content

Commit

Permalink
Updating intersect helper to work with ManyArrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
Garrett Murphey committed Nov 20, 2019
1 parent 55f4f08 commit ce6f0a3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
6 changes: 3 additions & 3 deletions addon/helpers/intersect.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { helper } from '@ember/component/helper';
import { isArray as isEmberArray } from '@ember/array';
import { isArray as isEmberArray, A as emberArray } from '@ember/array';

export function intersect([...arrays]) {
let confirmedArrays = arrays.map(array => {
Expand All @@ -9,9 +9,9 @@ export function intersect([...arrays]) {
let results = confirmedArrays.pop().filter(candidate => {
for (let i = 0; i < arrays.length; i++) {
let found = false;
let array = arrays[i];
let array = emberArray(arrays[i]);
for (let j = 0; j < array.length; j++) {
if (array[j] === candidate) {
if (array.objectAt(j) === candidate) {
found = true;
break;
}
Expand Down
25 changes: 25 additions & 0 deletions tests/integration/helpers/intersect-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,29 @@ module('Integration | Helper | {{intersect}}', function(hooks) {

assert.equal(find('*').textContent.trim(), 'this is all that will render', 'no error is thrown');
});

test('it accepts an ember data array', async function(assert) {
let store = this.owner.lookup('service:store');
let person = store.createRecord('person', {
name: 'Adam'
});

person.get('pets').pushObjects([
store.createRecord('pet', { name: 'Kirby' }),
store.createRecord('pet', { name: 'Jake' })
]);

store.createRecord('pet', { name: 'Eva' });

this.set('person', person);
this.set('allPets', store.peekAll('pet'));

await this.render(hbs`
{{~#each (intersect person.pets allPets) as |pet|~}}
{{~pet.name~}}
{{~/each~}}
`);

assert.equal(this.element.textContent.trim(), 'KirbyJake', 'the pets belonging to the person are shown');
});
});

0 comments on commit ce6f0a3

Please sign in to comment.