live-server/node_modules/is-descriptor
2023-12-05 17:03:01 +00:00
..
.github test 2023-12-05 17:03:01 +00:00
test test 2023-12-05 17:03:01 +00:00
.editorconfig test 2023-12-05 17:03:01 +00:00
.eslintrc test 2023-12-05 17:03:01 +00:00
.gitattributes test 2023-12-05 17:03:01 +00:00
.nycrc test 2023-12-05 17:03:01 +00:00
CHANGELOG.md test 2023-12-05 17:03:01 +00:00
index.js test 2023-12-05 17:03:01 +00:00
LICENSE test 2023-12-05 17:03:01 +00:00
package.json test 2023-12-05 17:03:01 +00:00
README.md test 2023-12-05 17:03:01 +00:00

is-descriptor Version Badge

github actions coverage License Downloads

npm badge

Returns true if a value has the characteristics of a valid JavaScript descriptor. Works for data descriptors and accessor descriptors.

Usage

const isDescriptor = require('is-descriptor');
const assert = require('assert');

assert.equal(isDescriptor({ value: 'foo' }), true);
assert.equal(isDescriptor({ get() {}, set() {} }), true);
assert.equal(isDescriptor({ get: 'foo', set() {} }), false);

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 = { foo: 'abc' };

Object.defineProperty(obj, 'bar', {
  value: 'xyz'
});

assert.equal(isDescriptor(obj, 'foo'), true);
assert.equal(isDescriptor(obj, 'bar'), true);

Examples

value type

false when not an object

assert.equal(isDescriptor('a'), false);
assert.equal(isDescriptor(null), false);
assert.equal(isDescriptor([]), false);

data descriptor

true when the object has valid properties with valid values.

assert.equal(isDescriptor({ value: 'foo' }), true);
assert.equal(isDescriptor({ value() {} }), true);

false when the object has invalid properties

assert.equal(isDescriptor({ value: 'foo', enumerable: 'baz' }), false);
assert.equal(isDescriptor({ value: 'foo', configurable: 'baz' }), false);
assert.equal(isDescriptor({ value: 'foo', get() {} }), false);
assert.equal(isDescriptor({ get() {}, value() {} }), false);

false when a value is not the correct type

assert.equal(isDescriptor({ value: 'foo', enumerable: 'foo' }), false);
assert.equal(isDescriptor({ value: 'foo', configurable: 'foo' }), false);
assert.equal(isDescriptor({ value: 'foo', writable: 'foo' }), false);

accessor descriptor

true when the object has valid properties with valid values.

assert.equal(isDescriptor({ get() {}, set() {} }), true);
assert.equal(isDescriptor({ get() {} }), true);
assert.equal(isDescriptor({ set() {} }), true);

false when the object has invalid properties

assert.equal(isDescriptor({ get() {}, set() {}, enumerable: 'baz' }), false);
assert.equal(isDescriptor({ get() {}, writable: true }), false);
assert.equal(isDescriptor({ get() {}, value: true }), false);

false when an accessor is not a function

assert.equal(isDescriptor({ get() {}, set: 'baz' }), false);
assert.equal(isDescriptor({ get: 'foo', set() {} }), false);
assert.equal(isDescriptor({ get: 'foo', bar: 'baz' }), false);
assert.equal(isDescriptor({ get: 'foo', set: 'baz' }), false);

false when a value is not the correct type

assert.equal(isDescriptor({ get() {}, set() {}, enumerable: 'foo' }), false);
assert.equal(isDescriptor({ set() {}, configurable: 'foo' }), false);
assert.equal(isDescriptor({ get() {}, configurable: 'foo' }), false);

You might also be interested in these projects:

  • is-accessor-descriptor: Returns true if a value has the characteristics of a valid JavaScript accessor descriptor.
  • is-data-descriptor: Returns true if a value has the characteristics of a valid JavaScript data descriptor.
  • is-object: Returns true if the value is an object and not an array or null.

Tests

Simply clone the repo, npm install, and run npm test