-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
66 lines (51 loc) · 1.98 KB
/
index.js
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
'use strict';
function withIs(Class, { className, symbolName }) {
const symbol = Symbol.for(symbolName);
const ClassIsWrapper = {
// The code below assigns the class wrapper to an object to trick
// JavaScript engines to show the name of the extended class when
// logging an instances.
// We are assigning an anonymous class (class wrapper) to the object
// with key `className` to keep the correct name.
// If this is not supported it falls back to logging `ClassIsWrapper`.
[className]: class extends Class {
constructor(...args) {
super(...args);
Object.defineProperty(this, symbol, { value: true });
}
get [Symbol.toStringTag]() {
return className;
}
},
}[className];
ClassIsWrapper[`is${className}`] = (obj) => !!(obj && obj[symbol]);
return ClassIsWrapper;
}
function withIsProto(Class, { className, symbolName, withoutNew }) {
const symbol = Symbol.for(symbolName);
/* eslint-disable object-shorthand */
const ClassIsWrapper = {
[className]: function (...args) {
if (withoutNew && !(this instanceof ClassIsWrapper)) {
return new ClassIsWrapper(...args);
}
const _this = Class.call(this, ...args) || this;
if (_this && !_this[symbol]) {
Object.defineProperty(_this, symbol, { value: true });
}
return _this;
},
}[className];
/* eslint-enable object-shorthand */
ClassIsWrapper.prototype = Object.create(Class.prototype);
ClassIsWrapper.prototype.constructor = ClassIsWrapper;
Object.defineProperty(ClassIsWrapper.prototype, Symbol.toStringTag, {
get() {
return className;
},
});
ClassIsWrapper[`is${className}`] = (obj) => !!(obj && obj[symbol]);
return ClassIsWrapper;
}
module.exports = withIs;
module.exports.proto = withIsProto;