Skip to content

Commit bb932ae

Browse files
committed
Node.js compatibility for module and jasmine tests
Also added travis.ci build for autotesting package (Do not forget to change address for icon in README.md and create account there 😄) Closes stretchr#4
1 parent 608cec2 commit bb932ae

File tree

8 files changed

+187
-86
lines changed

8 files changed

+187
-86
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
release
1+
release
2+
node_modules
3+
build

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
language: node_js
2+
node_js:
3+
- "0.11"
4+
- "0.10"
5+
- "0.8"
6+
before_script:
7+
- npm install -g grunt-cli

Gruntfile.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module.exports = function(grunt) {
2+
3+
grunt.loadNpmTasks('grunt-contrib-jshint');
4+
grunt.loadNpmTasks('grunt-jasmine-node');
5+
grunt.loadNpmTasks('grunt-contrib-watch');
6+
7+
grunt.initConfig({
8+
jshint: {
9+
all: ['Gruntfile.js', 'lib/**/*.js', 'test/spec/**/*.js']
10+
},
11+
jasmine_node: {
12+
},
13+
watch: {
14+
all: {
15+
files: ['src/**/*', 'tests/**/*'],
16+
tasks: ['default'],
17+
options: {
18+
atBegin: true,
19+
interrupt: true
20+
}
21+
}
22+
}
23+
});
24+
25+
grunt.registerTask('default', ['jshint', 'jasmine_node']);
26+
};

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# over.js
22

3+
[![Build Status](https://travis-ci.org/floatdrop/over.js.png)](https://travis-ci.org/floatdrop/over.js)
4+
35
Elegant function overloading in JavaScript.
46

57
## Example

package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "over.js",
3+
"version": "1.1.0",
4+
"description": "Elegant funciton overloading in Javascript",
5+
"main": "src/over.js",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/stretchr/over.js.git"
9+
},
10+
"keywords": [
11+
"overload",
12+
"optional",
13+
"arguments"
14+
],
15+
"author": "Mat Ryer <mat@stretchr.com>",
16+
"license": "MIT",
17+
"readmeFilename": "README.md",
18+
"dependencies": {
19+
20+
},
21+
"devDependencies": {
22+
"grunt": "*",
23+
"grunt-jasmine-node": "*",
24+
"grunt-contrib-jshint": "*",
25+
"grunt-contrib-watch": "*",
26+
"grunt-mocha-test": "*"
27+
},
28+
"scripts": {
29+
"test": "grunt"
30+
}
31+
}

src/over.js

Lines changed: 64 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,59 @@
2525
DEALINGS IN THE SOFTWARE.
2626
2727
*/
28+
2829
(function(global){
2930

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(){
3160
return function(){
3261

3362
// get the mapping
34-
var $map = global.Over.map.apply(this, arguments);
63+
var $map = root.Over.map.apply(this, arguments);
3564

3665
return function(){
3766

3867
for (var i in $map) {
39-
if (global.Over.test($map[i].sig, arguments)) {
68+
if (root.Over.test($map[i].sig, arguments)) {
4069

4170
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) {
4372
// collect all $etc arguments
4473

4574
var args = [];
4675
var etcArr = [];
47-
for (var ietc = 0; ietc < sig.length-1; ietc++) {
76+
var iets;
77+
for (ietc = 0; ietc < sig.length-1; ietc++) {
4878
args.push(arguments[ietc]);
4979
}
50-
for (var ietc = sig.length-1; ietc < arguments.length; ietc++) {
80+
for (ietc = sig.length - 1; ietc < arguments.length; ietc++) {
5181
etcArr.push(arguments[ietc]);
5282
}
5383
args.push(etcArr);
@@ -66,56 +96,56 @@
6696
/**
6797
* MakeOver makes a new Over function (useful for testing).
6898
*/
69-
global.Over = global.MakeOver();
99+
root.Over = root.MakeOver();
70100

71101
/**
72102
* The current version.
73103
*/
74-
global.Over.version = 1.1;
104+
root.Over.version = 1.1;
75105

76106
/**
77107
* The current version as a string.
78108
*/
79-
global.Over.versionString = "v1.1.0";
109+
root.Over.versionString = "v1.1.0";
80110

81111
/**
82112
* An object containing functions that can check individual arguments
83113
* and make decisions on whether they are indeed something or not.
84114
*/
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); },
93123
"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; }
97127
};
98128

99129
// shortcuts
100-
global.Over.is.bool = global.Over.is["boolean"];
130+
root.Over.is.bool = root.Over.is.boolean;
101131

102132
/**
103133
* A special reference object that means whatever the arguments are,
104134
* they're OK.
105135
*/
106-
global.Over.etc = {};
136+
root.Over.etc = {};
107137

108138
/**
109139
* Creates a list of signatures mapped to the handler functions.
110140
*/
111-
global.Over.map = function(){
141+
root.Over.map = function(){
112142

113143
var items = [];
114144

115145
for (var i in arguments) {
116146
var func = arguments[i];
117147
items.push({
118-
"sig": global.Over.signature(func),
148+
"sig": root.Over.signature(func),
119149
"func": func
120150
});
121151
}
@@ -127,7 +157,7 @@
127157
/**
128158
* Checks arguments against a signature array.
129159
*/
130-
global.Over.test = function(sig, args){
160+
root.Over.test = function(sig, args){
131161

132162
for (var i = 0; i < Math.max(sig.length, args.length); i++) {
133163

@@ -149,7 +179,7 @@
149179

150180
if (result === false) {
151181
return false;
152-
} else if (result === global.Over.etc) {
182+
} else if (result === root.Over.etc) {
153183
return true;
154184
}
155185

@@ -162,15 +192,15 @@
162192
* Gets an array of is methods to be called to test a
163193
* method call, based on the specified function.
164194
*/
165-
global.Over.signature = function(f){
195+
root.Over.signature = function(f){
166196

167197
var sig = [];
168-
var args = global.Over.argnames(f);
198+
var args = root.Over.argnames(f);
169199
for (var argI in args) {
170200
var arg = args[argI];
171-
var checker = global.Over.is[global.Over.checkFuncFromArg(arg)];
201+
var checker = root.Over.is[root.Over.checkFuncFromArg(arg)];
172202
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){};");
174204
}
175205
sig.push(checker);
176206
}
@@ -182,7 +212,7 @@
182212
/**
183213
* Gets the names of all
184214
*/
185-
global.Over.argnames = function(f){
215+
root.Over.argnames = function(f){
186216
var names = f.toString().split("(")[1].split(")")[0].split(",");
187217
for (var i in names) names[i] = names[i].replace(/^\s+|\s+$/g, '');
188218
return names;
@@ -191,8 +221,8 @@
191221
/**
192222
* Gets the name of the checker func from an argument.r
193223
*/
194-
global.Over.checkFuncFromArg = function(arg){
224+
root.Over.checkFuncFromArg = function(arg){
195225
return arg.split("$")[1];
196-
}
226+
};
197227

198-
})(window);
228+
}).call(this);

test/SpecRunner.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="../src/over.js"></script>
1414

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

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

0 commit comments

Comments
 (0)