You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
5.7 KiB

4 years ago
  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. > Returns true if a value has the characteristics of a valid JavaScript descriptor. Works for data descriptors and accessor descriptors.
  3. ## Install
  4. Install with [npm](https://www.npmjs.com/):
  5. ```sh
  6. $ npm install --save is-descriptor
  7. ```
  8. ## Usage
  9. ```js
  10. var isDescriptor = require('is-descriptor');
  11. isDescriptor({value: 'foo'})
  12. //=> true
  13. isDescriptor({get: function(){}, set: function(){}})
  14. //=> true
  15. isDescriptor({get: 'foo', set: function(){}})
  16. //=> false
  17. ```
  18. You may also check for a descriptor by passing an object as the first argument and property name (`string`) as the second argument.
  19. ```js
  20. var obj = {};
  21. obj.foo = 'abc';
  22. Object.defineProperty(obj, 'bar', {
  23. value: 'xyz'
  24. });
  25. isDescriptor(obj, 'foo');
  26. //=> true
  27. isDescriptor(obj, 'bar');
  28. //=> true
  29. ```
  30. ## Examples
  31. ### value type
  32. `false` when not an object
  33. ```js
  34. isDescriptor('a');
  35. //=> false
  36. isDescriptor(null);
  37. //=> false
  38. isDescriptor([]);
  39. //=> false
  40. ```
  41. ### data descriptor
  42. `true` when the object has valid properties with valid values.
  43. ```js
  44. isDescriptor({value: 'foo'});
  45. //=> true
  46. isDescriptor({value: noop});
  47. //=> true
  48. ```
  49. `false` when the object has invalid properties
  50. ```js
  51. isDescriptor({value: 'foo', bar: 'baz'});
  52. //=> false
  53. isDescriptor({value: 'foo', bar: 'baz'});
  54. //=> false
  55. isDescriptor({value: 'foo', get: noop});
  56. //=> false
  57. isDescriptor({get: noop, value: noop});
  58. //=> false
  59. ```
  60. `false` when a value is not the correct type
  61. ```js
  62. isDescriptor({value: 'foo', enumerable: 'foo'});
  63. //=> false
  64. isDescriptor({value: 'foo', configurable: 'foo'});
  65. //=> false
  66. isDescriptor({value: 'foo', writable: 'foo'});
  67. //=> false
  68. ```
  69. ### accessor descriptor
  70. `true` when the object has valid properties with valid values.
  71. ```js
  72. isDescriptor({get: noop, set: noop});
  73. //=> true
  74. isDescriptor({get: noop});
  75. //=> true
  76. isDescriptor({set: noop});
  77. //=> true
  78. ```
  79. `false` when the object has invalid properties
  80. ```js
  81. isDescriptor({get: noop, set: noop, bar: 'baz'});
  82. //=> false
  83. isDescriptor({get: noop, writable: true});
  84. //=> false
  85. isDescriptor({get: noop, value: true});
  86. //=> false
  87. ```
  88. `false` when an accessor is not a function
  89. ```js
  90. isDescriptor({get: noop, set: 'baz'});
  91. //=> false
  92. isDescriptor({get: 'foo', set: noop});
  93. //=> false
  94. isDescriptor({get: 'foo', bar: 'baz'});
  95. //=> false
  96. isDescriptor({get: 'foo', set: 'baz'});
  97. //=> false
  98. ```
  99. `false` when a value is not the correct type
  100. ```js
  101. isDescriptor({get: noop, set: noop, enumerable: 'foo'});
  102. //=> false
  103. isDescriptor({set: noop, configurable: 'foo'});
  104. //=> false
  105. isDescriptor({get: noop, configurable: 'foo'});
  106. //=> false
  107. ```
  108. ## About
  109. ### Related projects
  110. * [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.")
  111. * [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.")
  112. * [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.")
  113. * [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.")
  114. ### Contributing
  115. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  116. ### Contributors
  117. | **Commits** | **Contributor** |
  118. | --- | --- |
  119. | 24 | [jonschlinkert](https://github.com/jonschlinkert) |
  120. | 1 | [doowb](https://github.com/doowb) |
  121. | 1 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) |
  122. ### Building docs
  123. _(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.)_
  124. To generate the readme, run the following command:
  125. ```sh
  126. $ npm install -g verbose/verb#dev verb-generate-readme && verb
  127. ```
  128. ### Running tests
  129. 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:
  130. ```sh
  131. $ npm install && npm test
  132. ```
  133. ### Author
  134. **Jon Schlinkert**
  135. * [github/jonschlinkert](https://github.com/jonschlinkert)
  136. * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
  137. ### License
  138. Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
  139. Released under the [MIT License](LICENSE).
  140. ***
  141. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on July 22, 2017._