Big news!Introducing Socket AI - ChatGPT-Powered Threat Analysis.Learn more
Socket
Socket

is-descriptor

Package Overview
Dependencies
0
Maintainers
2
Versions
12
Issues
File Explorer

Advanced tools

is-descriptor

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

    3.1.0latest
    GitHub
    npm

Version published
Maintainers
2
Weekly downloads
48,012,594
increased by6.46%

Weekly downloads

Changelog

Source

v3.1.0 - 2023-05-01

Commits

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 fully completed data descriptors and accessor descriptors.

Usage

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'));

Examples

value type

Returns false when not an object

assert.ok(!isDescriptor('a')); assert.ok(!isDescriptor(null)); assert.ok(!isDescriptor([]));

data descriptor

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'}));

accessor descriptor

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:

  • 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 02 May 2023

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.

Install Socket
Socket
Socket SOC 2 Logo

Product

  • Package Issues
  • 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