|
| 1 | +/* |
| 2 | +
|
| 3 | + over.js - BETA |
| 4 | + Elegant function overloading in JavaScript. |
| 5 | +
|
| 6 | + by Mat Ryer and Ryan Quinn |
| 7 | + Copyright (c) 2013 Stretchr, Inc. |
| 8 | +
|
| 9 | + Please consider promoting this project if you find it useful. |
| 10 | +
|
| 11 | + Permission is hereby granted, free of charge, to any person obtaining a copy of this |
| 12 | + software and associated documentation files (the "Software"), to deal in the Software |
| 13 | + without restriction, including without limitation the rights to use, copy, modify, merge, |
| 14 | + publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
| 15 | + to whom the Software is furnished to do so, subject to the following conditions: |
| 16 | +
|
| 17 | + The above copyright notice and this permission notice shall be included in all copies |
| 18 | + or substantial portions of the Software. |
| 19 | +
|
| 20 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 21 | + INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
| 22 | + PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
| 23 | + FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 24 | + OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 25 | + DEALINGS IN THE SOFTWARE. |
| 26 | +
|
| 27 | +*/ |
| 28 | +(function(global){ |
| 29 | + |
| 30 | + global.MakeOver = function(){ |
| 31 | + return function(){ |
| 32 | + |
| 33 | + // get the mapping |
| 34 | + var $map = global.Over.map.apply(this, arguments); |
| 35 | + |
| 36 | + return function(){ |
| 37 | + |
| 38 | + for (var i in $map) { |
| 39 | + if (global.Over.test($map[i].sig, arguments)) { |
| 40 | + return $map[i].func.apply(this, arguments); |
| 41 | + } |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + }; |
| 46 | + }; |
| 47 | + |
| 48 | + /** |
| 49 | + * MakeOver makes a new Over function (useful for testing). |
| 50 | + */ |
| 51 | + global.Over = global.MakeOver(); |
| 52 | + |
| 53 | + /** |
| 54 | + * An object containing functions that can check individual arguments |
| 55 | + * and make decisions on whether they are indeed something or not. |
| 56 | + */ |
| 57 | + global.Over._isType = function(t,v){ return typeof(v)===t; }; |
| 58 | + global.Over.is = { |
| 59 | + "string": function(v){ return global.Over._isType("string", v); }, |
| 60 | + "number": function(v){ return global.Over._isType("number", v); }, |
| 61 | + "object": function(v){ return v != null && global.Over._isType("object", v) && typeof(v.length)==="undefined"; }, |
| 62 | + "array": function(v){ return v != null && global.Over._isType("object", v) && typeof(v.length)!=="undefined"; }, |
| 63 | + "boolean": function(v){ return global.Over._isType("boolean", v); }, |
| 64 | + "function": function(v){ return global.Over._isType("function", v); }, |
| 65 | + "null": function(v){ return v === null; }, |
| 66 | + "undefined": function(v){ return global.Over._isType("undefined", v); }, |
| 67 | + "nothing": function(v){ return global.Over.is["null"](v) || global.Over.is["undefined"](v) }, |
| 68 | + "etc": function(){ return global.Over.etc; } |
| 69 | + }; |
| 70 | + |
| 71 | + // shortcuts |
| 72 | + global.Over.is.bool = global.Over.is["boolean"]; |
| 73 | + |
| 74 | + /** |
| 75 | + * A special reference object that means whatever the arguments are, |
| 76 | + * they're OK. |
| 77 | + */ |
| 78 | + global.Over.etc = {}; |
| 79 | + |
| 80 | + /** |
| 81 | + * Creates a list of signatures mapped to the handler functions. |
| 82 | + */ |
| 83 | + global.Over.map = function(){ |
| 84 | + |
| 85 | + var items = []; |
| 86 | + |
| 87 | + for (var i in arguments) { |
| 88 | + var func = arguments[i]; |
| 89 | + items.push({ |
| 90 | + "sig": global.Over.signature(func), |
| 91 | + "func": func |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + return items; |
| 96 | + |
| 97 | + }; |
| 98 | + |
| 99 | + /** |
| 100 | + * Checks arguments against a signature array. |
| 101 | + */ |
| 102 | + global.Over.test = function(sig, args){ |
| 103 | + |
| 104 | + for (var i = 0; i < Math.max(sig.length, args.length); i++) { |
| 105 | + |
| 106 | + var sigItem = sig[i]; |
| 107 | + var argItem = args[i]; |
| 108 | + |
| 109 | + if (typeof(sigItem)==="undefined") { |
| 110 | + // no sig item |
| 111 | + return false; |
| 112 | + } |
| 113 | + |
| 114 | + var result; |
| 115 | + try { |
| 116 | + result = sigItem(argItem); |
| 117 | + } catch(e) { |
| 118 | + console.warn(e); |
| 119 | + return false; |
| 120 | + } |
| 121 | + |
| 122 | + if (result === false) { |
| 123 | + return false; |
| 124 | + } else if (result === global.Over.etc) { |
| 125 | + return true; |
| 126 | + } |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + return true; |
| 131 | + }; |
| 132 | + |
| 133 | + /** |
| 134 | + * Gets an array of is methods to be called to test a |
| 135 | + * method call, based on the specified function. |
| 136 | + */ |
| 137 | + global.Over.signature = function(f){ |
| 138 | + |
| 139 | + var sig = []; |
| 140 | + var args = global.Over.argnames(f); |
| 141 | + for (var argI in args) { |
| 142 | + var arg = args[argI]; |
| 143 | + var checker = global.Over.is[global.Over.checkFuncFromArg(arg)]; |
| 144 | + if (typeof(checker)==="undefined") { |
| 145 | + console.warn("over.js: Unknown checker for '" + arg + "'. Try adding Over.is[\"" + arg + "\"] = function(v){};") |
| 146 | + } |
| 147 | + sig.push(checker); |
| 148 | + } |
| 149 | + |
| 150 | + return sig; |
| 151 | + |
| 152 | + }; |
| 153 | + |
| 154 | + /** |
| 155 | + * Gets the names of all |
| 156 | + */ |
| 157 | + global.Over.argnames = function(f){ |
| 158 | + var names = f.toString().split("(")[1].split(")")[0].split(","); |
| 159 | + for (var i in names) names[i] = names[i].replace(/^\s+|\s+$/g, ''); |
| 160 | + return names; |
| 161 | + }; |
| 162 | + |
| 163 | + /** |
| 164 | + * Gets the name of the checker func from an argument.r |
| 165 | + */ |
| 166 | + global.Over.checkFuncFromArg = function(arg){ |
| 167 | + return arg.split("$")[1]; |
| 168 | + } |
| 169 | + |
| 170 | +})(window); |
0 commit comments