-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (43 loc) · 1.17 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
/**
* Dependencies
*/
var Emitter = require('emitter');
/**
* Expose
*/
module.exports = functionSignatures;
/**
* Constructor
*/
function functionSignatures(_signatures) {
if (arguments.length !== 1)
throw new Error('You must pass exactly one object');
if (arguments.length === 1)
for (var signature in _signatures)
if (_signatures.hasOwnProperty(signature))
if (typeof _signatures[signature] !== 'function')
throw new Error("Property `" + signature + "` is not a function");
this._signatures = _signatures;
}
/**
* Prototype
*/
functionSignatures.prototype.normalize = function normalize(args) {
var signatures = this._signatures;
args = Array.prototype.slice.call(args, 0);
for (var name in signatures) {
if (signatures.hasOwnProperty(name)) {
var signature = signatures[name];
if (signature.length === 0 || signature.length === args.length) {
if (signature.apply(this, args)) {
return this.emit.apply(this, [name].concat(args));
}
}
}
}
throw new Error("Invalid signature for: " + normalize.caller.toString());
};
/**
* Mixin
*/
Emitter(functionSignatures.prototype);