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 | Please 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 |
|
9 | Install with [npm](https://www.npmjs.com/):
|
10 |
|
11 | ```sh
|
12 | $ npm install --save is-descriptor
|
13 | ```
|
14 |
|
15 | ## Usage
|
16 |
|
17 | ```js
|
18 | const isDescriptor = require('is-descriptor');
|
19 |
|
20 | isDescriptor({ value: 'foo' })
|
21 | //=> true
|
22 | isDescriptor({ get: function() {}, set: function() {} })
|
23 | //=> true
|
24 | isDescriptor({ get: 'foo', set: function() {} })
|
25 | //=> false
|
26 | ```
|
27 |
|
28 | You 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
|
31 | const obj = {};
|
32 | obj.foo = null;
|
33 |
|
34 | Object.defineProperty(obj, 'bar', { value: 'xyz' });
|
35 | Reflect.defineProperty(obj, 'baz', { value: 'xyz' });
|
36 |
|
37 | isDescriptor(obj, 'foo'); //=> true
|
38 | isDescriptor(obj, 'bar'); //=> true
|
39 | isDescriptor(obj, 'baz'); //=> true
|
40 | ```
|
41 |
|
42 | ## Examples
|
43 |
|
44 | ### value type
|
45 |
|
46 | Returns `false` when not an object
|
47 |
|
48 | ```js
|
49 | isDescriptor('a'); //=> false
|
50 | isDescriptor(null); //=> false
|
51 | isDescriptor([]); //=> false
|
52 | ```
|
53 |
|
54 | ### data descriptor
|
55 |
|
56 | Returns `true` when the object has valid properties with valid values.
|
57 |
|
58 | ```js
|
59 | isDescriptor({ value: 'foo' }); //=> true
|
60 | isDescriptor({ value: function() {} }); //=> true
|
61 | ```
|
62 |
|
63 | Returns `false` when the object has invalid properties
|
64 |
|
65 | ```js
|
66 | isDescriptor({ value: 'foo', bar: 'baz' }); //=> false
|
67 | isDescriptor({ value: 'foo', bar: 'baz' }); //=> false
|
68 | isDescriptor({ value: 'foo', get: function() {} }); //=> false
|
69 | isDescriptor({ get: function() {}, value: function() {} }); //=> false
|
70 | ```
|
71 |
|
72 | `false` when a value is not the correct type
|
73 |
|
74 | ```js
|
75 | isDescriptor({ value: 'foo', enumerable: 'foo' }); //=> false
|
76 | isDescriptor({ value: 'foo', configurable: 'foo' }); //=> false
|
77 | isDescriptor({ 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
|
85 | isDescriptor({ get: function() {}, set: function() {} }); //=> true
|
86 | isDescriptor({ get: function() {} }); //=> true
|
87 | isDescriptor({ set: function() {} }); //=> true
|
88 | ```
|
89 |
|
90 | `false` when the object has invalid properties
|
91 |
|
92 | ```js
|
93 | isDescriptor({ get: function() {}, set: function() {}, bar: 'baz' }); //=> false
|
94 | isDescriptor({ get: function() {}, writable: true }); //=> false
|
95 | isDescriptor({ get: function() {}, value: true }); //=> false
|
96 | ```
|
97 |
|
98 | Returns `false` when an accessor is not a function
|
99 |
|
100 | ```js
|
101 | isDescriptor({ get: function() {}, set: 'baz' }); //=> false
|
102 | isDescriptor({ get: 'foo', set: function() {} }); //=> false
|
103 | isDescriptor({ get: 'foo', bar: 'baz' }); //=> false
|
104 | isDescriptor({ get: 'foo', set: 'baz' }); //=> false
|
105 | ```
|
106 |
|
107 | Returns `false` when a value is not the correct type
|
108 |
|
109 | ```js
|
110 | isDescriptor({ get: function() {}, set: function() {}, enumerable: 'foo' }); //=> false
|
111 | isDescriptor({ set: function() {}, configurable: 'foo' }); //=> false
|
112 | isDescriptor({ get: function() {}, configurable: 'foo' }); //=> false
|
113 | ```
|
114 |
|
115 | ## About
|
116 |
|
117 | <details>
|
118 | <summary><strong>Contributing</strong></summary>
|
119 |
|
120 | Pull 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 |
|
127 | 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:
|
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 |
|
140 | To 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 |
|
150 | You 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 |
|
176 | Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
|
177 | Released 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 |