-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (72 loc) · 2.33 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
67
68
69
70
71
72
73
74
75
76
77
78
module.exports = defineProtocol;
/**
* @param {Function} @optional protocol
* @param {Object} _interface is an object whose keys are the function names
* and whose values are an array representing the arguments signature
*/
function defineProtocol (protocol, _interface) {
if (arguments.length === 1) {
_interface = protocol;
var protocol = function (type, implementations) {
if (typeof type === 'function') {
var typePrototype = type.prototype;
}
var types = protocol[':types'];
var typeStr = typeOf(type && Object.create(typePrototype))
, typeFns = types[typeStr];
if (! typeFns) {
var protocolId = typePrototype.protocolId
, Other = types.Other;
typeFns = Other[protocolId] || (Other[protocolId] = {});
}
Object.keys(implementations).forEach( function (methodName) {
if (! (methodName in protocol)) {
throw new Error('This protocol does not include "' + methodName + '"');
}
if (methodName in typeFns) {
throw new Error('The type already implements the protocol method ' + methodName);
}
typeFns[methodName] = implementations[methodName];
});
}
protocol[':interface'] = _interface;
protocol[':types'] = {
'Arguments': {}
, 'Array': {}
, 'Boolean': {}
, 'Date': {}
, 'Function': {}
, 'Null': {}
, 'Number': {}
, 'Other': {} // For any constructors
, 'RegExp': {}
, 'String': {}
, 'Undefined': {}
};
} else {
for (var k in _interface) {
protocol[':interface'][k] = _interface[k];
}
}
extendProtocol(protocol, _interface);
return protocol;
}
function extendProtocol (protocol, _interface) {
Object.keys(_interface).forEach( function (methodName) {
var index = _interface[methodName].indexOf(defineProtocol)
, types = protocol[':types'];
protocol[methodName] = function method () {
var target = arguments[index]
, typeStr = typeOf(target)
, typeFns = types[typeStr]
, fn = (typeFns)
? typeFns[methodName]
: types.Other[target.protocolId][methodName];
return fn.apply(null, arguments);
};
});
}
function typeOf (x) {
var type = Object.prototype.toString.call(x).split(' ')[1].split(']')[0];
return type === 'Object' ? x.constructor.name : type;
}