russplaysguitar / UnderscoreCF

An UnderscoreJS port for Coldfusion. Functional programming library.
http://russplaysguitar.github.com/UnderscoreCF/
MIT License
89 stars 38 forks source link

Add support for Chaining #1

Open russplaysguitar opened 12 years ago

russplaysguitar commented 12 years ago

Need to implement the OOP wrapper to do this.

elpete commented 8 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);
}
russplaysguitar commented 8 years ago

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