Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
is-accessor-descriptor
Advanced tools
Returns true if a value has the characteristics of a valid JavaScript accessor descriptor.
The is-accessor-descriptor npm package is used to check if an object property descriptor defines an accessor descriptor. An accessor descriptor is one that includes getter and/or setter functions, as opposed to a data descriptor which contains a value and is writable.
Check if a descriptor is an accessor descriptor
This feature allows you to verify if a given property descriptor from an object is an accessor descriptor. It returns true if the descriptor has a get or set key, and false otherwise.
{"isAccessorDescriptor": require('is-accessor-descriptor');
var descriptor = Object.getOwnPropertyDescriptor({get foo() {}}, 'foo');
console.log(isAccessorDescriptor(descriptor)); //=> true
}
Check if an object is an accessor descriptor
This feature allows you to check if a plain object mimics the structure of an accessor descriptor. It is useful for validation purposes when you have an object that should represent a descriptor and you want to ensure it is an accessor descriptor.
{"isAccessorDescriptor": require('is-accessor-descriptor');
console.log(isAccessorDescriptor({get: function() {}})); //=> true
console.log(isAccessorDescriptor({set: function() {}})); //=> true
console.log(isAccessorDescriptor({value: 123})); //=> false
}
This package is similar to is-accessor-descriptor but checks for data descriptors instead. A data descriptor is an object with a value property and optionally writable, enumerable, and configurable properties. It is the counterpart to is-accessor-descriptor, which checks for accessor descriptors.
is-descriptor is a more general package that checks if an object is a valid property descriptor, which can be either a data descriptor or an accessor descriptor. It is a superset of is-accessor-descriptor and is-data-descriptor functionalities.
While not specifically for checking descriptors, is-plain-object can be used to determine if an object is plain (i.e., created by the Object constructor or one with a null prototype). This can be a preliminary check before further validation as a descriptor.
Returns true if a value has the characteristics of a valid JavaScript accessor descriptor.
const isAccessorDescriptor = require('is-accessor-descriptor');
const assert = require('assert');
const obj = {
get foo() {},
bar: { get: function() {} }
};
assert.equal(true, isAccessorDescriptor(obj, 'foo'));
assert.equal(false, isAccessorDescriptor(obj, 'bar'));
// or, if you already have the descriptor you can pass it directly
const foo = Object.getOwnPropertyDescriptor(obj, 'foo');
assert.equal(true, isAccessorDescriptor(foo));
const bar = Object.getOwnPropertyDescriptor(obj, 'bar');
assert.equal(false, isAccessorDescriptor(bar));
You might also be interested in these projects:
Simply clone the repo, npm install
, and run npm test
FAQs
Returns true if a value has the characteristics of a valid JavaScript accessor descriptor.
The npm package is-accessor-descriptor receives a total of 14,794,436 weekly downloads. As such, is-accessor-descriptor popularity was classified as popular.
We found that is-accessor-descriptor demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.