index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*!
  2. * is-accessor-descriptor <https://github.com/jonschlinkert/is-accessor-descriptor>
  3. *
  4. * Copyright (c) 2015, Jon Schlinkert.
  5. * Licensed under the MIT License.
  6. */
  7. 'use strict';
  8. var typeOf = require('kind-of');
  9. // accessor descriptor properties
  10. var accessor = {
  11. get: 'function',
  12. set: 'function',
  13. configurable: 'boolean',
  14. enumerable: 'boolean'
  15. };
  16. function isAccessorDescriptor(obj, prop) {
  17. if (typeof prop === 'string') {
  18. var val = Object.getOwnPropertyDescriptor(obj, prop);
  19. return typeof val !== 'undefined';
  20. }
  21. if (typeOf(obj) !== 'object') {
  22. return false;
  23. }
  24. if (has(obj, 'value') || has(obj, 'writable')) {
  25. return false;
  26. }
  27. if (!has(obj, 'get') || typeof obj.get !== 'function') {
  28. return false;
  29. }
  30. // tldr: it's valid to have "set" be undefined
  31. // "set" might be undefined if `Object.getOwnPropertyDescriptor`
  32. // was used to get the value, and only `get` was defined by the user
  33. if (has(obj, 'set') && typeof obj[key] !== 'function' && typeof obj[key] !== 'undefined') {
  34. return false;
  35. }
  36. for (var key in obj) {
  37. if (!accessor.hasOwnProperty(key)) {
  38. continue;
  39. }
  40. if (typeOf(obj[key]) === accessor[key]) {
  41. continue;
  42. }
  43. if (typeof obj[key] !== 'undefined') {
  44. return false;
  45. }
  46. }
  47. return true;
  48. }
  49. function has(obj, key) {
  50. return {}.hasOwnProperty.call(obj, key);
  51. }
  52. /**
  53. * Expose `isAccessorDescriptor`
  54. */
  55. module.exports = isAccessorDescriptor;