is-descriptor
Advanced tools
Weekly downloads
Changelog
v3.1.0 - 2023-05-01
1f4e8cd
safe-publish-latest
, npmignore
, auto-changelog
, evalmd
, aud
5993285
8807164
0bc26a3
1604d7f
7893404
1dcc45e
d1edefe
files
field to npmignore; add exports
c64d3d3
Readme
Returns true if a value has the characteristics of a valid JavaScript descriptor. Works for fully completed data descriptors and accessor descriptors.
const isDescriptor = require('is-descriptor');
const assert = require('assert');
const defaults = { configurable: false, enumerable: false };
const dataDefaults = { ...defaults, writable: false};
assert.ok(isDescriptor({ ...dataDefaults, value: 'foo' }));
assert.ok(isDescriptor({ ...defaults, get() {}, set() {} }));
assert.ok(!isDescriptor({ ...defaults, get: 'foo', set() {} }));
You may also check for a descriptor by passing an object as the first argument and property name (string
) as the second argument.
const obj = {};
obj.foo = null;
Object.defineProperty(obj, 'bar', { value: 'xyz' });
Reflect.defineProperty(obj, 'baz', { value: 'xyz' });
assert.ok(isDescriptor(obj, 'foo'));
assert.ok(isDescriptor(obj, 'bar'));
assert.ok(isDescriptor(obj, 'baz'));
Returns false
when not an object
assert.ok(!isDescriptor('a'));
assert.ok(!isDescriptor(null));
assert.ok(!isDescriptor([]));
Returns true
when the object has valid properties with valid values.
assert.ok(isDescriptor({ ...dataDefaults, value: 'foo' }));
assert.ok(isDescriptor({ ...dataDefaults, value() {} }));
Returns false
when the object has invalid properties
assert.ok(!isDescriptor({ ...dataDefaults, value: 'foo', bar: 'baz' }));
assert.ok(!isDescriptor({ ...dataDefaults, value: 'foo', bar: 'baz' }));
assert.ok(!isDescriptor({ ...dataDefaults, value: 'foo', get() {} }));
assert.ok(!isDescriptor({ ...dataDefaults, get() {}, value() {} }));
false
when a value is not the correct type
assert.ok(!isDescriptor({ ...dataDefaults, value: 'foo', enumerable: 'foo'}));
assert.ok(!isDescriptor({ ...dataDefaults, value: 'foo', configurable: 'foo'}));
assert.ok(!isDescriptor({ ...dataDefaults, value: 'foo', writable: 'foo'}));
true
when the object has valid properties with valid values.
assert.ok(isDescriptor({ ...defaults, get() {}, set() {} }));
assert.ok(isDescriptor({ ...defaults, get() {} }));
assert.ok(isDescriptor({ ...defaults, set() {} }));
false
when the object has invalid properties
assert.ok(!isDescriptor({ ...defaults, get() {}, set() {}, bar: 'baz' }));
assert.ok(!isDescriptor({ ...defaults, get() {}, writable: true }));
assert.ok(!isDescriptor({ ...defaults, get() {}, value: true }));
Returns false
when an accessor is not a function
assert.ok(!isDescriptor({ ...defaults, get() {}, set: 'baz' }));
assert.ok(!isDescriptor({ ...defaults, get: 'foo', set() {} }));
assert.ok(!isDescriptor({ ...defaults, get: 'foo', bar: 'baz' }));
assert.ok(!isDescriptor({ ...defaults, get: 'foo', set: 'baz' }));
Returns false
when a value is not the correct type
assert.ok(!isDescriptor({ ...defaults, get() {}, set() {}, enumerable: 'foo' }));
assert.ok(!isDescriptor({ ...defaults, set() {}, configurable: 'foo' }));
assert.ok(!isDescriptor({ ...defaults, get() {}, configurable: 'foo' }));
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 descriptor. Works for data descriptors and accessor descriptors.
The npm package is-descriptor receives a total of 40,083,411 weekly downloads. As such, is-descriptor popularity was classified as popular.
We found that is-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 installs a Github app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.