mirror of
https://github.com/hanzoai/es5-ext.git
synced 2026-09-24 16:58:43 +00:00
feat: Object.isPlainFunction utility
This commit is contained in:
@@ -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"),
|
||||
|
||||
@@ -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
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user