index.js
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 8 'use strict'; 9 10 var typeOf = require('kind-of'); 11 12 // accessor descriptor properties 13 var accessor = { 14 get: 'function', 15 set: 'function', 16 configurable: 'boolean', 17 enumerable: 'boolean' 18 }; 19 20 function isAccessorDescriptor(obj, prop) { 21 if (typeof prop === 'string') { 22 var val = Object.getOwnPropertyDescriptor(obj, prop); 23 return typeof val !== 'undefined'; 24 } 25 26 if (typeOf(obj) !== 'object') { 27 return false; 28 } 29 30 if (has(obj, 'value') || has(obj, 'writable')) { 31 return false; 32 } 33 34 if (!has(obj, 'get') || typeof obj.get !== 'function') { 35 return false; 36 } 37 38 // tldr: it's valid to have "set" be undefined 39 // "set" might be undefined if `Object.getOwnPropertyDescriptor` 40 // was used to get the value, and only `get` was defined by the user 41 if (has(obj, 'set') && typeof obj[key] !== 'function' && typeof obj[key] !== 'undefined') { 42 return false; 43 } 44 45 for (var key in obj) { 46 if (!accessor.hasOwnProperty(key)) { 47 continue; 48 } 49 50 if (typeOf(obj[key]) === accessor[key]) { 51 continue; 52 } 53 54 if (typeof obj[key] !== 'undefined') { 55 return false; 56 } 57 } 58 return true; 59 } 60 61 function has(obj, key) { 62 return {}.hasOwnProperty.call(obj, key); 63 } 64 65 /** 66 * Expose `isAccessorDescriptor` 67 */ 68 69 module.exports = isAccessorDescriptor;