Skip to content

Commit 35fe5ed

Browse files
author
Mat Ryer
committed
added v1 release and minified version
1 parent cb72262 commit 35fe5ed

File tree

5 files changed

+228
-2
lines changed

5 files changed

+228
-2
lines changed

min/over-min.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('(4(g){g.K=4(){3 4(){5 a=g.2.E.L(B,k);3 4(){9(5 i m a){l(g.2.A(a[i].z,k)){3 a[i].w.L(B,k)}}}}};g.2=g.K();g.2.6=4(t,v){3 h(v)===t};g.2.7={"C":4(v){3 g.2.6("C",v)},"D":4(v){3 g.2.6("D",v)},"r":4(v){3 v!=j&&g.2.6("r",v)&&h(v.p)==="8"},"O":4(v){3 v!=j&&g.2.6("r",v)&&h(v.p)!=="8"},"u":4(v){3 g.2.6("u",v)},"4":4(v){3 g.2.6("4",v)},"j":4(v){3 v===j},"8":4(v){3 g.2.6("8",v)},"M":4(v){3 g.2.7["j"](v)||g.2.7["8"](v)},"n":4(){3 g.2.n}};g.2.7.P=g.2.7["u"];g.2.n={};g.2.E=4(){5 a=[];9(5 i m k){5 b=k[i];a.x({"z":g.2.y(b),"w":b})}3 a};g.2.A=4(a,b){9(5 i=0;i<Y.Z(a.p,b.p);i++){5 c=a[i];5 d=b[i];l(h(c)==="8"){3 q}5 f;Q{f=c(d)}12(e){F.G(e);3 q}l(f===q){3 q}N l(f===g.2.n){3 H}}3 H};g.2.y=4(f){5 a=[];5 b=g.2.I(f);9(5 c m b){5 d=b[c];5 e=g.2.7[g.2.J(d)];l(h(e)==="8"){F.G("R.S: T U 9 \'"+d+"\'. V W 2.7[\\""+d+"\\"] = 4(v){};")}a.x(e)}3 a};g.2.I=4(f){5 a=f.X().o("(")[1].o(")")[0].o(",");9(5 i m a)a[i]=a[i].10(/^\\s+|\\s+$/g,\'\');3 a};g.2.J=4(a){3 a.o("$")[1]}})(11);',62,65,'||Over|return|function|var|_isType|is|undefined|for||||||||typeof||null|arguments|if|in|etc|split|length|false|object|||boolean||func|push|signature|sig|test|this|string|number|map|console|warn|true|argnames|checkFuncFromArg|MakeOver|apply|nothing|else|array|bool|try|over|js|Unknown|checker|Try|adding|toString|Math|max|replace|window|catch'.split('|'),0,{}));

release/over-v1-min.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('(4(g){g.K=4(){3 4(){5 a=g.2.E.L(B,k);3 4(){9(5 i m a){l(g.2.A(a[i].z,k)){3 a[i].w.L(B,k)}}}}};g.2=g.K();g.2.6=4(t,v){3 h(v)===t};g.2.7={"C":4(v){3 g.2.6("C",v)},"D":4(v){3 g.2.6("D",v)},"r":4(v){3 v!=j&&g.2.6("r",v)&&h(v.p)==="8"},"O":4(v){3 v!=j&&g.2.6("r",v)&&h(v.p)!=="8"},"u":4(v){3 g.2.6("u",v)},"4":4(v){3 g.2.6("4",v)},"j":4(v){3 v===j},"8":4(v){3 g.2.6("8",v)},"M":4(v){3 g.2.7["j"](v)||g.2.7["8"](v)},"n":4(){3 g.2.n}};g.2.7.P=g.2.7["u"];g.2.n={};g.2.E=4(){5 a=[];9(5 i m k){5 b=k[i];a.x({"z":g.2.y(b),"w":b})}3 a};g.2.A=4(a,b){9(5 i=0;i<Y.Z(a.p,b.p);i++){5 c=a[i];5 d=b[i];l(h(c)==="8"){3 q}5 f;Q{f=c(d)}12(e){F.G(e);3 q}l(f===q){3 q}N l(f===g.2.n){3 H}}3 H};g.2.y=4(f){5 a=[];5 b=g.2.I(f);9(5 c m b){5 d=b[c];5 e=g.2.7[g.2.J(d)];l(h(e)==="8"){F.G("R.S: T U 9 \'"+d+"\'. V W 2.7[\\""+d+"\\"] = 4(v){};")}a.x(e)}3 a};g.2.I=4(f){5 a=f.X().o("(")[1].o(")")[0].o(",");9(5 i m a)a[i]=a[i].10(/^\\s+|\\s+$/g,\'\');3 a};g.2.J=4(a){3 a.o("$")[1]}})(11);',62,65,'||Over|return|function|var|_isType|is|undefined|for||||||||typeof||null|arguments|if|in|etc|split|length|false|object|||boolean||func|push|signature|sig|test|this|string|number|map|console|warn|true|argnames|checkFuncFromArg|MakeOver|apply|nothing|else|array|bool|try|over|js|Unknown|checker|Try|adding|toString|Math|max|replace|window|catch'.split('|'),0,{}));

release/over-v1.js

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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);

src/over.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
try {
116116
result = sigItem(argItem);
117117
} catch(e) {
118-
console.warn(e)
118+
console.warn(e);
119119
return false;
120120
}
121121

test/SpecRunner-min.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<script type="text/javascript" src="../min/over-min.js"></script>
1414

1515
<!-- include spec files here... -->
16-
<script type="text/javascript" src="spec/arg.js"></script>
16+
<script type="text/javascript" src="spec/over.js"></script>
1717

1818
<script type="text/javascript">
1919
(function() {

0 commit comments

Comments
 (0)