feat: Object.isPlainFunction utility

This commit is contained in:
Mariusz Nowak
2017-10-09 17:14:40 +02:00
parent 230985e549
commit 031be0a22f
4 changed files with 66 additions and 11 deletions
+1
View File
@@ -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"),
+11
View File
@@ -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));
};
+12 -11
View File
@@ -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
View File
@@ -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");
}
};