|
25 | 25 | DEALINGS IN THE SOFTWARE.
|
26 | 26 |
|
27 | 27 | */
|
| 28 | + |
28 | 29 | (function(global){
|
29 | 30 |
|
30 |
| - global.MakeOver = function(){ |
| 31 | + /** Used to determine if values are of the language type Object */ |
| 32 | + var objectTypes = { |
| 33 | + 'boolean': false, |
| 34 | + 'function': true, |
| 35 | + 'object': true, |
| 36 | + 'number': false, |
| 37 | + 'string': false, |
| 38 | + 'undefined': false |
| 39 | + }; |
| 40 | + |
| 41 | + /** Used as a reference to the global object */ |
| 42 | + var root = (objectTypes[typeof window] && window) || this; |
| 43 | + |
| 44 | + /** Detect free variable `exports` */ |
| 45 | + var freeExports = objectTypes[typeof exports] && exports && !exports.nodeType && exports; |
| 46 | + |
| 47 | + /** Detect free variable `module` */ |
| 48 | + var freeModule = objectTypes[typeof module] && module && !module.nodeType && module; |
| 49 | + |
| 50 | + /** Detect the popular CommonJS extension `module.exports` */ |
| 51 | + var moduleExports = freeModule && freeModule.exports === freeExports && freeExports; |
| 52 | + |
| 53 | + /** Detect free variable `global` from Node.js or Browserified code and use it as `root` */ |
| 54 | + var freeGlobal = objectTypes[typeof global] && global; |
| 55 | + if (freeGlobal && (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal)) { |
| 56 | + root = freeGlobal; |
| 57 | + } |
| 58 | + |
| 59 | + root.MakeOver = function(){ |
31 | 60 | return function(){
|
32 | 61 |
|
33 | 62 | // get the mapping
|
34 |
| - var $map = global.Over.map.apply(this, arguments); |
| 63 | + var $map = root.Over.map.apply(this, arguments); |
35 | 64 |
|
36 | 65 | return function(){
|
37 | 66 |
|
38 | 67 | for (var i in $map) {
|
39 |
| - if (global.Over.test($map[i].sig, arguments)) { |
| 68 | + if (root.Over.test($map[i].sig, arguments)) { |
40 | 69 |
|
41 | 70 | var sig = $map[i].sig;
|
42 |
| - if (sig[sig.length-1] === global.Over.is.etc) { |
| 71 | + if (sig[sig.length-1] === root.Over.is.etc) { |
43 | 72 | // collect all $etc arguments
|
44 | 73 |
|
45 | 74 | var args = [];
|
46 | 75 | var etcArr = [];
|
47 |
| - for (var ietc = 0; ietc < sig.length-1; ietc++) { |
| 76 | + var iets; |
| 77 | + for (ietc = 0; ietc < sig.length-1; ietc++) { |
48 | 78 | args.push(arguments[ietc]);
|
49 | 79 | }
|
50 |
| - for (var ietc = sig.length-1; ietc < arguments.length; ietc++) { |
| 80 | + for (ietc = sig.length - 1; ietc < arguments.length; ietc++) { |
51 | 81 | etcArr.push(arguments[ietc]);
|
52 | 82 | }
|
53 | 83 | args.push(etcArr);
|
|
66 | 96 | /**
|
67 | 97 | * MakeOver makes a new Over function (useful for testing).
|
68 | 98 | */
|
69 |
| - global.Over = global.MakeOver(); |
| 99 | + root.Over = root.MakeOver(); |
70 | 100 |
|
71 | 101 | /**
|
72 | 102 | * The current version.
|
73 | 103 | */
|
74 |
| - global.Over.version = 1.1; |
| 104 | + root.Over.version = 1.1; |
75 | 105 |
|
76 | 106 | /**
|
77 | 107 | * The current version as a string.
|
78 | 108 | */
|
79 |
| - global.Over.versionString = "v1.1.0"; |
| 109 | + root.Over.versionString = "v1.1.0"; |
80 | 110 |
|
81 | 111 | /**
|
82 | 112 | * An object containing functions that can check individual arguments
|
83 | 113 | * and make decisions on whether they are indeed something or not.
|
84 | 114 | */
|
85 |
| - global.Over._isType = function(t,v){ return typeof(v)===t; }; |
86 |
| - global.Over.is = { |
87 |
| - "string": function(v){ return global.Over._isType("string", v); }, |
88 |
| - "number": function(v){ return global.Over._isType("number", v); }, |
89 |
| - "object": function(v){ return v != null && global.Over._isType("object", v) && typeof(v.length)==="undefined"; }, |
90 |
| - "array": function(v){ return v != null && global.Over._isType("object", v) && typeof(v.length)!=="undefined"; }, |
91 |
| - "boolean": function(v){ return global.Over._isType("boolean", v); }, |
92 |
| - "function": function(v){ return global.Over._isType("function", v); }, |
| 115 | + root.Over._isType = function(t,v){ return typeof(v)===t; }; |
| 116 | + root.Over.is = { |
| 117 | + "string": function(v){ return root.Over._isType("string", v); }, |
| 118 | + "number": function(v){ return root.Over._isType("number", v); }, |
| 119 | + "object": function(v){ return v !== null && root.Over._isType("object", v) && typeof(v.length)==="undefined"; }, |
| 120 | + "array": function(v){ return v !== null && root.Over._isType("object", v) && typeof(v.length)!=="undefined"; }, |
| 121 | + "boolean": function(v){ return root.Over._isType("boolean", v); }, |
| 122 | + "function": function(v){ return root.Over._isType("function", v); }, |
93 | 123 | "null": function(v){ return v === null; },
|
94 |
| - "undefined": function(v){ return global.Over._isType("undefined", v); }, |
95 |
| - "nothing": function(v){ return global.Over.is["null"](v) || global.Over.is["undefined"](v) }, |
96 |
| - "etc": function(){ return global.Over.etc; } |
| 124 | + "undefined": function(v){ return root.Over._isType("undefined", v); }, |
| 125 | + "nothing": function(v){ return root.Over.is["null"](v) || root.Over.is["undefined"](v); }, |
| 126 | + "etc": function(){ return root.Over.etc; } |
97 | 127 | };
|
98 | 128 |
|
99 | 129 | // shortcuts
|
100 |
| - global.Over.is.bool = global.Over.is["boolean"]; |
| 130 | + root.Over.is.bool = root.Over.is.boolean; |
101 | 131 |
|
102 | 132 | /**
|
103 | 133 | * A special reference object that means whatever the arguments are,
|
104 | 134 | * they're OK.
|
105 | 135 | */
|
106 |
| - global.Over.etc = {}; |
| 136 | + root.Over.etc = {}; |
107 | 137 |
|
108 | 138 | /**
|
109 | 139 | * Creates a list of signatures mapped to the handler functions.
|
110 | 140 | */
|
111 |
| - global.Over.map = function(){ |
| 141 | + root.Over.map = function(){ |
112 | 142 |
|
113 | 143 | var items = [];
|
114 | 144 |
|
115 | 145 | for (var i in arguments) {
|
116 | 146 | var func = arguments[i];
|
117 | 147 | items.push({
|
118 |
| - "sig": global.Over.signature(func), |
| 148 | + "sig": root.Over.signature(func), |
119 | 149 | "func": func
|
120 | 150 | });
|
121 | 151 | }
|
|
127 | 157 | /**
|
128 | 158 | * Checks arguments against a signature array.
|
129 | 159 | */
|
130 |
| - global.Over.test = function(sig, args){ |
| 160 | + root.Over.test = function(sig, args){ |
131 | 161 |
|
132 | 162 | for (var i = 0; i < Math.max(sig.length, args.length); i++) {
|
133 | 163 |
|
|
149 | 179 |
|
150 | 180 | if (result === false) {
|
151 | 181 | return false;
|
152 |
| - } else if (result === global.Over.etc) { |
| 182 | + } else if (result === root.Over.etc) { |
153 | 183 | return true;
|
154 | 184 | }
|
155 | 185 |
|
|
162 | 192 | * Gets an array of is methods to be called to test a
|
163 | 193 | * method call, based on the specified function.
|
164 | 194 | */
|
165 |
| - global.Over.signature = function(f){ |
| 195 | + root.Over.signature = function(f){ |
166 | 196 |
|
167 | 197 | var sig = [];
|
168 |
| - var args = global.Over.argnames(f); |
| 198 | + var args = root.Over.argnames(f); |
169 | 199 | for (var argI in args) {
|
170 | 200 | var arg = args[argI];
|
171 |
| - var checker = global.Over.is[global.Over.checkFuncFromArg(arg)]; |
| 201 | + var checker = root.Over.is[root.Over.checkFuncFromArg(arg)]; |
172 | 202 | if (typeof(checker)==="undefined") {
|
173 |
| - console.warn("over.js: Unknown checker for '" + arg + "'. Try adding Over.is[\"" + arg + "\"] = function(v){};") |
| 203 | + console.warn("over.js: Unknown checker for '" + arg + "'. Try adding Over.is[\"" + arg + "\"] = function(v){};"); |
174 | 204 | }
|
175 | 205 | sig.push(checker);
|
176 | 206 | }
|
|
182 | 212 | /**
|
183 | 213 | * Gets the names of all
|
184 | 214 | */
|
185 |
| - global.Over.argnames = function(f){ |
| 215 | + root.Over.argnames = function(f){ |
186 | 216 | var names = f.toString().split("(")[1].split(")")[0].split(",");
|
187 | 217 | for (var i in names) names[i] = names[i].replace(/^\s+|\s+$/g, '');
|
188 | 218 | return names;
|
|
191 | 221 | /**
|
192 | 222 | * Gets the name of the checker func from an argument.r
|
193 | 223 | */
|
194 |
| - global.Over.checkFuncFromArg = function(arg){ |
| 224 | + root.Over.checkFuncFromArg = function(arg){ |
195 | 225 | return arg.split("$")[1];
|
196 |
| - } |
| 226 | + }; |
197 | 227 |
|
198 |
| -})(window); |
| 228 | +}).call(this); |
0 commit comments