ec77c2974c6e30a9e504f78d64e8f2185b47a3f36158a553abbb49d5c4cf229ba149b87b872d3a1c04da29b8ab22077bcde91dd2ed7d35fe278ab2fed4e1dc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # is-descriptor <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
  2. [![github actions][actions-image]][actions-url]
  3. [![coverage][codecov-image]][codecov-url]
  4. [![License][license-image]][license-url]
  5. [![Downloads][downloads-image]][downloads-url]
  6. [![npm badge][npm-badge-png]][package-url]
  7. > Returns true if a value has the characteristics of a valid JavaScript descriptor. Works for data descriptors and accessor descriptors.
  8. ## Usage
  9. ```js
  10. const isDescriptor = require('is-descriptor');
  11. const assert = require('assert');
  12. assert.equal(isDescriptor({ value: 'foo' }), true);
  13. assert.equal(isDescriptor({ get() {}, set() {} }), true);
  14. assert.equal(isDescriptor({ get: 'foo', set() {} }), false);
  15. ```
  16. You may also check for a descriptor by passing an object as the first argument and property name (`string`) as the second argument.
  17. ```js
  18. const obj = { foo: 'abc' };
  19. Object.defineProperty(obj, 'bar', {
  20. value: 'xyz'
  21. });
  22. assert.equal(isDescriptor(obj, 'foo'), true);
  23. assert.equal(isDescriptor(obj, 'bar'), true);
  24. ```
  25. ## Examples
  26. ### value type
  27. `false` when not an object
  28. ```js
  29. assert.equal(isDescriptor('a'), false);
  30. assert.equal(isDescriptor(null), false);
  31. assert.equal(isDescriptor([]), false);
  32. ```
  33. ### data descriptor
  34. `true` when the object has valid properties with valid values.
  35. ```js
  36. assert.equal(isDescriptor({ value: 'foo' }), true);
  37. assert.equal(isDescriptor({ value() {} }), true);
  38. ```
  39. `false` when the object has invalid properties
  40. ```js
  41. assert.equal(isDescriptor({ value: 'foo', enumerable: 'baz' }), false);
  42. assert.equal(isDescriptor({ value: 'foo', configurable: 'baz' }), false);
  43. assert.equal(isDescriptor({ value: 'foo', get() {} }), false);
  44. assert.equal(isDescriptor({ get() {}, value() {} }), false);
  45. ```
  46. `false` when a value is not the correct type
  47. ```js
  48. assert.equal(isDescriptor({ value: 'foo', enumerable: 'foo' }), false);
  49. assert.equal(isDescriptor({ value: 'foo', configurable: 'foo' }), false);
  50. assert.equal(isDescriptor({ value: 'foo', writable: 'foo' }), false);
  51. ```
  52. ### accessor descriptor
  53. `true` when the object has valid properties with valid values.
  54. ```js
  55. assert.equal(isDescriptor({ get() {}, set() {} }), true);
  56. assert.equal(isDescriptor({ get() {} }), true);
  57. assert.equal(isDescriptor({ set() {} }), true);
  58. ```
  59. `false` when the object has invalid properties
  60. ```js
  61. assert.equal(isDescriptor({ get() {}, set() {}, enumerable: 'baz' }), false);
  62. assert.equal(isDescriptor({ get() {}, writable: true }), false);
  63. assert.equal(isDescriptor({ get() {}, value: true }), false);
  64. ```
  65. `false` when an accessor is not a function
  66. ```js
  67. assert.equal(isDescriptor({ get() {}, set: 'baz' }), false);
  68. assert.equal(isDescriptor({ get: 'foo', set() {} }), false);
  69. assert.equal(isDescriptor({ get: 'foo', bar: 'baz' }), false);
  70. assert.equal(isDescriptor({ get: 'foo', set: 'baz' }), false);
  71. ```
  72. `false` when a value is not the correct type
  73. ```js
  74. assert.equal(isDescriptor({ get() {}, set() {}, enumerable: 'foo' }), false);
  75. assert.equal(isDescriptor({ set() {}, configurable: 'foo' }), false);
  76. assert.equal(isDescriptor({ get() {}, configurable: 'foo' }), false);
  77. ```
  78. ### Related projects
  79. You might also be interested in these projects:
  80. * [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.
  81. * [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.
  82. * [is-object](https://www.npmjs.com/package/is-object): Returns true if the value is an object and not an array or null.
  83. ## Tests
  84. Simply clone the repo, `npm install`, and run `npm test`
  85. [package-url]: https://npmjs.org/package/is-descriptor
  86. [npm-version-svg]: https://versionbadg.es/inspect-js/is-descriptor.svg
  87. [deps-svg]: https://david-dm.org/inspect-js/is-descriptor.svg
  88. [deps-url]: https://david-dm.org/inspect-js/is-descriptor
  89. [dev-deps-svg]: https://david-dm.org/inspect-js/is-descriptor/dev-status.svg
  90. [dev-deps-url]: https://david-dm.org/inspect-js/is-descriptor#info=devDependencies
  91. [npm-badge-png]: https://nodei.co/npm/is-descriptor.png?downloads=true&stars=true
  92. [license-image]: https://img.shields.io/npm/l/is-descriptor.svg
  93. [license-url]: LICENSE
  94. [downloads-image]: https://img.shields.io/npm/dm/is-descriptor.svg
  95. [downloads-url]: https://npm-stat.com/charts.html?package=is-descriptor
  96. [codecov-image]: https://codecov.io/gh/inspect-js/is-descriptor/branch/main/graphs/badge.svg
  97. [codecov-url]: https://app.codecov.io/gh/inspect-js/is-descriptor/
  98. [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/inspect-js/is-descriptor
  99. [actions-url]: https://github.com/inspect-js/is-descriptor/actions