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 is-present helper #88

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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ gte | `if (a >= b)` | `{{if (gte a b)}}`
lt | `if (a < b)` | `{{if (lt a b)}}` | No |
lte | `if (a <= b)` | `{{if (lte a b)}}` | No |
is-array | `if (Ember.isArray(a))` | `{{if (is-array a)}}` | Yes |
is-empty | `if (Ember.isEmpty(a))` | `{{if (is-empty a)}}` | No |
is-empty | `if (Ember.isEmpty(a))` | `{{if (is-empty a)}}` | No |
is-equal | `if (Ember.isEqual(a, b))` | `{{if (is-equal a b)}}` | No |
is-present | `if (Ember.isPresent(a))` | `{{if (is-present a)}}` | No |

### API

Expand Down
6 changes: 6 additions & 0 deletions addon/helpers/is-present.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { helper } from '@ember/component/helper';
import { isPresent } from '@ember/utils';

export default helper(function([obj]) {
return isPresent(obj)
})
1 change: 1 addition & 0 deletions app/helpers/is-present.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'ember-truth-helpers/helpers/is-present';
48 changes: 48 additions & 0 deletions tests/unit/helpers/is-present-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';

module('helper:is-present', function(hooks) {
setupRenderingTest(hooks);

test('undefined/null values', async function(assert) {
this.set('thisIsUndefined', undefined)
this.set('thisIsNull', null)
this.set('thisIsNotNull', new Date())
await render(hbs("[{{is-present thisIsUndefined}}] [{{is-present thisIsNull}}] [{{is-present thisIsNotNull}}]"));

assert.equal(this.$().text(), '[false] [false] [true]', 'value should be "[false] [false] [true]"');
});

test('boolean values', async function(assert) {
await render(hbs("[{{is-present true}}] [{{is-present false}}]"));

assert.equal(this.$().text(), '[true] [true]', 'value should be "[true] [true]"');
});

test('objects', async function(assert) {
this.set('presentObject', {})
this.set('notpresentObject', { some: 'object' })
await render(hbs("[{{is-present presentObject}}] [{{is-present notpresentObject}}]"));

assert.equal(this.$().text(), '[true] [true]', 'value should be "[true] [true]"');
});

test('arrays', async function(assert) {
this.set('presentArray', [])
this.set('notpresentArray', [ 'a', 8, {} ])
await render(hbs("[{{is-present presentArray}}] [{{is-present notpresentArray}}]"));

assert.equal(this.$().text(), '[false] [true]', 'value should be "[false] [true]"');
});

test('strings', async function(assert) {
this.set('presentString', '')
this.set('whitespaceString', ' ')
this.set('notpresentString', 'full of text')
await render(hbs("[{{is-present presentString}}] [{{is-present whitespaceString}}] [{{is-present notpresentString}}]"));

assert.equal(this.$().text(), '[false] [false] [true]', 'value should be "[false] [false] [true]"');
});
});