Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80677 views
1
var path = require('path');
2
3
/**
4
* Intercepts a method by replacing the prototype's implementation
5
* with a wrapper that invokes the given interceptor instead.
6
*
7
* utils.intercept(core.Element, 'inserBefore',
8
* function(_super, args, newChild, refChild) {
9
* console.log('insertBefore', newChild, refChild);
10
* return _super.apply(this, args);
11
* }
12
* );
13
*/
14
exports.intercept = function(clazz, method, interceptor) {
15
var proto = clazz.prototype,
16
_super = proto[method],
17
unwrapArgs = interceptor.length > 2;
18
19
proto[method] = function() {
20
if (unwrapArgs) {
21
var args = Array.prototype.slice.call(arguments);
22
args.unshift(_super, arguments);
23
return interceptor.apply(this, args);
24
}
25
else {
26
return interceptor.call(this, _super, arguments);
27
}
28
};
29
};
30
31
exports.toFileUrl = function (fileName) {
32
// Beyond just the `path.resolve`, this is mostly for the benefit of Windows,
33
// where we need to convert '\' to '/' and add an extra '/' prefix before the
34
// drive letter.
35
var pathname = path.resolve(process.cwd(), fileName).replace(/\\/g, '/');
36
if (pathname[0] !== '/') {
37
pathname = '/' + pathname;
38
}
39
40
return 'file://' + pathname;
41
};
42
43
/**
44
* Define a setter on an object
45
*
46
* This method replaces any existing setter but leaves getters in place.
47
*
48
* - `object` {Object} the object to define the setter on
49
* - `property` {String} the name of the setter
50
* - `setterFn` {Function} the setter
51
*/
52
exports.defineSetter = function defineSetter(object, property, setterFn) {
53
var descriptor = Object.getOwnPropertyDescriptor(object, property) || {
54
configurable: true,
55
enumerable: true
56
};
57
58
descriptor.set = setterFn;
59
60
Object.defineProperty(object, property, descriptor);
61
};
62
63
/**
64
* Define a getter on an object
65
*
66
* This method replaces any existing getter but leaves setters in place.
67
*
68
* - `object` {Object} the object to define the getter on
69
* - `property` {String} the name of the getter
70
* - `getterFn` {Function} the getter
71
*/
72
exports.defineGetter = function defineGetter(object, property, getterFn) {
73
var descriptor = Object.getOwnPropertyDescriptor(object, property) || {
74
configurable: true,
75
enumerable: true
76
};
77
78
descriptor.get = getterFn;
79
80
Object.defineProperty(object, property, descriptor);
81
};
82
83
/**
84
* Create an object with the given prototype
85
*
86
* Optionally augment the created object.
87
*
88
* - `prototyp` {Object} the created object's prototype
89
* - `[properties]` {Object} properties to attach to the created object
90
*/
91
exports.createFrom = function createFrom(prototype, properties) {
92
properties = properties || {};
93
94
var descriptors = {};
95
Object.getOwnPropertyNames(properties).forEach(function (name) {
96
descriptors[name] = Object.getOwnPropertyDescriptor(properties, name);
97
});
98
99
return Object.create(prototype, descriptors);
100
};
101
102
/**
103
* Create an inheritance relationship between two classes
104
*
105
* Optionally augment the inherited prototype.
106
*
107
* - `Superclass` {Function} the inherited class
108
* - `Subclass` {Function} the inheriting class
109
* - `[properties]` {Object} properties to attach to the inherited prototype
110
*/
111
exports.inheritFrom = function inheritFrom(Superclass, Subclass, properties) {
112
properties = properties || {};
113
114
Object.defineProperty(properties, 'constructor', {
115
value: Subclass,
116
writable: true,
117
configurable: true
118
});
119
120
Subclass.prototype = exports.createFrom(Superclass.prototype, properties);
121
};
122
123