File: index.js

package info (click to toggle)
node-is-accessor-descriptor 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 112 kB
  • sloc: makefile: 4; sh: 2
file content (71 lines) | stat: -rw-r--r-- 1,626 bytes parent folder | download
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
70
71
/*!
 * is-accessor-descriptor <https://github.com/jonschlinkert/is-accessor-descriptor>
 *
 * Copyright (c) 2015-2017, Jon Schlinkert.
 * Released under the MIT License.
 */

'use strict';

var typeOf = require('kind-of');

function isAccessorDescriptor(obj, prop, checkProto) {
  if (typeof prop === 'boolean') {
    checkProto = prop;
    prop = null;
  }
  if (typeof prop === 'string') {
    return isGetterSetter(obj, prop, checkProto);
  }
  return looksLikeGetterSetter(obj);
}

function isGetterSetter(obj, key, checkProto) {
  const descriptor = Object.getOwnPropertyDescriptor(obj, key);
  if (descriptor) {
    return looksLikeGetterSetter(descriptor);
  }
  if (checkProto !== false && obj.constructor && obj.constructor.prototype) {
    return isGetterSetter(obj.constructor.prototype, key, false);
  }
  return false;
}

function looksLikeGetterSetter(descriptor) {
  if (typeOf(descriptor) !== 'object') {
    return false;
  }

  const keys = Object.keys(descriptor);
  const len = keys.length;

  const validKeys = {
    get: 'function',
    set: 'function',
    enumerable: 'boolean',
    configurable: 'boolean'
  };

  if (len !== 4) return false;
  for (let i = 0; i < len; i++) {
    const key = keys[i];
    if (!validKeys.hasOwnProperty(key)) {
      return false;
    }
    const val = descriptor[key];
    if (val != null && typeOf(val) !== validKeys[key]) {
      return false;
    }
  }
  return true;
}

function isValidType(obj, key) {
  return typeof obj[key] === 'function' || typeof obj[key] === 'undefined';
}

/**
 * Expose `isAccessorDescriptor`
 */

module.exports = isAccessorDescriptor;