1
|
# is-descriptor [![NPM version](https://img.shields.io/npm/v/is-descriptor.svg?style=flat)](https://www.npmjs.com/package/is-descriptor) [![NPM monthly downloads](https://img.shields.io/npm/dm/is-descriptor.svg?style=flat)](https://npmjs.org/package/is-descriptor) [![NPM total downloads](https://img.shields.io/npm/dt/is-descriptor.svg?style=flat)](https://npmjs.org/package/is-descriptor) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/is-descriptor.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/is-descriptor)
|
2
|
|
3
|
> Returns true if a value has the characteristics of a valid JavaScript descriptor. Works for data descriptors and accessor descriptors.
|
4
|
|
5
|
## Install
|
6
|
|
7
|
Install with [npm](https://www.npmjs.com/):
|
8
|
|
9
|
```sh
|
10
|
$ npm install --save is-descriptor
|
11
|
```
|
12
|
|
13
|
## Usage
|
14
|
|
15
|
```js
|
16
|
var isDescriptor = require('is-descriptor');
|
17
|
|
18
|
isDescriptor({value: 'foo'})
|
19
|
//=> true
|
20
|
isDescriptor({get: function(){}, set: function(){}})
|
21
|
//=> true
|
22
|
isDescriptor({get: 'foo', set: function(){}})
|
23
|
//=> false
|
24
|
```
|
25
|
|
26
|
You may also check for a descriptor by passing an object as the first argument and property name (`string`) as the second argument.
|
27
|
|
28
|
```js
|
29
|
var obj = {};
|
30
|
obj.foo = 'abc';
|
31
|
|
32
|
Object.defineProperty(obj, 'bar', {
|
33
|
value: 'xyz'
|
34
|
});
|
35
|
|
36
|
isDescriptor(obj, 'foo');
|
37
|
//=> true
|
38
|
isDescriptor(obj, 'bar');
|
39
|
//=> true
|
40
|
```
|
41
|
|
42
|
## Examples
|
43
|
|
44
|
### value type
|
45
|
|
46
|
`false` when not an object
|
47
|
|
48
|
```js
|
49
|
isDescriptor('a');
|
50
|
//=> false
|
51
|
isDescriptor(null);
|
52
|
//=> false
|
53
|
isDescriptor([]);
|
54
|
//=> false
|
55
|
```
|
56
|
|
57
|
### data descriptor
|
58
|
|
59
|
`true` when the object has valid properties with valid values.
|
60
|
|
61
|
```js
|
62
|
isDescriptor({value: 'foo'});
|
63
|
//=> true
|
64
|
isDescriptor({value: noop});
|
65
|
//=> true
|
66
|
```
|
67
|
|
68
|
`false` when the object has invalid properties
|
69
|
|
70
|
```js
|
71
|
isDescriptor({value: 'foo', bar: 'baz'});
|
72
|
//=> false
|
73
|
isDescriptor({value: 'foo', bar: 'baz'});
|
74
|
//=> false
|
75
|
isDescriptor({value: 'foo', get: noop});
|
76
|
//=> false
|
77
|
isDescriptor({get: noop, value: noop});
|
78
|
//=> false
|
79
|
```
|
80
|
|
81
|
`false` when a value is not the correct type
|
82
|
|
83
|
```js
|
84
|
isDescriptor({value: 'foo', enumerable: 'foo'});
|
85
|
//=> false
|
86
|
isDescriptor({value: 'foo', configurable: 'foo'});
|
87
|
//=> false
|
88
|
isDescriptor({value: 'foo', writable: 'foo'});
|
89
|
//=> false
|
90
|
```
|
91
|
|
92
|
### accessor descriptor
|
93
|
|
94
|
`true` when the object has valid properties with valid values.
|
95
|
|
96
|
```js
|
97
|
isDescriptor({get: noop, set: noop});
|
98
|
//=> true
|
99
|
isDescriptor({get: noop});
|
100
|
//=> true
|
101
|
isDescriptor({set: noop});
|
102
|
//=> true
|
103
|
```
|
104
|
|
105
|
`false` when the object has invalid properties
|
106
|
|
107
|
```js
|
108
|
isDescriptor({get: noop, set: noop, bar: 'baz'});
|
109
|
//=> false
|
110
|
isDescriptor({get: noop, writable: true});
|
111
|
//=> false
|
112
|
isDescriptor({get: noop, value: true});
|
113
|
//=> false
|
114
|
```
|
115
|
|
116
|
`false` when an accessor is not a function
|
117
|
|
118
|
```js
|
119
|
isDescriptor({get: noop, set: 'baz'});
|
120
|
//=> false
|
121
|
isDescriptor({get: 'foo', set: noop});
|
122
|
//=> false
|
123
|
isDescriptor({get: 'foo', bar: 'baz'});
|
124
|
//=> false
|
125
|
isDescriptor({get: 'foo', set: 'baz'});
|
126
|
//=> false
|
127
|
```
|
128
|
|
129
|
`false` when a value is not the correct type
|
130
|
|
131
|
```js
|
132
|
isDescriptor({get: noop, set: noop, enumerable: 'foo'});
|
133
|
//=> false
|
134
|
isDescriptor({set: noop, configurable: 'foo'});
|
135
|
//=> false
|
136
|
isDescriptor({get: noop, configurable: 'foo'});
|
137
|
//=> false
|
138
|
```
|
139
|
|
140
|
## About
|
141
|
|
142
|
### Related projects
|
143
|
|
144
|
* [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 "Returns true if a value has the characteristics of a valid JavaScript accessor descriptor.")
|
145
|
* [is-data-descriptor](https://www.npmjs.com/package/is-data-descriptor): Returns true if a value has the characteristics of a valid JavaScript data descriptor. | [homepage](https://github.com/jonschlinkert/is-data-descriptor "Returns true if a value has the characteristics of a valid JavaScript data descriptor.")
|
146
|
* [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://github.com/jonschlinkert/is-descriptor) | [homepage](https://github.com/jonschlinkert/is-descriptor "Returns true if a value has the characteristics of a valid JavaScript descriptor. Works for data descriptors and accessor descriptors.")
|
147
|
* [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 "Returns true if the value is an object and not an array or null.")
|
148
|
|
149
|
### Contributing
|
150
|
|
151
|
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
|
152
|
|
153
|
### Contributors
|
154
|
|
155
|
| **Commits** | **Contributor** |
|
156
|
| --- | --- |
|
157
|
| 24 | [jonschlinkert](https://github.com/jonschlinkert) |
|
158
|
| 1 | [doowb](https://github.com/doowb) |
|
159
|
| 1 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) |
|
160
|
|
161
|
### Building docs
|
162
|
|
163
|
_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
|
164
|
|
165
|
To generate the readme, run the following command:
|
166
|
|
167
|
```sh
|
168
|
$ npm install -g verbose/verb#dev verb-generate-readme && verb
|
169
|
```
|
170
|
|
171
|
### Running tests
|
172
|
|
173
|
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
|
174
|
|
175
|
```sh
|
176
|
$ npm install && npm test
|
177
|
```
|
178
|
|
179
|
### Author
|
180
|
|
181
|
**Jon Schlinkert**
|
182
|
|
183
|
* [github/jonschlinkert](https://github.com/jonschlinkert)
|
184
|
* [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
|
185
|
|
186
|
### License
|
187
|
|
188
|
Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
|
189
|
Released under the [MIT License](LICENSE).
|
190
|
|
191
|
***
|
192
|
|
193
|
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on July 22, 2017._
|