From ca687fb0b5b82712ce264ba03f487867048118c0 Mon Sep 17 00:00:00 2001 From: sanjeevkpandit Date: Wed, 8 Jun 2016 09:37:12 +0545 Subject: [PATCH] Update README.md and add few test cases for 'includes' helper --- README.md | 14 ++++++------- src/helpers/conditionals.js | 12 +++-------- tests/helpers/conditionals.spec.js | 32 ++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 13fc88f..92fdca9 100644 --- a/README.md +++ b/README.md @@ -356,20 +356,18 @@ Usage: var array = [1, 2, 3]; var value = 2; -{{includes array value}} => TRUE +{{includes array value}} => true -value = '2' -{{includes array value}} => FALSE -{{includes array value true}} => FALSE -{{includes array value false}} => TRUE - -// Since it returns a boolean, we can use it in any conditional helpers like: +var value = '2' +{{includes array value}} => false +{{includes array value true}} => false +{{includes array value false}} => true {{#if (includes array value)}} {{/if}} -Includes = {{ifx (includes array value) 'Yes' 'No'}} +{{ifx (includes array value) 'Yes' 'No'}} ``` ### Strings diff --git a/src/helpers/conditionals.js b/src/helpers/conditionals.js index 565e71e..2fe6d1a 100644 --- a/src/helpers/conditionals.js +++ b/src/helpers/conditionals.js @@ -278,19 +278,13 @@ export default { * @returns boolean */ includes: (array, value, strict = true) => { - if (!Array.isArray(array) || array.length === 0) { + if (!isArray(array) || array.length === 0) { return false; } for (let index in array) { - if (strict) { - if (array[index] === value) { - return true; - } - } else { - if (array[index] == value) { - return true; - } + if ((strict && array[index] === value) || (!strict && array[index] == value)) { + return true; } } diff --git a/tests/helpers/conditionals.spec.js b/tests/helpers/conditionals.spec.js index 30e6f5f..5ef96ad 100644 --- a/tests/helpers/conditionals.spec.js +++ b/tests/helpers/conditionals.spec.js @@ -441,5 +441,37 @@ describe('conditionals', () => { expect(template({array: array, value: value})).toEqual('false'); }); + + it('should return false if array argument is not an array', () => { + var value = 2; + var array = 3; + var template = compile('{{includes array value}}'); + + expect(template({array: array, value: value})).toEqual('false'); + }); + + it('should return false if array argument is an empty array', () => { + var value = 2; + var array = []; + var template = compile('{{includes array value}}'); + + expect(template({array: array, value: value})).toEqual('false'); + }); + + it('should return false if array argument is not an array', () => { + var value = 2; + var array = 3; + var template = compile('{{includes array value}}'); + + expect(template({array: array, value: value})).toEqual('false'); + }); + + it('should return false if array checks for existence of an empty string', () => { + var value = ''; + var array = [5, 6, 7]; + var template = compile('{{includes array value}}'); + + expect(template({array: array, value: value})).toEqual('false'); + }); }); });