Skip to content

Commit

Permalink
feat: make it all testable with tap (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelkaron authored Sep 8, 2016
1 parent 8fe4891 commit bdc891d
Show file tree
Hide file tree
Showing 10 changed files with 513 additions and 113 deletions.
137 changes: 42 additions & 95 deletions compose.js
Original file line number Diff line number Diff line change
@@ -1,113 +1,60 @@
(function(root, factory) {
(function(modules, root, factory) {
if (typeof define === "function" && define.amd) {
define([], factory);
define(modules, factory);
} else if (typeof module === "object" && module.exports) {
module.exports = factory();
module.exports = factory.apply(root, modules.map(require));
} else {
root["mu-compose/compose"] = factory();
}
})(this, function() {
var w = this;
var concat = Array.prototype.concat;
var toString = Object.prototype.toString

function flip(key) {
return {
"key": key,
"value": this[key]
};
}

function transform(data, index) {
var type = toString.call(data);

switch (type) {
case "[object Object]":
if (!data.hasOwnProperty("key")) {
data = Object.keys(data).map(flip, data);
}
break;

default:
data = {
"key": type,
"value": data
}
}

return data;
root["mu-compose/compose"] = factory.apply(root, modules.map(function(m) {
return root[m.replace(/^\./, "mu-compose")];
}));
}
})(["./transform", "./process"], this, function(transform, process) {
var root = this;
var array = Array.prototype;
var slice = array.slice;
var concat = array.concat;

function clean(data) {
return !!data;
}

function compose(composers) {
var config = this;

return function(instance, data, index, array) {
var skip = false;
return function() {
var rules = slice.call(arguments);

return composers.reduce(function(result, composer) {
var composed = skip ? result : composer.call(config, result, data, index, array);
return function() {
var config = this === root ? {} : this;
var result = slice.call(arguments);

if (composed !== undefined && composed !== null) {
if (composed === false) {
skip = true;
} else {
result = composed;
// Flatten & Clean
result = concat.apply(array, result).filter(clean, config);
// Transform
result = result.map(config.transform || transform, config);
// Flatten & Clean
result = concat.apply(array, result).filter(clean, config);
// Process
result = result.reduce(process.apply(config, rules), function Composition() {
var self = this;

(this.constructor.constructors || []).reduce(function(args, c) {
var r = c.apply(self, args);

switch (toString.call(r)) {
case "[object String]":
case "[object Object]":
case "[object Number]":
case "[object Boolean]":
r = [r];
break;

default:
r = r && r.length ? r : args;
}
}

return result;
}, instance);
}
}

function Composition() {
var self = this;

(this.constructor.constructors || []).reduce(function(args, c) {
var result = c.apply(self, args);

switch (toString.call(result)) {
case "[object String]":
case "[object Object]":
case "[object Number]":
case "[object Boolean]":
result = [result];
break;

case "[object Array]":
case "[object Arguments]":
break;

default:
result = args;
}
return r;
}, arguments);
});

return result;
}, arguments);
}

return function() {
var config = this === w ? {} : this;
var composers = concat.apply([], arguments);

return function() {
var _config = this === w ? config : this;
var result = arguments;

// Flatten
result = concat.apply([], result);
// Transform
result = result.map(_config.transform || transform, _config);
// Flatten
result = concat.apply([], result);
// Clean
result = result.filter(_config.clean || clean, _config);
// Compose
return result.reduce(compose.call(_config, composers), _config.composition || Composition);
}
}
});
12 changes: 7 additions & 5 deletions constructor.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
(function(root, factory) {
(function(modules, root, factory) {
if (typeof define === "function" && define.amd) {
define([], factory);
define(modules, factory);
} else if (typeof module === "object" && module.exports) {
module.exports = factory();
module.exports = factory.apply(root, modules.map(require));
} else {
root["mu-compose/constructor"] = factory();
root["mu-compose/constructor"] = factory.apply(root, modules.map(function(m) {
return root[m.replace(/^\./, "mu-compose")];
}));
}
})(this, function() {
})([], this, function() {
return function(result, data) {
var key = data.key;

Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
"name": "mu-compose",
"version": "2.0.0",
"description": "A micro object composer",
"main": "coppose.js",
"main": "compose.js",
"scripts": {
"release": "standard-version"
"release": "standard-version",
"test": "tap test/*.js"
},
"repository": {
"type": "git",
Expand All @@ -17,6 +18,7 @@
},
"homepage": "https://github.com/mu-lib/mu-compose#readme",
"devDependencies": {
"standard-version": "^2.4.0"
"standard-version": "^2.4.0",
"tap": "^7.1.0"
}
}
39 changes: 39 additions & 0 deletions process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
(function(modules, root, factory) {
if (typeof define === "function" && define.amd) {
define(modules, factory);
} else if (typeof module === "object" && module.exports) {
module.exports = factory.apply(root, modules.map(require));
} else {
root["mu-compose/process"] = factory.apply(root, modules.map(function(m) {
return root[m.replace(/^\./, "mu-compose")];
}));
}
})([], this, function() {
var slice = Array.prototype.slice;
var concat = Array.prototype.concat;

return function() {
var self = this;
var rules = concat.apply([], arguments);

return function(input) {
var skip = false;
var args = slice.call(arguments, 1);

return rules.reduce(function(output, rule) {
var composed = skip ? output : rule.apply(self, concat.call([output], args));

if (composed !== undefined) {
if (composed === false) {
skip = true;
}
else {
output = composed;
}
}

return output;
}, input);
}
}
});
12 changes: 7 additions & 5 deletions prototype.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
(function(root, factory) {
(function(modules, root, factory) {
if (typeof define === "function" && define.amd) {
define([], factory);
define(modules, factory);
} else if (typeof module === "object" && module.exports) {
module.exports = factory();
module.exports = factory.apply(root, modules.map(require));
} else {
root["mu-compose/prototype"] = factory();
root["mu-compose/prototype"] = factory.apply(root, modules.map(function(m) {
return root[m.replace(/^\./, "mu-compose")];
}));
}
})(this, function() {
})([], this, function() {
return function(result, data) {
result.prototype[data.key] = data.value;
}
Expand Down
12 changes: 7 additions & 5 deletions regexp.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
(function(root, factory) {
(function(modules, root, factory) {
if (typeof define === "function" && define.amd) {
define([], factory);
define(modules, factory);
} else if (typeof module === "object" && module.exports) {
module.exports = factory();
module.exports = factory.apply(root, modules.map(require));
} else {
root["mu-compose/regexp"] = factory();
root["mu-compose/regexp"] = factory.apply(root, modules.map(function(m) {
return root[m.replace(/^\./, "mu-compose")];
}));
}
})(this, function() {
})([], this, function() {
return function(regexp, callback) {
return function(result, data) {
var matches = data.key.match(regexp);
Expand Down
Loading

0 comments on commit bdc891d

Please sign in to comment.