is-data-descriptor
Version:
Returns true if a value has the characteristics of a valid JavaScript data descriptor.
92 lines (67 loc) • 3.55 kB
Markdown
true if a value has the characteristics of a valid JavaScript data descriptor.
`true` when the descriptor has valid properties with valid values.
`false` when not an object or when the object has invalid properties.
```js
var isDataDesc = require('is-data-descriptor');
var assert = require('assert');
assert.equal(true, isDataDesc({ value: 'foo' }));
assert.equal(true, isDataDesc({ value: function () {} }));
assert.equal(true, isDataDesc({ value: true }));
assert.equal(false, isDataDesc('a'));
assert.equal(false, isDataDesc(null));
assert.equal(false, isDataDesc([]));
assert.equal(false, isDataDesc({ value: 'foo', bar: 'baz' }));
assert.equal(false, isDataDesc({ value: 'foo', bar: 'baz' }));
assert.equal(false, isDataDesc({ value: 'foo', get: function () {} }));
assert.equal(false, isDataDesc({ get: function () {}, value: 'foo' }) );
assert.equal(false, isDataDesc({ value: 'foo', enumerable: 'foo' }));
assert.equal(false, isDataDesc({ value: 'foo', configurable: 'foo' }));
assert.equal(false, isDataDesc({ value: 'foo', writable: 'foo' }));
```
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).
```js
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');
```
* [is-accessor-descriptor](https://npmjs.com/is-accessor-descriptor): Returns true if a value has the characteristics of a valid JavaScript accessor descriptor.
* [is-descriptor](https://npmjs.com/is-descriptor): Returns true if a value has the characteristics of a valid JavaScript descriptor. Works for… [more](https://npmjs.com/is-descriptor)
Simply clone the repo, `npm install`, and run `npm test`
[ ]: https://npmjs.org/package/is-data-descriptor
[ ]: https://versionbadg.es/inspect-js/is-data-descriptor.svg
[ ]: https://david-dm.org/inspect-js/is-data-descriptor.svg
[ ]: https://david-dm.org/inspect-js/is-data-descriptor
[ ]: https://david-dm.org/inspect-js/is-data-descriptor/dev-status.svg
[ ]: https://david-dm.org/inspect-js/is-data-descriptor#info=devDependencies
[ ]: https://nodei.co/npm/is-data-descriptor.png?downloads=true&stars=true
[ ]: https://img.shields.io/npm/l/is-data-descriptor.svg
[ ]: LICENSE
[ ]: https://img.shields.io/npm/dm/is-data-descriptor.svg
[ ]: https://npm-stat.com/charts.html?package=is-data-descriptor
[ ]: https://codecov.io/gh/inspect-js/is-data-descriptor/branch/main/graphs/badge.svg
[ ]: https://app.codecov.io/gh/inspect-js/is-data-descriptor/
[ ]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/inspect-js/is-data-descriptor
[ ]: https://github.com/inspect-js/is-data-descriptor/actions
[![github actions][actions-image]][actions-url]
[![coverage][codecov-image]][codecov-url]
[![License][license-image]][license-url]
[![Downloads][downloads-image]][downloads-url]
[![npm badge][npm-badge-png]][package-url]
> Returns