Open russplaysguitar opened 12 years ago
What about something like this:
_.wrap([1, 2, 3, 4]).map(function(item) {
return item * 2;
}).reduce(function(acc, item) {
return acc + item;
}, 0);
writeDump(arr.getResult()); // 20
And something like this:
component {
public UnderscoreWrapper function init(obj, underscoreInstance) {
if (! StructKeyExists(arguments, '_')) {
arguments._ = new modules.underscorecf.Underscore();
}
variables._ = arguments._;
variables.obj = arguments.obj;
return this;
}
public any function getResult() {
return variables.obj;
}
function onMissingMethod(string missingMethodName, struct missingMethodArguments) {
var method = variables._[arguments.missingMethodName];
ArrayPrepend(arguments.missingMethodArguments, variables.obj);
variables.obj = method(argumentCollection = arguments.missingMethodArguments);
return this;
}
}
And adding this to Underscore.cfc:
public models.UnderscoreWrapper function chain(obj = {}) {
return new models.UnderscoreWrapper(obj, this);
}
I'm open to any solution to this as long as it passes basically all of the same tests that Underscore.js does: https://github.com/jashkenas/underscore/blob/master/test/chaining.js
Need to implement the OOP wrapper to do this.