Skip to content

Commit

Permalink
Add call helper (#380)
Browse files Browse the repository at this point in the history
* Add call helper

* add test

* add readme

* add export
  • Loading branch information
snewcomer authored Sep 2, 2020
1 parent 8ef94db commit ab6dc9f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Watch a free video overview presented by EmberMap:
- [`compact`](#compact)
- [`contains`](#contains)
- [`append`](#append)
- [`call`](#call)
- [`chunk`](#chunk)
- [`without`](#without)
- [`shuffle`](#shuffle)
Expand Down Expand Up @@ -550,6 +551,19 @@ Appends the given arrays and/or values into a single flat array.

**[⬆️ back to top](#table-of-contents)**

#### `call`
Calls the given function with arguments

```hbs
{{#each (call (fn this.callMeWith @daysInMonth) as |week|}}
{{#each week as |day|}}
{{day}}
{{/each}}
{{/each}}
```
**[⬆️ back to top](#table-of-contents)**
#### `chunk`
Returns the given array split into sub-arrays the length of the given value.
Expand Down
27 changes: 27 additions & 0 deletions addon/helpers/call.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Helper from '@ember/component/helper';

/**
* Calls a function passed within a template and returns its value.
* In order to pass arguments to the function being called, you must
* curry the function using the `fn` helper.
*
```example
<div data-metrics={{call (fn this.myMetrics (hash item=@item))}}
```
*
* @function apply
* @param {Array<Function>} fn - The function to be called
* @param {*=} thisArg - An optional `this` context
*/
export function call([fn, thisArg]) {
if (fn) {
if (thisArg) {
return fn.apply(thisArg);
} else {
return fn();
}
}
}

export default Helper.helper(call);

1 change: 1 addition & 0 deletions app/helpers/call.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default, call } from 'ember-composable-helpers/helpers/call';
33 changes: 33 additions & 0 deletions tests/integration/helpers/call-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';

module('Integration | Helper | call', function (hooks) {
setupRenderingTest(hooks);

test('it renders a function return value with curried arguments', async function (assert) {
this.fn = function (value) {
return value.toUpperCase();
};

await render(hbs`{{call (fn this.fn "foo")}}`);

assert.dom(this.element).hasText('FOO');
});

test('it calls a function with this binding', async function (assert) {
this.that = {
value: 'foo',
};

this.fn = function () {
return this.value.toUpperCase();
};

await render(hbs`{{call this.fn this.that}}`);

assert.dom(this.element).hasText('FOO');
});
});

0 comments on commit ab6dc9f

Please sign in to comment.