-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyojs.js
70 lines (59 loc) · 2.08 KB
/
yojs.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
// git@github.com:codus/yojs.git
// Version: 0.0.1
if(!Array.prototype.last) {
Array.prototype.last = function() {
return this[this.length - 1];
}
}
var yojs = (function(){
var nameFunctionHash = {};
var parentsNamespaces = [];
var getNameSpaceResult = function(namespace, arguments) {
if (typeof(nameFunctionHash[namespace]) == 'function') {
return nameFunctionHash[namespace].apply(this, arguments);
} else {
return nameFunctionHash[namespace];
}
}
return {
define: function(namespace, value) {
if (this.isDefined(namespace)) {
var errorMessage = "Error: trying to define namespace '" + namespace + "'' twice";
throw errorMessage;
} else {
nameFunctionHash[namespace] = value;
}
return;
},
set: function(namespace, value) {
nameFunctionHash[namespace] = value;
},
call: function(namespace){
var parentNamespaceArray = namespace.split(".");
var lastNameCalled = parentNamespaceArray.pop();
var currentParentNamespace = parentNamespaceArray.join(".");
var returnValue = null;
var lastParentNamespace = parentsNamespaces.last();
var functionWithLastParentClassNamespace = lastParentNamespace + "." + lastNameCalled;
var errorMessage = "";
var calledFunctionArguments = Array.prototype.slice.apply(arguments, [1]);
parentsNamespaces.push(currentParentNamespace);
if (this.isDefined(namespace)) {
returnValue = getNameSpaceResult(namespace, calledFunctionArguments);
} else if (this.isDefined(functionWithLastParentClassNamespace)) {
returnValue = getNameSpaceResult(functionWithLastParentClassNamespace, calledFunctionArguments);
} else {
var errorMessage = "Error: called namespace '" + namespace + "'. But it doesn't exist.";
throw errorMessage;
}
parentsNamespaces.pop();
return returnValue;
},
get: function(namespace) {
return this.call(namespace);
},
isDefined: function(namespace) {
return nameFunctionHash.hasOwnProperty(namespace);
}
}
}());