Blame view

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