Skip to content

Commit

Permalink
Colocate all library code browserable teep.js
Browse files Browse the repository at this point in the history
  • Loading branch information
earldouglas committed Jun 11, 2015
1 parent 5e6a427 commit 8a76189
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 191 deletions.
12 changes: 0 additions & 12 deletions lib/array.js

This file was deleted.

61 changes: 0 additions & 61 deletions lib/function.js

This file was deleted.

33 changes: 0 additions & 33 deletions lib/list.js

This file was deleted.

23 changes: 0 additions & 23 deletions lib/option.js

This file was deleted.

16 changes: 0 additions & 16 deletions lib/promise.js

This file was deleted.

29 changes: 0 additions & 29 deletions lib/validation.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"scripts": {
"test": "nodeunit test",
"coveralls": "jscoverage lib && LIB_COV=1 nodeunit --reporter=lcov test | coveralls"
"coveralls": "jscoverage teep.js teep.js && nodeunit --reporter=lcov test | coveralls"
},
"devDependencies": {
"bluebird": "2.2.2",
Expand Down
191 changes: 175 additions & 16 deletions teep.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,178 @@

'use strict';

// Use instrumented code for code coverage tests
var lib = process.env.LIB_COV ? 'lib-cov' : 'lib';

var option = require('./' + lib + '/option');
var validation = require('./' + lib + '/validation');
var list = require('./' + lib + '/list');
var promise = require('./' + lib + '/promise');
var fn = require('./' + lib + '/function');
var array = require('./' + lib + '/array');

exports.option = option;
exports.validation = validation;
exports.list = list;
exports.promise = promise;
exports.fn = fn;
exports.array = array;
var edc;
(function (edc) {

var array = {
contains: function (xs, x) {
for (var i = 0; i < xs.length; i++) {
if (xs[i] === x) {
return true;
}
}
return false;
},
};

var fn = {
compose: function (f, g) {
return function (x) {
return f(g(x));
};
},
curry: function (f, args) {
if (!args || !args.length) {
args = [];
}
return function(x) {
args.push(x);
if (f.length === args.length) {
return f.apply(null, args);
} else {
return fn.curry(f, args);
}
};
},
memoize: function (f, cache) {
if (!cache) {
cache = {
get: function(k) { return cache[k]; },
put: function(k,v) { cache[k] = v; },
};
}
return function () {
var key = (arguments.length === 0) ?
arguments[0] : JSON.stringify(arguments);
var val = cache.get(key);
if (val === undefined) {
val = f.apply(undefined, arguments);
cache.put(key, val);
return val;
} else {
return val;
}
};
},
lazy: function (f, cache) {
var fMemo = fn.memoize(f, cache);
return function() {
var args = arguments;
return {
get: function() { return fMemo.apply(undefined, args); }
};
};
},
};

var option = function (value) {
if (value || value === false || value === 0 || value === '') {
return {
empty : false,
map : function(f) { return option(f(value)); },
flatMap : function(f) { return f(value); },
ap : function(a) { return a.map(value); },
toString : function() { return 'some(' + value.toString() + ')'; }
};
} else {
return {
empty : true,
map : function() { return this; },
flatMap : function() { return this; },
ap : function() { return this; },
toString : function() { return 'none()'; }
};
}
};

var validation = {
valid: function (value) {
return {
valid : true,
value : value,
map : function(f) { return validation.valid(f(value)); },
flatMap : function(f) { return f(value); },
ap : function(a) { return a.map(value); },
toString : function() { return 'valid(' + value.toString() + ')'; }
};
},
invalid: function (errors) {
var self = {
valid : false,
errors : errors,
map : function() { return self; },
flatMap : function() { return self; },
ap : function(a) {
if (a.valid) {
return self;
} else {
return validation.invalid(errors.concat(a.errors));
}
},
toString : function() { return 'invalid(' + errors.toString() + ')'; }
};
return self;
},
};

var list = function (head, tail) {
var _nil = {
length : 0,
map : function() { return this; },
flatMap : function() { return this; },
concat : function(l) { return l; },
toString : function() { return 'nil'; }
};
var _cons = function (head, tail) {
return {
head : head,
tail : tail,
length : 1 + tail.length,
map : function(f) { return _cons(f(head), tail.map(f)); },
flatMap : function(f) { return f(head).concat(tail.flatMap(f)); },
concat : function(l) { return _cons(head, tail.concat(l)); },
toString : function() {
return 'cons(' + head + ', ' + tail.toString() + ')';
}
};
};

if (tail === undefined || tail === null) {
return list(head, _nil);
} else if (head === undefined || head === null) {
return tail;
} else {
return _cons(head, tail);
}
};

var promise = {
collect: function (promises, callback) {
var f = fn.curry(callback);
var p = promises.reduce(function (p1, p2) {
return p1.then(function (r1) {
f = f(r1);
return (p2 instanceof Function) ? p2() : p2;
});
});
return p.then(function (r) { return f(r); });
}
};

edc.teep = {
array: array,
fn: fn,
option: option,
validation: validation,
list: list,
promise: promise,
};

for (var i in edc.teep) {
if (edc.teep.hasOwnProperty(i)) {
exports[i] = edc.teep[i];
}
}

})(edc || (edc = {}));

0 comments on commit 8a76189

Please sign in to comment.