Skip to content

Commit

Permalink
Pick Helper (#366)
Browse files Browse the repository at this point in the history
* Introduce pick object helper

* Alphabetize object helpers in README
  • Loading branch information
mattmcmanus authored Jun 3, 2020
1 parent 9eb2030 commit 1d4a32a
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 42 deletions.
106 changes: 64 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ Watch a free video overview presented by EmberMap:
- [`previous`](#previous)
- [`has-previous`](#has-previous)
- [Object helpers](#object-helpers)
- [`entries`](#entries)
- [`from-entries`](#from-entries)
- [`group-by`](#group-by)
- [`keys`](#keys)
- [`pick`](#pick)
- [`values`](#values)
- [`entries`](#entries)
- [`from-entries`](#from-entries)
- [Math helpers](#math-helpers)
- [`inc`](#inc)
- [`dec`](#dec)
Expand Down Expand Up @@ -669,6 +670,45 @@ parameter, `useDeepEqual`, to flag whether a deep equal comparison should be per

### Object helpers

#### `entries`
Returns an array of a given object's own enumerable string-keyed property `[key, value]` pairs

```hbs
{{#each (entries object) as |entry|}}
{{get entry 0}}:{{get entry 1}}
{{/each}}
```

You can pair it with other array helpers too. For example

```hbs
{{#each (sort-by myOwnSortByFunction (entries myObject)) as |entry|}}
{{get entry 0}}
{{/each}}`);
```

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

#### `from-entries`
Converts a two-dimensional array of `[key, value]` pairs into an Object

```hbs
{{#each-in (from-entries entries) as |key value|}}
{{key}}:{{value}}
{{/each}}
```

You can pair it with other array helpers too. For example, to copy only
properties with non-falsey values:

```hbs
{{#each-in (from-entries (filter-by "1" (entries myObject))) as |k v|}}
{{k}}: {{v}}
{{/each-in}}`);
```

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

#### `group-by`
Returns an object where the keys are the unique values of the given property, and the values are an array with all items of the array that have the same value of that property.

Expand Down Expand Up @@ -701,6 +741,27 @@ Returns an array of keys of given object.

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

#### `pick`
Receives an object and picks a specified path off of it to pass on. Intended for use with `{{on}}` modifiers placed on form elements.

```hbs
<input
...
{{on 'input' (pipe (pick 'target.value') this.onInput)}}
/>
```

It also supports an optional second argument to make common usage more ergonomic.

```hbs
<input
...
{{on 'input' (pick 'target.value' this.onInput)}}
/>
```

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

#### `values`
Returns an array of values from the given object.

Expand All @@ -715,46 +776,7 @@ Returns an array of values from the given object.
{{/with}}
```

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

#### `entries`
Returns an array of a given object's own enumerable string-keyed property `[key, value]` pairs

```hbs
{{#each (entries object) as |entry|}}
{{get entry 0}}:{{get entry 1}}
{{/each}}
```

You can pair it with other array helpers too. For example

```hbs
{{#each (sort-by myOwnSortByFunction (entries myObject)) as |entry|}}
{{get entry 0}}
{{/each}}`);
```

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

#### `from-entries`
Converts a two-dimensional array of `[key, value]` pairs into an Object

```hbs
{{#each-in (from-entries entries) as |key value|}}
{{key}}:{{value}}
{{/each}}
```

You can pair it with other array helpers too. For example, to copy only
properties with non-falsey values:

```hbs
{{#each-in (from-entries (filter-by "1" (entries myObject))) as |k v|}}
{{k}}: {{v}}
{{/each-in}}`);
```

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

---

Expand Down
14 changes: 14 additions & 0 deletions addon/helpers/pick.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { helper } from '@ember/component/helper';
import { get } from '@ember/object';

export default helper(function event([path, action]/*, hash*/) {
return function(event) {
let value = get(event, path);

if (!action) {
return value;
}

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

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

test("Works when used with {{on}} modifier and pipe", async function(assert) {
assert.expect(1);

this.set('onFocus', function(value) {
assert.equal(value, 'pizza party', 'The action receives the correct value');
})

await render(hbs`
<input
id="test-input"
value="pizza party"
{{on 'focusin' (pipe (pick 'target.value') this.onFocus)}}
/>
`);

await click('#test-input');
});

test("Shorthand works when used with {{on}} modifier and optional action is provided", async function(assert) {
assert.expect(1);

this.set('onFocus', function(value) {
assert.equal(value, 'pizza party', 'The action receives the correct value');
})

await render(hbs`
<input
id="test-input"
value="pizza party"
{{on 'focusin' (pick 'target.value' this.onFocus)}}
/>
`);

await click('#test-input');
});
});

0 comments on commit 1d4a32a

Please sign in to comment.