Socket
Socket
Sign inDemoInstall

is-data-descriptor

Package Overview
Dependencies
2
Maintainers
2
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    is-data-descriptor

Returns true if a value has the characteristics of a valid JavaScript data descriptor.


Version published
Weekly downloads
30M
decreased by-5.08%
Maintainers
2
Install size
59.6 kB
Created
Weekly downloads
 

Package description

What is is-data-descriptor?

The is-data-descriptor npm package is used to determine if an object is a valid data descriptor. Data descriptors are object keys that have attributes like value, writable, enumerable, and configurable. This package checks if a given object conforms to the specification of a data descriptor.

What are is-data-descriptor's main functionalities?

Check if an object is a data descriptor

This feature allows you to verify if a given object is a valid data descriptor by checking its properties and their respective values.

const isDataDescriptor = require('is-data-descriptor');

const descriptor = {value: 'value', writable: true, enumerable: true, configurable: true};
const result = isDataDescriptor(descriptor); // result is true

Other packages similar to is-data-descriptor

Changelog

Source

v1.0.1 - 2023-10-26

Commits

  • [eslint] actually use eslint 65fed07
  • [readme] clean up readme, remove verb 10ad663
  • [meta] clean up package.json 7f76a01
  • [meta] update .gitignore a2ca593
  • [Tests] switch to tape 70540e5
  • [Tests] migrate from travis to github actions eee138d
  • [Fix] properly return false for an accessor descriptor 2c213cd
  • [Performance] move data object to module level 37688a1
  • [Fix] allow any non-primitive; arrays and functions are objects too 197c77a
  • Only apps should have lockfiles 20aa6e5
  • [Robustness] switch to hasown aa48e2f
  • [Fix] properly guard for-in loop 014971e
  • [Robustness] use a null object just in case ab05aad

Readme

Source

is-data-descriptor Version Badge

github actions coverage License Downloads

npm badge

Returns true if a value has the characteristics of a valid JavaScript data descriptor.

Install

Install with npm:

$ npm install --save is-data-descriptor

Usage

var isDataDesc = require('is-data-descriptor');
var assert = require('assert');

Examples

true when the descriptor has valid properties with valid values.

// `value` can be anything
assert.equal(isDataDesc({ value: 'foo' }), true);
assert.equal(isDataDesc({ value: function () {} }), true);
assert.equal(isDataDesc({ value: true }), true);

false when not an object

assert.equal(isDataDesc('a'), false);
assert.equal(isDataDesc(null), false);

false when the object has invalid properties

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

false when a value is not the correct type

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

Valid properties

The only valid data descriptor properties are the following:

  • configurable (required)
  • enumerable (required)
  • value (optional)
  • writable (optional)

To be a valid data descriptor, either value or writable must be defined.

Invalid properties

A descriptor may have additional invalid properties (an error will not be thrown).

var foo = {};

Object.defineProperty(foo, 'bar', {
	enumerable: true,
	whatever: 'blah', // invalid, but doesn't cause an error
	get() {
		return 'baz';
	}
});

assert.equal(foo.bar, 'baz');

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-descriptor: Returns true if a value has the characteristics of a valid JavaScript descriptor. Works for… more

Keywords

FAQs

Last updated on 26 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