-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make it all testable with tap (#1)
- Loading branch information
1 parent
8fe4891
commit bdc891d
Showing
10 changed files
with
513 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.