Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit 97a7fa2

Browse files
josediazseKent C. Dodds
authored andcommitted
feat(contains): Add contains function (#128)
* feat(contains): Add contains function * Fixing linting errors for hex2rgb.test.js
1 parent f97c874 commit 97a7fa2

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

src/contains.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export default contains
2+
3+
/**
4+
* Original Source: https://stackoverflow.com/questions/237104
5+
*
6+
* Checks if a given element is present in an array.
7+
*
8+
* @param {Object[]} array - the array to scan for the given element.
9+
* @param {Object} element - the element to look for.
10+
*
11+
* @returns {boolean} true if array contains element, false otherwise
12+
*/
13+
function contains(array, element) {
14+
let result = false
15+
for (let i = 0; i < array.length; i++) {
16+
if (array[i] === element) {
17+
result = true
18+
break
19+
}
20+
}
21+
return result
22+
}

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import slugify from './slugify'
4444
import convertToRoman from './convertToRoman'
4545
import gcd from './gcd'
4646
import range from './range'
47+
import contains from './contains'
4748

4849
export {
4950
isOdd,
@@ -92,4 +93,5 @@ export {
9293
convertToRoman,
9394
gcd,
9495
range,
96+
contains,
9597
}

test/contains.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import test from 'ava'
2+
import {contains} from '../src'
3+
4+
test('array contains element', t => {
5+
t.true(contains([1, 2, 3], 3))
6+
})
7+
8+
test('array does not contain element', t => {
9+
t.false(contains([1, 2, 3], 4))
10+
})

test/hex2rgb.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import test from 'ava'
22
import {hex2rgb} from '../src'
33

4-
test('test hex without opacity value', t=>{
4+
test('test hex without opacity value', t => {
55
const hex = '#ff0000'
66
const expected = 'rgba(255,0,0)'
77
const actual = hex2rgb(hex)
88
t.deepEqual(actual, expected)
99
})
1010

11-
test('test hex with opacity value', t=>{
11+
test('test hex with opacity value', t => {
1212
const hex = '#ff0000'
1313
const opacity = 1
1414
const expected = 'rgba(255,0,0,1)'
15-
const actual = hex2rgb(hex,opacity)
16-
t.deepEqual(actual,expected)
15+
const actual = hex2rgb(hex, opacity)
16+
t.deepEqual(actual, expected)
1717
})
1818

19-
test('test short hex without opacity value', t=>{
19+
test('test short hex without opacity value', t => {
2020
const hex = '#f00'
2121
const expected = 'rgba(255,0,0)'
2222
const actual = hex2rgb(hex)

0 commit comments

Comments
 (0)