Skip to content

Commit

Permalink
Update README.md and add few test cases for 'includes' helper
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjeevkpandit committed Jun 8, 2016
1 parent 9c3a761 commit ca687fb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)}}
<!-- Do something -->
{{/if}}
Includes = {{ifx (includes array value) 'Yes' 'No'}}
{{ifx (includes array value) 'Yes' 'No'}}
```

### Strings
Expand Down
12 changes: 3 additions & 9 deletions src/helpers/conditionals.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
32 changes: 32 additions & 0 deletions tests/helpers/conditionals.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});

0 comments on commit ca687fb

Please sign in to comment.