Skip to content

Commit

Permalink
feat: Object.isPlainFunction utility
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Oct 9, 2017
1 parent 230985e commit 031be0a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 11 deletions.
1 change: 1 addition & 0 deletions object/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module.exports = {
isFiniteNumber: require("./is-finite-number"),
isNumberValue: require("./is-number-value"),
isObject: require("./is-object"),
isPlainFunction: require("./is-plain-function"),
isPlainObject: require("./is-plain-object"),
isPromise: require("./is-promise"),
isValue: require("./is-value"),
Expand Down
11 changes: 11 additions & 0 deletions object/is-plain-function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict";

var isClassStr = RegExp.prototype.test.bind(/^\s*class[\s{/}]/)
, fnToString = Function.prototype.toString;

module.exports = function (fn) {
if (typeof fn !== "function") return false;
if (typeof fn.call !== "function") return false;
if (typeof fn.apply !== "function") return false;
return !isClassStr(fnToString.call(fn));
};
23 changes: 12 additions & 11 deletions test/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"globals": {
"Symbol": true
},
"rules": {
"consistent-this": "off",
"id-length": "off",
"no-empty-function": "off",
"no-new-wrappers": "off",
"no-prototype-builtins": "off",
"no-shadow": "off"
}
"globals": {
"Symbol": true
},
"rules": {
"consistent-this": "off",
"id-length": "off",
"no-empty-function": "off",
"no-eval": "off",
"no-new-wrappers": "off",
"no-prototype-builtins": "off",
"no-shadow": "off"
}
}
42 changes: 42 additions & 0 deletions test/object/is-plain-function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"use strict";

var setPrototypeOf = require("../../object/set-prototype-of");

module.exports = function (t, a) {
a(t(function () {}), true, "Function");
a(t({}), false, "Object");
a(t(), false, "Undefined");
a(t(null), false, "Null");
if (setPrototypeOf) {
a(
t(Object.setPrototypeOf(function () {}, Object.prototype)),
false,
"Function with non-function prototype"
);
}
var arrowfn;
try {
arrowfn = eval("(() => {})");
} catch (e) {}
if (arrowfn) {
a(t(arrowfn), true, "Arrow function");
}

var classFn;
try {
classFn = eval("(class {})");
} catch (e) {}
if (classFn) {
a(t(classFn), false, "Class");
}

var commentedClassFn;
try {
// Follows issue reported to ljhard/is-callable project:
// https://github.com/ljharb/is-callable/issues/4
commentedClassFn = eval("(class/*kkk*/\n//blah\n Bar\n//blah\n {})");
} catch (e) {}
if (commentedClassFn) {
a(t(commentedClassFn, false, "Class"), false, "Class with comments");
}
};

0 comments on commit 031be0a

Please sign in to comment.