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

Updating intersect helper to work with ManyArrays #341

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: 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question @gmurphey before merging - what makes this code as is on master not able to work with Ember arrays?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping!

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');
});
});