function-feature
v1.1.2
Published
Get V8 function type flags (IsConstructor, IsAsyncFunction, IsGeneratorFunction). Also includes features like getting bind source, Proxy source, function renaming, etc.
Maintainers
Readme
function-feature 🚀
High-performance V8 native function feature detection for Node.js. Supports Node.js 16/18/20/22/24, auto-loads prebuilt binaries, no Nan/N-API dependency required.
Note: Only works in Node.js environments. And prebuilds are only for linux.
Recommended: set "type": "module" in your package.json to use this module with ES6 imports.
For more awesome packages, check out my homepage💛
📦 Installation
npm install function-feature✨ Import
import {
getFeatures,
getBound,
getOrigin,
getProxyConfig,
setName,
protoToString,
isClass,
} from 'function-feature';🧩 API Reference
getFeatures(fn: Function): FunctionFeaturesResult
Analyze a JavaScript function using V8 internals and return its feature flags:
isConstructor: Determines if argument is a function object with a[[Construct]]internal methodisCallable: Determines if argument is a callable function with a[[Call]]internal methodisAsyncFunction:trueif the function is an async functionisGeneratorFunction:trueif the function is a generator functionisProxy:trueif the function is a ProxyisBound:trueif the function has a bound target (created by Function.prototype.bind)isClass:trueif the function is a class (either user-defined or native)origin: The original function/object (unwrapped)
Example:
getFeatures(function test(a, b) {});
const result = {
isConstructor: false,
isCallable: true,
isAsyncFunction: false,
isGeneratorFunction: false,
isProxy: false,
isBound: false,
isClass: false,
origin: [Function: test]
}getBound(fn: Function): Function | undefined
Get the original function that satisfies fn = fn0.bind(thisArg, ...args). If not bound, returns itself.
Example:
function test() {}
const bound = test.bind(null);
getBound(bound); // returns testgetOrigin(o: Function | object): Function | object
Get the true origin function/object, tracing through bound and proxy wrappers. For objects, only proxy will be unwrapped.
Example:
const f0 = function () {};
const proxy = new Proxy(f0, {});
const bound = proxy.bind(null);
getOrigin(bound); // returns f0
getOrigin(proxy); // returns f0
getOrigin({}); // returns {}getProxyConfig(o: object | Function): { target: unknown, handler: unknown } | undefined
Get proxy details (target and handler) if the input is a Proxy object/function. Returns undefined if not a Proxy.
Example:
const target = () => {};
const handler = {};
const proxy = new Proxy(target, handler);
getProxyConfig(proxy); // { target: [Function], handler: {} }
getProxyConfig(target); // undefinedsetName(fn: Function, name: string | symbol): Function
Set the name of a function. Only works if the name property is configurable. Class/arrow/native functions cannot be changed.
- If
nameis a symbol, usesname.description:- If
descriptionis undefined, uses'' - If
descriptionis a string, uses'[description]'
- If
Example:
function foo() {}
setName(foo, 'bar');
console.log(foo.name); // 'bar'
setName(foo, Symbol('baz'));
console.log(foo.name); // '[baz]'protoToString(fn: Function): string
Equivalent to Function.prototype.toString but uses V8 internals to get the string representation. Safe and not affected by user overrides.
Example:
protoToString(function foo() {});isClass(fn: Function): boolean
Check if a function is a class (either user-defined or native constructor).
Rules:
- Must be a constructor
- Gets the original function (unwraps bound functions)
- Native constructor (Array, Object, etc.) will return
true - Checks function.toString() for class syntax patterns
Example:
isClass(Array); // true
isClass(class A {}); // true
isClass(function () {}); // false📄 License
MIT
