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

Commit 0f7621e

Browse files
mitchellporterKent C. Dodds
authored and
Kent C. Dodds
committed
feat: adds an 'isFunction' function (#102)
* feat: adds an 'isFunction' function Closes #100 * refactor: removes package-lock.json and adds to .gitignore removes package-lock.json and adds to .gitignore
1 parent 10f1e42 commit 0f7621e

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ node_modules
44
.nyc_output/
55
coverage/
66
npm-debug.log
7+
package-lock.json

src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import swapCase from './swap-case'
3434
import endsWith from './endsWith'
3535
import round from './round'
3636
import checkPalindrome from './checkPalindrome'
37+
import isFunction from './is-function'
3738

3839
export {
3940
isEven,
@@ -72,4 +73,5 @@ export {
7273
endsWith,
7374
round,
7475
checkPalindrome,
76+
isFunction,
7577
}

src/is-function.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default isFunction
2+
3+
/**
4+
* Original source: https://stackoverflow.com/a/7356528/3344977
5+
*
6+
* This method checks whether an object passed as an
7+
* argument is a javascript Function or not
8+
* @param {Object} functionToCheck - the object to check
9+
* @return {Boolean} - true if functionToCheck is a function else false
10+
*/
11+
function isFunction(functionToCheck) {
12+
const getType = {}
13+
return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]'
14+
}

test/is-function.test.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import test from 'ava'
2+
import {isFunction} from '../src'
3+
4+
test('check a function', t => {
5+
const func = () => {}
6+
const expected = true
7+
const actual = isFunction(func)
8+
t.deepEqual(actual, expected)
9+
})
10+
11+
test('check an object', t => {
12+
const object = {}
13+
const expected = false
14+
const actual = isFunction(object)
15+
t.deepEqual(actual, expected)
16+
})
17+
18+
test('check a string', t => {
19+
const string = 'string'
20+
const expected = false
21+
const actual = isFunction(string)
22+
t.deepEqual(actual, expected)
23+
})
24+
25+
test('check a boolean', t=> {
26+
const boolean = true
27+
const expected = false
28+
const actual = isFunction(boolean)
29+
t.deepEqual(actual, expected)
30+
})
31+
32+
test('check a number', t => {
33+
const number = 1
34+
const expected = false
35+
const actual = isFunction(number)
36+
t.deepEqual(actual, expected)
37+
})

0 commit comments

Comments
 (0)