Skip to content

Commit

Permalink
Modified the typeOf method to return case sensitive function and clas…
Browse files Browse the repository at this point in the history
…s names (more accurate data types).
  • Loading branch information
coreybutler committed Sep 23, 2020
1 parent 1f25acd commit 838c8f8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/operator.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,16 @@ function typeOf (el) {

if (value === 'function' || typeof el === 'function') {
if (!el.name) {
const name = coalesceb(el.toString().replace(/\n/gi, '').replace(/^function\s|\(.*$/mgi, '').toLowerCase(), 'function')
const name = coalesceb(el.toString().replace(/\n/gi, '').replace(/^function\s|\(.*$/mgi, ''), 'function')

if (name.indexOf(' ') >= 0) {
return 'function'
}

return name.toLowerCase()
return name
}

return coalesceb(el.name, 'function').toLowerCase()
return coalesceb(el.name, 'function')
}

return value.toLowerCase()
Expand Down
6 changes: 3 additions & 3 deletions tests/05-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ test('typeOf', function (t) {
t.ok(typeOf(true) === 'boolean', 'typeof recognizes booleans.')
t.ok(typeOf(/\s{1,10}/gi) === 'regexp', 'typeof recognizes regular expressions.')
t.ok(typeOf({}) === 'object', 'typeof recognizes objects.')
t.ok(typeOf(EmptyFn) === 'emptyfn', 'typeof recognizes unique function names as types.')
t.ok(typeOf(EmptyFn) === 'EmptyFn', 'typeof recognizes unique function names as types.')
t.ok(typeOf([]) === 'array', 'typeof recognizes arrays.')
t.ok(typeOf(null) === 'null', 'typeof recognizes null.')
t.ok(typeOf() === 'undefined', 'typeof recognizes undefined.')

function myFn () { }
t.ok(typeOf(myFn) === 'myfn', 'typeof recognizes custom function names.')
t.ok(typeOf(myFn) === 'myFn', 'typeof recognizes custom function names.')

class myClass {
constructor () {
console.log('Nothing')
}
}
t.ok(typeOf(myClass) === 'myclass', 'typeof recognizes classes.')
t.ok(typeOf(myClass) === 'myClass', 'typeof recognizes classes.')

t.ok(typeOf(() => { console.log('nothing') }) === 'function', 'typeof recognizes fat arrow methods as functions.')

Expand Down

0 comments on commit 838c8f8

Please sign in to comment.