From 41437d6dfb1933c9ad8079eb73bdaf21f8015a6b Mon Sep 17 00:00:00 2001 From: Eric Honkanen Date: Wed, 16 May 2018 12:59:49 -0700 Subject: [PATCH] Add helper:bool --- README.md | 1 + addon/helpers/bool.js | 7 +++++++ app/helpers/bool.js | 1 + tests/unit/helpers/bool-test.js | 17 +++++++++++++++++ 4 files changed, 26 insertions(+) create mode 100644 addon/helpers/bool.js create mode 100644 app/helpers/bool.js create mode 100644 tests/unit/helpers/bool-test.js diff --git a/README.md b/README.md index f027a52..2c1820f 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ HTMLBars template helpers for additional truth logic in `if` and `unless` statem Helper | JavaScript | HTMLBars | Variable argument count allowed | ---------|-----------------------------------------------|-------------------------|---------------------------------| +bool | `Boolean(a)` | `{{bool a}}` | No eq | `if (a === b)` | `{{if (eq a b)}}` | No | not-eq | `if (a !== b)` | `{{if (not-eq a b)}}` | No | not | `if (!a)` | `{{if (not a)}}` | Yes | diff --git a/addon/helpers/bool.js b/addon/helpers/bool.js new file mode 100644 index 0000000..64f5b2f --- /dev/null +++ b/addon/helpers/bool.js @@ -0,0 +1,7 @@ +import { helper } from '@ember/component/helper'; + +export function bool(params) { + return Boolean(params[0]); +} + +export default helper(bool); diff --git a/app/helpers/bool.js b/app/helpers/bool.js new file mode 100644 index 0000000..d9cbd68 --- /dev/null +++ b/app/helpers/bool.js @@ -0,0 +1 @@ +export { default, bool } from 'ember-truth-helpers/helpers/bool'; diff --git a/tests/unit/helpers/bool-test.js b/tests/unit/helpers/bool-test.js new file mode 100644 index 0000000..bd49960 --- /dev/null +++ b/tests/unit/helpers/bool-test.js @@ -0,0 +1,17 @@ +import { module, test } from 'qunit'; +import { setupRenderingTest } from 'ember-qunit'; +import { render } from '@ember/test-helpers'; +import hbs from 'htmlbars-inline-precompile'; + +module('helper:bool', function(hooks) { + setupRenderingTest(hooks); + + test('simple test 1', async function(assert) { + this.set('object', {}); + this.set('array', []); + + await render(hbs("[{{bool true}}] [{{bool false}}] [{{bool null}}] [{{bool undefined}}] [{{bool ''}}] [{{bool ' '}}] [{{bool object}}] [{{bool array}}]")); + + assert.equal(this.$().text(), '[true] [false] [false] [false] [false] [true] [true] [true]', 'value should be "[true] [false] [false] [false] [false] [true] [true]"'); + }); +});