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

add support for array includes #111

Open
wants to merge 2 commits 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ lte | `if (a <= b)` | `{{if (lte a b)}}`
is-array | `if (Ember.isArray(a))` | `{{if (is-array a)}}` | Yes |
is-empty | `if (Ember.isEmpty(a))` | `{{if (is-empty a)}}` | No |
is-equal | `if (Ember.isEqual(a, b))` | `{{if (is-equal a b)}}` | No |
includes | `if (a.includes(b))` | `{{if (includes a b)}}` | No |

<sup>
* Unlike their JavaScript counterparts, these expressions do <em>not</em> short circuit.
Expand Down
11 changes: 11 additions & 0 deletions addon/helpers/includes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { helper } from '@ember/component/helper';

export function includes([haystack, pin]) {
if (!haystack || haystack.constructor.name !== 'Array') {
return false;
}

return haystack.includes(pin);
}

export default helper(includes);
1 change: 1 addition & 0 deletions app/helpers/includes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default, includes } from 'ember-truth-helpers/helpers/includes';
63 changes: 63 additions & 0 deletions tests/unit/helpers/includes-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { run } from '@ember/runloop';
import EmberObject from '@ember/object';
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render, find } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';

module('helper:includes', function(hooks) {
setupRenderingTest(hooks);

test('first param is an array', async function(assert) {
const fakeContextObject = EmberObject.create({
valueA: [1, 2, 3, 4, 5]
});
this.set('contextChild', fakeContextObject);

await render(
hbs`[{{includes contextChild.valueA 5}}] [{{includes contextChild.valueA 6}}] [{{includes contextChild.valueA 9}}]`
);

assert.equal(find('*').textContent, '[true] [false] [false]', 'value should be "[true] [false] [false]"');

run(fakeContextObject, 'set', 'valueA', [1, 2, 3, 4, '5']);
assert.equal(find('*').textContent, '[false] [false] [false]', 'value should be "[false] [false] [false]"');

run(fakeContextObject, 'set', 'valueA', []);
assert.equal(find('*').textContent, '[false] [false] [false]', 'value should be "[false] [false] [false]"');
});

test('first param is not an array', async function(assert) {
const fakeContextObject = EmberObject.create({
valueA: null,
valueB: 'string',
valueC: 42,
valueD: {}
});
this.set('contextChild', fakeContextObject);

await render(
hbs`[{{includes contextChild.valueA 5}}] [{{includes contextChild.valueA 6}}] [{{includes contextChild.valueA 9}}]`
);

assert.equal(find('*').textContent, '[false] [false] [false]', 'value should be "[false] [false] [false]"');

await render(
hbs`[{{includes contextChild.valueB 'st'}}] [{{includes contextChild.valueB 6}}] [{{includes contextChild.valueB 9}}]`
);

assert.equal(find('*').textContent, '[false] [false] [false]', 'value should be "[false] [false] [false]"');

await render(
hbs`[{{includes contextChild.valueC 'st'}}] [{{includes contextChild.valueC 6}}] [{{includes contextChild.valueC 9}}]`
);

assert.equal(find('*').textContent, '[false] [false] [false]', 'value should be "[false] [false] [false]"');

await render(
hbs`[{{includes contextChild.valueD 'st'}}] [{{includes contextChild.valueD 6}}] [{{includes contextChild.valueD 9}}]`
);

assert.equal(find('*').textContent, '[false] [false] [false]', 'value should be "[false] [false] [false]"');
});
});