index.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. 'use strict';
  2. var hasOwn = require('hasown');
  3. // accessor descriptor properties
  4. var accessor = {
  5. __proto__: null,
  6. configurable: 'boolean',
  7. enumerable: 'boolean',
  8. get: 'function',
  9. set: 'function'
  10. };
  11. module.exports = function isAccessorDescriptor(obj, prop) {
  12. if (typeof prop === 'string') {
  13. var val = Object.getOwnPropertyDescriptor(obj, prop);
  14. return typeof val !== 'undefined';
  15. }
  16. if (!obj || typeof obj !== 'object') {
  17. return false;
  18. }
  19. if (hasOwn(obj, 'value') || hasOwn(obj, 'writable')) {
  20. return false;
  21. }
  22. // one of them must be a function
  23. if (
  24. (!hasOwn(obj, 'get') || typeof obj.get !== 'function')
  25. && (!hasOwn(obj, 'set') || typeof obj.set !== 'function')
  26. ) {
  27. return false;
  28. }
  29. // both of them must be a function or undefined
  30. if (
  31. (hasOwn(obj, 'get') && typeof obj.get !== 'function' && typeof obj.get !== 'undefined')
  32. || (hasOwn(obj, 'set') && typeof obj.set !== 'function' && typeof obj.set !== 'undefined')
  33. ) {
  34. return false;
  35. }
  36. for (var key in obj) { // eslint-disable-line no-restricted-syntax
  37. if (hasOwn(obj, key) && hasOwn(accessor, key) && typeof obj[key] !== accessor[key] && typeof obj[key] !== 'undefined') {
  38. return false;
  39. }
  40. }
  41. return true;
  42. };