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.

127 lines
3.2 KiB

4 years ago
  1. # is-data-descriptor [![NPM version](https://img.shields.io/npm/v/is-data-descriptor.svg)](https://www.npmjs.com/package/is-data-descriptor) [![Build Status](https://img.shields.io/travis/jonschlinkert/is-data-descriptor.svg)](https://travis-ci.org/jonschlinkert/is-data-descriptor)
  2. > Returns true if a value has the characteristics of a valid JavaScript data descriptor.
  3. ## Install
  4. Install with [npm](https://www.npmjs.com/):
  5. ```sh
  6. $ npm i is-data-descriptor --save
  7. ```
  8. ## Usage
  9. ```js
  10. var isDataDesc = require('is-data-descriptor');
  11. ```
  12. ## Examples
  13. `true` when the descriptor has valid properties with valid values.
  14. ```js
  15. // `value` can be anything
  16. isDataDesc({value: 'foo'})
  17. isDataDesc({value: function() {}})
  18. isDataDesc({value: true})
  19. //=> true
  20. ```
  21. `false` when not an object
  22. ```js
  23. isDataDesc('a')
  24. //=> false
  25. isDataDesc(null)
  26. //=> false
  27. isDataDesc([])
  28. //=> false
  29. ```
  30. `false` when the object has invalid properties
  31. ```js
  32. isDataDesc({value: 'foo', bar: 'baz'})
  33. //=> false
  34. isDataDesc({value: 'foo', bar: 'baz'})
  35. //=> false
  36. isDataDesc({value: 'foo', get: function(){}})
  37. //=> false
  38. isDataDesc({get: function(){}, value: 'foo'})
  39. //=> false
  40. ```
  41. `false` when a value is not the correct type
  42. ```js
  43. isDataDesc({value: 'foo', enumerable: 'foo'})
  44. //=> false
  45. isDataDesc({value: 'foo', configurable: 'foo'})
  46. //=> false
  47. isDataDesc({value: 'foo', writable: 'foo'})
  48. //=> false
  49. ```
  50. ## Valid properties
  51. The only valid data descriptor properties are the following:
  52. * `configurable` (required)
  53. * `enumerable` (required)
  54. * `value` (optional)
  55. * `writable` (optional)
  56. To be a valid data descriptor, either `value` or `writable` must be defined.
  57. **Invalid properties**
  58. A descriptor may have additional _invalid_ properties (an error will **not** be thrown).
  59. ```js
  60. var foo = {};
  61. Object.defineProperty(foo, 'bar', {
  62. enumerable: true,
  63. whatever: 'blah', // invalid, but doesn't cause an error
  64. get: function() {
  65. return 'baz';
  66. }
  67. });
  68. console.log(foo.bar);
  69. //=> 'baz'
  70. ```
  71. ## Related projects
  72. * [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)
  73. * [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://www.npmjs.com/package/is-descriptor) | [homepage](https://github.com/jonschlinkert/is-descriptor)
  74. * [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)
  75. ## Running tests
  76. Install dev dependencies:
  77. ```sh
  78. $ npm i -d && npm test
  79. ```
  80. ## Contributing
  81. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/is-data-descriptor/issues/new).
  82. ## Author
  83. **Jon Schlinkert**
  84. * [github/jonschlinkert](https://github.com/jonschlinkert)
  85. * [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
  86. ## License
  87. Copyright © 2015 [Jon Schlinkert](https://github.com/jonschlinkert)
  88. Released under the MIT license.
  89. ***
  90. _This file was generated by [verb](https://github.com/verbose/verb) on December 28, 2015._