UNPKG

6.68 kBMarkdownView Raw
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
5Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
6
7## Install
8
9Install with [npm](https://www.npmjs.com/):
10
11```sh
12$ npm install --save is-descriptor
13```
14
15## Usage
16
17```js
18const isDescriptor = require('is-descriptor');
19
20isDescriptor({ value: 'foo' })
21//=> true
22isDescriptor({ get: function() {}, set: function() {} })
23//=> true
24isDescriptor({ get: 'foo', set: function() {} })
25//=> false
26```
27
28You may also check for a descriptor by passing an object as the first argument and property name (`string`) as the second argument.
29
30```js
31const obj = {};
32obj.foo = null;
33
34Object.defineProperty(obj, 'bar', { value: 'xyz' });
35Reflect.defineProperty(obj, 'baz', { value: 'xyz' });
36
37isDescriptor(obj, 'foo'); //=> true
38isDescriptor(obj, 'bar'); //=> true
39isDescriptor(obj, 'baz'); //=> true
40```
41
42## Examples
43
44### value type
45
46Returns `false` when not an object
47
48```js
49isDescriptor('a'); //=> false
50isDescriptor(null); //=> false
51isDescriptor([]); //=> false
52```
53
54### data descriptor
55
56Returns `true` when the object has valid properties with valid values.
57
58```js
59isDescriptor({ value: 'foo' }); //=> true
60isDescriptor({ value: function() {} }); //=> true
61```
62
63Returns `false` when the object has invalid properties
64
65```js
66isDescriptor({ value: 'foo', bar: 'baz' }); //=> false
67isDescriptor({ value: 'foo', bar: 'baz' }); //=> false
68isDescriptor({ value: 'foo', get: function() {} }); //=> false
69isDescriptor({ get: function() {}, value: function() {} }); //=> false
70```
71
72`false` when a value is not the correct type
73
74```js
75isDescriptor({ value: 'foo', enumerable: 'foo' }); //=> false
76isDescriptor({ value: 'foo', configurable: 'foo' }); //=> false
77isDescriptor({ value: 'foo', writable: 'foo' }); //=> false
78```
79
80### accessor descriptor
81
82`true` when the object has valid properties with valid values.
83
84```js
85isDescriptor({ get: function() {}, set: function() {} }); //=> true
86isDescriptor({ get: function() {} }); //=> true
87isDescriptor({ set: function() {} }); //=> true
88```
89
90`false` when the object has invalid properties
91
92```js
93isDescriptor({ get: function() {}, set: function() {}, bar: 'baz' }); //=> false
94isDescriptor({ get: function() {}, writable: true }); //=> false
95isDescriptor({ get: function() {}, value: true }); //=> false
96```
97
98Returns `false` when an accessor is not a function
99
100```js
101isDescriptor({ get: function() {}, set: 'baz' }); //=> false
102isDescriptor({ get: 'foo', set: function() {} }); //=> false
103isDescriptor({ get: 'foo', bar: 'baz' }); //=> false
104isDescriptor({ get: 'foo', set: 'baz' }); //=> false
105```
106
107Returns `false` when a value is not the correct type
108
109```js
110isDescriptor({ get: function() {}, set: function() {}, enumerable: 'foo' }); //=> false
111isDescriptor({ set: function() {}, configurable: 'foo' }); //=> false
112isDescriptor({ get: function() {}, configurable: 'foo' }); //=> false
113```
114
115## About
116
117<details>
118<summary><strong>Contributing</strong></summary>
119
120Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
121
122</details>
123
124<details>
125<summary><strong>Running Tests</strong></summary>
126
127Running 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:
128
129```sh
130$ npm install && npm test
131```
132
133</details>
134
135<details>
136<summary><strong>Building docs</strong></summary>
137
138_(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.)_
139
140To generate the readme, run the following command:
141
142```sh
143$ npm install -g verbose/verb#dev verb-generate-readme && verb
144```
145
146</details>
147
148### Related projects
149
150You might also be interested in these projects:
151
152* [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.")
153* [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.")
154* [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.")
155* [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.")
156
157### Contributors
158
159| **Commits** | **Contributor** |
160| --- | --- |
161| 33 | [jonschlinkert](https://github.com/jonschlinkert) |
162| 1 | [doowb](https://github.com/doowb) |
163| 1 | [realityking](https://github.com/realityking) |
164| 1 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) |
165
166### Author
167
168**Jon Schlinkert**
169
170* [GitHub Profile](https://github.com/jonschlinkert)
171* [Twitter Profile](https://twitter.com/jonschlinkert)
172* [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)
173
174### License
175
176Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
177Released under the [MIT License](LICENSE).
178
179***
180
181_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on December 13, 2018._
\No newline at end of file