• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hildjj / node-inspect-extracted / 9053558361

12 May 2024 06:59PM UTC coverage: 97.922%. Remained the same
9053558361

push

github

hildjj
3.0.2

1812 of 1862 branches covered (97.31%)

Branch coverage included in aggregate %.

3749 of 3817 relevant lines covered (98.22%)

2306.02 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/internal/util/types.js
1
'use strict';
40✔
2

40✔
3
const { getConstructorName } = require('../../util');
40✔
4

40✔
5
// From https://mathiasbynens.be/notes/globalthis
40✔
6
/* c8 ignore start */ // only needed for node 10
4✔
7
(function() {
4✔
8
  if (typeof globalThis === 'object') return;
4✔
9
  Object.defineProperty(Object.prototype, '__magic__', {
4✔
10
    get: function() {
4✔
11
      return this;
4✔
12
    },
4✔
13
    configurable: true,
4✔
14
  });
4✔
15
  // eslint-disable-next-line no-undef
4✔
16
  __magic__.globalThis = __magic__;
4✔
17
  delete Object.prototype.__magic__;
4✔
18
}());
4✔
19
/* c8 ignore stop */
4✔
20

40✔
21
function constructorNamed(val, ...name) {
13,212✔
22
  // Pass in names rather than types, in case SharedArrayBuffer (e.g.) isn't
13,212✔
23
  // in your browser
13,212✔
24
  for (const n of name) {
13,212✔
25
    const typ = globalThis[n];
21,804✔
26
    if (typ) {
21,804✔
27
      if (val instanceof typ) {
21,804✔
28
        return true;
1,100✔
29
      }
1,100✔
30
    }
21,804✔
31
  }
21,804✔
32
  // instanceOf doesn't work across vm boundaries, so check the whole
12,112✔
33
  // inheritance chain
12,112✔
34
  while (val) {
13,200✔
35
    if (typeof val !== 'object') {
24,688✔
36
      return false;
364✔
37
    }
364✔
38
    if (name.indexOf(getConstructorName(val)) >= 0) {
24,688✔
39
      return true;
168✔
40
    }
168✔
41
    val = Object.getPrototypeOf(val);
24,156✔
42
  }
24,156✔
43
  return false;
11,580✔
44
}
13,212✔
45

40✔
46
function checkBox(cls) {
200✔
47
  return (val) => {
200✔
48
    if (!constructorNamed(val, cls.name)) {
3,320✔
49
      return false;
3,072✔
50
    }
3,072✔
51
    try {
248✔
52
      cls.prototype.valueOf.call(val);
248✔
53
    } catch {
3,180✔
54
      return false;
4✔
55
    }
4✔
56
    return true;
244✔
57
  };
200✔
58
}
200✔
59

40✔
60
const isStringObject = checkBox(String);
40✔
61
const isNumberObject = checkBox(Number);
40✔
62
const isBooleanObject = checkBox(Boolean);
40✔
63
const isBigIntObject = checkBox(BigInt);
40✔
64
const isSymbolObject = checkBox(Symbol);
40✔
65

40✔
66
module.exports = {
40✔
67
  isAsyncFunction(val) {
40✔
68
    return (typeof val === 'function') &&
580✔
69
      Function.prototype.toString.call(val).startsWith('async');
580✔
70
  },
40✔
71
  isGeneratorFunction(val) {
40✔
72
    return (typeof val === 'function') &&
580✔
73
      Function.prototype.toString.call(val).match(/^(async\s+)?function *\*/);
580✔
74
  },
40✔
75
  isAnyArrayBuffer(val) {
40✔
76
    return constructorNamed(val, 'ArrayBuffer', 'SharedArrayBuffer');
972✔
77
  },
40✔
78
  isArrayBuffer(val) {
40✔
79
    return constructorNamed(val, 'ArrayBuffer');
152✔
80
  },
40✔
81
  isArgumentsObject(val) {
40✔
82
    const cond = (val !== null) &&
5,242✔
83
      (typeof val === 'object') &&
5,242✔
84
      !Array.isArray(val) &&
5,242✔
85
      (typeof val.length === 'number') &&
5,242✔
86
      (val.length === (val.length | 0)) &&
5,242✔
87
      (val.length >= 0);
5,242✔
88
    if (cond) {
5,242✔
89
      const prop = Object.getOwnPropertyDescriptor(val, 'callee');
4✔
90
      return prop && !prop.enumerable;
4✔
91
    }
4✔
92
    return false;
5,238✔
93
  },
40✔
94
  isBoxedPrimitive(val) {
40✔
95
    return isNumberObject(val) ||
656✔
96
      isStringObject(val) ||
656✔
97
      isBooleanObject(val) ||
656✔
98
      isBigIntObject(val) ||
656✔
99
      isSymbolObject(val);
656✔
100
  },
40✔
101
  isDataView(val) {
40✔
102
    return constructorNamed(val, 'DataView');
820✔
103
  },
40✔
104
  isExternal(val) {
40✔
105
    return (typeof val === 'object') &&
149✔
106
      (Object.isFrozen(val)) &&
149✔
107
      (Object.getPrototypeOf(val) == null);
149✔
108
  },
40✔
109
  isMap(val) {
40✔
110
    if (!constructorNamed(val, 'Map')) {
996✔
111
      return false;
880✔
112
    }
880✔
113
    try {
116✔
114
      val.has();
116✔
115
    } catch {
980✔
116
      return false;
32✔
117
    }
32✔
118
    return true;
84✔
119
  },
40✔
120
  isMapIterator(val) {
40✔
121
    return Object.prototype.toString.call(Object.getPrototypeOf(val)) ===
676✔
122
      '[object Map Iterator]';
676✔
123
  },
40✔
124
  isModuleNamespaceObject(val) {
40✔
125
    // TODO: this is weak and easily faked
660✔
126
    return val &&
660✔
127
      (typeof val === 'object') &&
660✔
128
      (val[Symbol.toStringTag] === 'Module');
660✔
129
  },
40✔
130
  isNativeError(val) {
40✔
131
    return (val instanceof Error) && constructorNamed(
12✔
132
      val,
8✔
133
      'Error',
8✔
134
      'EvalError',
8✔
135
      'RangeError',
8✔
136
      'ReferenceError',
8✔
137
      'SyntaxError',
8✔
138
      'TypeError',
8✔
139
      'URIError',
8✔
140
      'AggregateError');
12✔
141
  },
40✔
142
  isPromise(val) {
40✔
143
    return constructorNamed(val, 'Promise');
796✔
144
  },
40✔
145
  isSet(val) {
40✔
146
    if (!constructorNamed(val, 'Set')) {
1,048✔
147
      return false;
996✔
148
    }
996✔
149
    try {
52✔
150
      val.has();
52✔
151
    } catch {
1,032✔
152
      return false;
4✔
153
    }
4✔
154
    return true;
48✔
155
  },
40✔
156
  isSetIterator(val) {
40✔
157
    return Object.prototype.toString.call(Object.getPrototypeOf(val)) ===
628✔
158
      '[object Set Iterator]';
628✔
159
  },
40✔
160
  isWeakMap(val) {
40✔
161
    return constructorNamed(val, 'WeakMap');
708✔
162
  },
40✔
163
  isWeakSet(val) {
40✔
164
    return constructorNamed(val, 'WeakSet');
756✔
165
  },
40✔
166
  isRegExp(val) {
40✔
167
    return constructorNamed(val, 'RegExp');
1,388✔
168
  },
40✔
169
  isDate(val) {
40✔
170
    if (constructorNamed(val, 'Date')) {
1,336✔
171
      try {
96✔
172
        Date.prototype.getTime.call(val); // Throws for pseudo-dates
96✔
173
        return true;
96✔
174
      } catch {
96✔
175
        // Ignored
4✔
176
      }
4✔
177
    }
96✔
178
    return false;
1,244✔
179
  },
40✔
180
  isTypedArray(val) {
40✔
181
    return constructorNamed(
912✔
182
      val,
912✔
183
      'Int8Array',
912✔
184
      'Uint8Array',
912✔
185
      'Uint8ClampedArray',
912✔
186
      'Int16Array',
912✔
187
      'Uint16Array',
912✔
188
      'Int32Array',
912✔
189
      'Uint32Array',
912✔
190
      'Float32Array',
912✔
191
      'Float64Array',
912✔
192
      'BigInt64Array',
912✔
193
      'BigUint64Array',
912✔
194
    );
912✔
195
  },
40✔
196
  isStringObject,
40✔
197
  isNumberObject,
40✔
198
  isBooleanObject,
40✔
199
  isBigIntObject,
40✔
200
  isSymbolObject,
40✔
201
};
40✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc