[e29cc2e] | 1 | # is-data-descriptor [![NPM version](https://img.shields.io/npm/v/is-data-descriptor.svg)](https://www.npmjs.com/package/is-data-descriptor) [![Build Status](https://img.shields.io/travis/jonschlinkert/is-data-descriptor.svg)](https://travis-ci.org/jonschlinkert/is-data-descriptor)
|
---|
[6a3a178] | 2 |
|
---|
| 3 | > Returns true if a value has the characteristics of a valid JavaScript data descriptor.
|
---|
| 4 |
|
---|
| 5 | ## Install
|
---|
| 6 |
|
---|
| 7 | Install with [npm](https://www.npmjs.com/):
|
---|
| 8 |
|
---|
| 9 | ```sh
|
---|
[e29cc2e] | 10 | $ npm i is-data-descriptor --save
|
---|
[6a3a178] | 11 | ```
|
---|
| 12 |
|
---|
| 13 | ## Usage
|
---|
| 14 |
|
---|
| 15 | ```js
|
---|
| 16 | var isDataDesc = require('is-data-descriptor');
|
---|
| 17 | ```
|
---|
| 18 |
|
---|
| 19 | ## Examples
|
---|
| 20 |
|
---|
| 21 | `true` when the descriptor has valid properties with valid values.
|
---|
| 22 |
|
---|
| 23 | ```js
|
---|
| 24 | // `value` can be anything
|
---|
| 25 | isDataDesc({value: 'foo'})
|
---|
| 26 | isDataDesc({value: function() {}})
|
---|
| 27 | isDataDesc({value: true})
|
---|
| 28 | //=> true
|
---|
| 29 | ```
|
---|
| 30 |
|
---|
| 31 | `false` when not an object
|
---|
| 32 |
|
---|
| 33 | ```js
|
---|
| 34 | isDataDesc('a')
|
---|
| 35 | //=> false
|
---|
| 36 | isDataDesc(null)
|
---|
| 37 | //=> false
|
---|
| 38 | isDataDesc([])
|
---|
| 39 | //=> false
|
---|
| 40 | ```
|
---|
| 41 |
|
---|
| 42 | `false` when the object has invalid properties
|
---|
| 43 |
|
---|
| 44 | ```js
|
---|
| 45 | isDataDesc({value: 'foo', bar: 'baz'})
|
---|
| 46 | //=> false
|
---|
| 47 | isDataDesc({value: 'foo', bar: 'baz'})
|
---|
| 48 | //=> false
|
---|
| 49 | isDataDesc({value: 'foo', get: function(){}})
|
---|
| 50 | //=> false
|
---|
| 51 | isDataDesc({get: function(){}, value: 'foo'})
|
---|
| 52 | //=> false
|
---|
| 53 | ```
|
---|
| 54 |
|
---|
| 55 | `false` when a value is not the correct type
|
---|
| 56 |
|
---|
| 57 | ```js
|
---|
| 58 | isDataDesc({value: 'foo', enumerable: 'foo'})
|
---|
| 59 | //=> false
|
---|
| 60 | isDataDesc({value: 'foo', configurable: 'foo'})
|
---|
| 61 | //=> false
|
---|
| 62 | isDataDesc({value: 'foo', writable: 'foo'})
|
---|
| 63 | //=> false
|
---|
| 64 | ```
|
---|
| 65 |
|
---|
| 66 | ## Valid properties
|
---|
| 67 |
|
---|
| 68 | The only valid data descriptor properties are the following:
|
---|
| 69 |
|
---|
| 70 | * `configurable` (required)
|
---|
| 71 | * `enumerable` (required)
|
---|
| 72 | * `value` (optional)
|
---|
| 73 | * `writable` (optional)
|
---|
| 74 |
|
---|
| 75 | To be a valid data descriptor, either `value` or `writable` must be defined.
|
---|
| 76 |
|
---|
| 77 | **Invalid properties**
|
---|
| 78 |
|
---|
| 79 | A descriptor may have additional _invalid_ properties (an error will **not** be thrown).
|
---|
| 80 |
|
---|
| 81 | ```js
|
---|
| 82 | var foo = {};
|
---|
| 83 |
|
---|
| 84 | Object.defineProperty(foo, 'bar', {
|
---|
| 85 | enumerable: true,
|
---|
| 86 | whatever: 'blah', // invalid, but doesn't cause an error
|
---|
| 87 | get: function() {
|
---|
| 88 | return 'baz';
|
---|
| 89 | }
|
---|
| 90 | });
|
---|
| 91 |
|
---|
| 92 | console.log(foo.bar);
|
---|
| 93 | //=> 'baz'
|
---|
| 94 | ```
|
---|
| 95 |
|
---|
[e29cc2e] | 96 | ## Related projects
|
---|
[6a3a178] | 97 |
|
---|
[e29cc2e] | 98 | * [is-accessor-descriptor](https://www.npmjs.com/package/is-accessor-descriptor): Returns true if a value has the characteristics of a valid JavaScript accessor descriptor. | [homepage](https://github.com/jonschlinkert/is-accessor-descriptor)
|
---|
| 99 | * [is-descriptor](https://www.npmjs.com/package/is-descriptor): Returns true if a value has the characteristics of a valid JavaScript descriptor. Works for… [more](https://www.npmjs.com/package/is-descriptor) | [homepage](https://github.com/jonschlinkert/is-descriptor)
|
---|
| 100 | * [isobject](https://www.npmjs.com/package/isobject): Returns true if the value is an object and not an array or null. | [homepage](https://github.com/jonschlinkert/isobject)
|
---|
[6a3a178] | 101 |
|
---|
[e29cc2e] | 102 | ## Running tests
|
---|
[6a3a178] | 103 |
|
---|
[e29cc2e] | 104 | Install dev dependencies:
|
---|
[6a3a178] | 105 |
|
---|
| 106 | ```sh
|
---|
[e29cc2e] | 107 | $ npm i -d && npm test
|
---|
[6a3a178] | 108 | ```
|
---|
| 109 |
|
---|
[e29cc2e] | 110 | ## Contributing
|
---|
[6a3a178] | 111 |
|
---|
[e29cc2e] | 112 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/is-data-descriptor/issues/new).
|
---|
[6a3a178] | 113 |
|
---|
[e29cc2e] | 114 | ## Author
|
---|
[6a3a178] | 115 |
|
---|
| 116 | **Jon Schlinkert**
|
---|
| 117 |
|
---|
| 118 | * [github/jonschlinkert](https://github.com/jonschlinkert)
|
---|
[e29cc2e] | 119 | * [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
|
---|
[6a3a178] | 120 |
|
---|
[e29cc2e] | 121 | ## License
|
---|
[6a3a178] | 122 |
|
---|
[e29cc2e] | 123 | Copyright © 2015 [Jon Schlinkert](https://github.com/jonschlinkert)
|
---|
| 124 | Released under the MIT license.
|
---|
[6a3a178] | 125 |
|
---|
| 126 | ***
|
---|
| 127 |
|
---|
[e29cc2e] | 128 | _This file was generated by [verb](https://github.com/verbose/verb) on December 28, 2015._ |
---|