f3522d3000d35d4508e1810229987bebede92a9f699ef7914f32bf4fdcf9585bb62ba5625dbf58f58d8bcdb6efe5ea60c25e4832ccc50510151f23e08025ee 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # is-data-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 data descriptor.
  8. ## Install
  9. Install with [npm](https://npmjs.com/):
  10. ```sh
  11. $ npm install --save is-data-descriptor
  12. ```
  13. ## Usage
  14. ```js
  15. var isDataDesc = require('is-data-descriptor');
  16. var assert = require('assert');
  17. ```
  18. ## Examples
  19. `true` when the descriptor has valid properties with valid values.
  20. ```js
  21. // `value` can be anything
  22. assert.equal(isDataDesc({ value: 'foo' }), true);
  23. assert.equal(isDataDesc({ value: function () {} }), true);
  24. assert.equal(isDataDesc({ value: true }), true);
  25. ```
  26. `false` when not an object
  27. ```js
  28. assert.equal(isDataDesc('a'), false);
  29. assert.equal(isDataDesc(null), false);
  30. ```
  31. `false` when the object has invalid properties
  32. ```js
  33. assert.equal(isDataDesc({ value: 'foo', enumerable: 'baz' }), false);
  34. assert.equal(isDataDesc({ value: 'foo', configurable: 'baz' }), false);
  35. assert.equal(isDataDesc({ value: 'foo', get() {} }), false);
  36. assert.equal(isDataDesc({ get() {}, value: 'foo' }), false);
  37. ```
  38. `false` when a value is not the correct type
  39. ```js
  40. assert.equal(isDataDesc({ value: 'foo', enumerable: 'foo' }), false);
  41. assert.equal(isDataDesc({ value: 'foo', configurable: 'foo' }), false);
  42. assert.equal(isDataDesc({ value: 'foo', writable: 'foo' }), false);
  43. ```
  44. ## Valid properties
  45. The only valid data descriptor properties are the following:
  46. * `configurable` (required)
  47. * `enumerable` (required)
  48. * `value` (optional)
  49. * `writable` (optional)
  50. To be a valid data descriptor, either `value` or `writable` must be defined.
  51. **Invalid properties**
  52. A descriptor may have additional _invalid_ properties (an error will **not** be thrown).
  53. ```js
  54. var foo = {};
  55. Object.defineProperty(foo, 'bar', {
  56. enumerable: true,
  57. whatever: 'blah', // invalid, but doesn't cause an error
  58. get() {
  59. return 'baz';
  60. }
  61. });
  62. assert.equal(foo.bar, 'baz');
  63. ```
  64. ### Related projects
  65. You might also be interested in these projects:
  66. * [is-accessor-descriptor](https://npmjs.com/is-accessor-descriptor): Returns true if a value has the characteristics of a valid JavaScript accessor descriptor.
  67. * [is-descriptor](https://npmjs.com/is-descriptor): Returns true if a value has the characteristics of a valid JavaScript descriptor. Works for… [more](https://npmjs.com/is-descriptor)
  68. [package-url]: https://npmjs.org/package/is-data-descriptor
  69. [npm-version-svg]: https://versionbadg.es/inspect-js/is-data-descriptor.svg
  70. [deps-svg]: https://david-dm.org/inspect-js/is-data-descriptor.svg
  71. [deps-url]: https://david-dm.org/inspect-js/is-data-descriptor
  72. [dev-deps-svg]: https://david-dm.org/inspect-js/is-data-descriptor/dev-status.svg
  73. [dev-deps-url]: https://david-dm.org/inspect-js/is-data-descriptor#info=devDependencies
  74. [npm-badge-png]: https://nodei.co/npm/is-data-descriptor.png?downloads=true&stars=true
  75. [license-image]: https://img.shields.io/npm/l/is-data-descriptor.svg
  76. [license-url]: LICENSE
  77. [downloads-image]: https://img.shields.io/npm/dm/is-data-descriptor.svg
  78. [downloads-url]: https://npm-stat.com/charts.html?package=is-data-descriptor
  79. [codecov-image]: https://codecov.io/gh/inspect-js/is-data-descriptor/branch/main/graphs/badge.svg
  80. [codecov-url]: https://app.codecov.io/gh/inspect-js/is-data-descriptor/
  81. [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/inspect-js/is-data-descriptor
  82. [actions-url]: https://github.com/inspect-js/is-data-descriptor/actions