JavaScript Accessor Descriptor Checker

3.0.5 · active · verified Sun Apr 19

The `is-accessor-descriptor` package provides a focused utility function to determine if a given JavaScript value, or a property on an object, represents a valid accessor property descriptor. It specifically checks for the presence of `get` and/or `set` properties and ensures they are functions, while rejecting `value` or `writable` properties which characterize data descriptors. Currently at version 3.0.5, this package is part of a family of descriptor-checking utilities under the `inspect-js` scope, emphasizing foundational JavaScript introspection. Its release cadence is stable and infrequent, reflecting its role as a fundamental helper. It differentiates itself by its precise focus, allowing developers to isolate and validate accessor descriptors without conflating them with other descriptor types, making it a reliable tool for metaprogramming and object property manipulation.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `is-accessor-descriptor` to verify if an object's property (by passing the object and key) or a pre-obtained descriptor object is a valid JavaScript accessor descriptor.

const isAccessorDescriptor = require('is-accessor-descriptor');
const assert = require('assert');

const obj = {
	// An accessor property
	get foo() { return 'bar'; },
	// A data property containing an object with a 'get' key, not an accessor descriptor
	bar: { get: function() { return 'baz'; } }
};

// Check a property by its descriptor on an object
assert.equal(true, isAccessorDescriptor(obj, 'foo'), 'obj.foo should be an accessor descriptor');
assert.equal(false, isAccessorDescriptor(obj, 'bar'), 'obj.bar should NOT be an accessor descriptor');

// Alternatively, if you already have the descriptor object
const fooDescriptor = Object.getOwnPropertyDescriptor(obj, 'foo');
assert.equal(true, isAccessorDescriptor(fooDescriptor), 'fooDescriptor should be an accessor descriptor');

const barDescriptor = Object.getOwnPropertyDescriptor(obj, 'bar');
assert.equal(false, isAccessorDescriptor(barDescriptor), 'barDescriptor should NOT be an accessor descriptor');

console.log('All assertions passed: is-accessor-descriptor works as expected!');

view raw JSON →