Socket
Socket
Sign inDemoInstall

is-descriptor

Package Overview
Dependencies
4
Maintainers
2
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    is-descriptor

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


Version published
Weekly downloads
40M
decreased by-9.94%
Maintainers
2
Install size
102 kB
Created
Weekly downloads
 

Package description

What is is-descriptor?

The is-descriptor npm package is used for checking if an object is a valid descriptor. Descriptors are objects that define the behavior of properties on JavaScript objects, such as whether they are writable, enumerable, or configurable, and they can also specify getter and setter functions. This package is useful for validation in libraries that manipulate object properties and descriptors, ensuring that operations like defining new properties or modifying existing ones are performed with valid descriptor objects.

What are is-descriptor's main functionalities?

Checking property descriptors

This feature allows you to check if a given object is a valid property descriptor. The code sample demonstrates checking various objects to see if they qualify as descriptors based on their properties.

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

console.log(isDescriptor({enumerable: false, configurable: true})); // true
console.log(isDescriptor({value: 'hello'})); // true
console.log(isDescriptor({get: function() {}})); // true
console.log(isDescriptor({set: undefined})); // false

Other packages similar to is-descriptor

Changelog

Source

v0.1.7 - 2023-10-26

Merged

  • Update dependencies #5

Commits

  • [eslint] actually use eslint 8bcf028
  • [meta] update package.json, gitignore from main 544cdfe
  • [readme] update readme from main 1130f79
  • [Tests] switch to tape 3f8f094
  • [Docs] remove verb 92ee1bf
  • [Tests] migrate from travis to github actions 8da3a3c
  • run update, lint 754cc73
  • [Fix] a descriptor with set and not get is still an accessor descriptor 269fb53
  • [patch] switch from files to exports 41b2d61
  • [Fix] allow any non-primitive; arrays and functions are objects too 9fd1ac8
  • update deps 2b58af6
  • [Deps] update is-accessor-descriptor, is-data-descriptor f4dbc73
  • v0.x line: v1 and v0 are the same, so, branch v0 from 1.x 91be723
  • [Tests] make a test dir 9eaa17c

Readme

Source

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

Keywords

FAQs

Last updated on 27 Oct 2023

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc