index.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*!
  2. * is-accessor-descriptor <https://github.com/jonschlinkert/is-accessor-descriptor>
  3. *
  4. * Copyright (c) 2015-2017, Jon Schlinkert.
  5. * Released 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;