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 'includes' helper #37

Merged
merged 2 commits into from
Jun 8, 2016
Merged

Add 'includes' helper #37

merged 2 commits into from
Jun 8, 2016

Conversation

sanjeevkpandit
Copy link
Contributor

@sanjeevkpandit sanjeevkpandit commented Jun 7, 2016

  • Closes Need an array helper contains or includes #36
  • Add includes helper to check for the existence of the value in array strictly(value + data type).
  • Can check non-strictly(value only) by sending third argument as false.
  • Checks strictly by default.

Parameters:

params [array] The array. (Required)
params [mixed] The value to checked for existence in the array. (Required)
params [boolean] FALSE for non-strict checking. TRUE by default. (Optional)

Returns boolean

Usage:

var array = [1, 2, 3];
var value = 2;

{{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:

{{#if (includes array value)}}
   <!-- Do something -->
{{/if}}

{{ifx (includes array value) 'Yes' 'No'}}

Test Cases:

includes
      ✓ should return true if array strictly includes the element after compilation
      ✓ should return false if array strictly does not include the element after compilation
      ✓ should return true if array non-strictly includes the element after compilation
      ✓ should return false if array non-strictly does not include the element after compilation
      ✓ should return false if array argument is not an array
      ✓ should return false if array argument is an empty array
      ✓ should return false if array argument is not an array
      ✓ should return false if array checks for existence of an empty string

var array = [1, 2, 3];
var value = 2;

{{includes array value}} => TRUE
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small case true

@codecov-io
Copy link

codecov-io commented Jun 7, 2016

Current coverage is 98.55%

Merging #37 into master will increase coverage by 0.07%

@@             master        #37   diff @@
==========================================
  Files             7          7          
  Lines           131        138     +7   
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
+ Hits            129        136     +7   
  Misses            2          2          
  Partials          0          0          

Powered by Codecov. Last updated by e2ad3fd...ca687fb

return false;
}

for (let index in array) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can reduce the nested ifs a little bit:

if ((strict && array[index] === value) ||
    (!strict && array[index] == value)
) {
    return true;
}

@kabirbaidhya
Copy link
Contributor

See this:

Merging #37 into master will decrease coverage by 0.60%

You need one or more tests

Parameters:
```
params [array] The array. (Required)
params [mixed] The value to checked for existence in the array. (Required)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Value to be checked

@mesaugat mesaugat merged commit 4214396 into master Jun 8, 2016
@mesaugat mesaugat deleted the includes-helper branch June 8, 2016 06:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants