munge.js 857 B

123456789101112131415161718192021222324252627282930313233
  1. var PipelineCommand = require('./commands/PipelineCommand');
  2. var Munger = (function(){
  3. // CONSTRUCTOR
  4. var base = Object, proto, klass = function Munger(ops){
  5. if (!ops){
  6. throw new Error("munge requires a pipeline!");
  7. }
  8. if (typeof ops.length !== "number"){
  9. ops = [ops];
  10. }
  11. this.pipeline = new PipelineCommand(ops);
  12. };
  13. proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  14. // PROTOTYPE MEMBERS
  15. proto.execute = function execute(inputs){
  16. var result = {};
  17. result.ok = this.pipeline.run(inputs, result);
  18. //return result; //TODO: figure out if we want mongo style result or a simpler one.
  19. return result.result;
  20. };
  21. return klass;
  22. })();
  23. module.exports = function munge(ops, inputs) {
  24. var munger = new Munger(ops);
  25. if(inputs)
  26. return munger.execute(inputs);
  27. return munger.execute.bind(munger);
  28. };