8a973d4ec470b737bd3311ef5e3a0fb1215551bc36ac5cc5abbca99a3e2901b235cbe1ec024257efe4b9c7252b8948a1dfde864af31a4e115f88c332b4ed06 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # is-accessor-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 accessor descriptor.
  8. ## Examples
  9. ```js
  10. var isAccessor = require('is-accessor-descriptor');
  11. var assert = require('assert');
  12. assert.equal(isAccessor({ get: function() {} }), true);
  13. ```
  14. You may also pass an object and property name to check if the property is an accessor:
  15. ```js
  16. assert.equal(isAccessor({ bar: {} }, 'bar'), true);
  17. ```
  18. ## Examples
  19. `false` when not an object
  20. ```js
  21. assert.equal(isAccessor('a'), false);
  22. assert.equal(isAccessor(null), false);
  23. ```
  24. `true` when the object has valid properties
  25. and the properties all have the correct JavaScript types:
  26. ```js
  27. assert.equal(isAccessor({ get() {}, set() {} }), true);
  28. assert.equal(isAccessor({ get() {} }), true);
  29. assert.equal(isAccessor({ set() {} }), true);
  30. ```
  31. `false` when the object has invalid properties
  32. ```js
  33. assert.equal(isAccessor({ get() {}, set() {}, enumerable: 'baz' }), false);
  34. assert.equal(isAccessor({ get() {}, writable: true }), false);
  35. assert.equal(isAccessor({ get() {}, value: true }), false);
  36. ```
  37. `false` when an accessor is not a function
  38. ```js
  39. isAccessor({ get() {}, set: 'baz' });
  40. isAccessor({ get: 'foo', set() {} });
  41. isAccessor({ get: 'foo', bar: 'baz' });
  42. isAccessor({ get: 'foo', set: 'baz' });
  43. //=> false
  44. ```
  45. `false` when a value is not the correct type
  46. ```js
  47. isAccessor({ get() {}, set() {}, enumerable: 'foo' });
  48. isAccessor({ set() {}, configurable: 'foo' });
  49. isAccessor({ get() {}, configurable: 'foo' });
  50. //=> false
  51. ```
  52. ### Related projects
  53. You might also be interested in these projects:
  54. * [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.
  55. * [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/inspect-js/is-descriptor)
  56. * [is-object](https://www.npmjs.com/package/is-object): Returns true if the value is an object and not an array or null.
  57. ## Tests
  58. Simply clone the repo, `npm install`, and run `npm test`
  59. [package-url]: https://npmjs.org/package/is-accessor-descriptor
  60. [npm-version-svg]: https://versionbadg.es/inspect-js/is-accessor-descriptor.svg
  61. [deps-svg]: https://david-dm.org/inspect-js/is-accessor-descriptor.svg
  62. [deps-url]: https://david-dm.org/inspect-js/is-accessor-descriptor
  63. [dev-deps-svg]: https://david-dm.org/inspect-js/is-accessor-descriptor/dev-status.svg
  64. [dev-deps-url]: https://david-dm.org/inspect-js/is-accessor-descriptor#info=devDependencies
  65. [npm-badge-png]: https://nodei.co/npm/is-accessor-descriptor.png?downloads=true&stars=true
  66. [license-image]: https://img.shields.io/npm/l/is-accessor-descriptor.svg
  67. [license-url]: LICENSE
  68. [downloads-image]: https://img.shields.io/npm/dm/is-accessor-descriptor.svg
  69. [downloads-url]: https://npm-stat.com/charts.html?package=is-accessor-descriptor
  70. [codecov-image]: https://codecov.io/gh/inspect-js/is-accessor-descriptor/branch/main/graphs/badge.svg
  71. [codecov-url]: https://app.codecov.io/gh/inspect-js/is-accessor-descriptor/
  72. [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/inspect-js/is-accessor-descriptor
  73. [actions-url]: https://github.com/inspect-js/is-accessor-descriptor/actions