<commit_before>var expect = require("chai").expect;

var _ = require("lodash");
var fp = require("functional-pipeline");

var triple = function(x) {
  return 3 * x;
};
var add2 = function(x) {
  return x + 12;
};
var data = {
  age: 10,
  getAge: function() {
    return this.age;
  }
};

describe("basic pipeline", function() {
  it("doesn't look good inside-out", function() {
    var pipeline = function(obj) {
      return add2(triple(obj.getAge()));
    };
    var result = pipeline(data);
    expect(result).to.equal(42);
  });

  it("works with lodash flow", function() {
    var getAge = function(obj) { return obj.getAge(); };
    var pipeline = _.flow(getAge, triple, add2);

    var result = pipeline(data);
    expect(result).to.equal(42);
  });
});

describe("context pipeline", function() {
  var ctx = function(extract, fn, reinsert) {
    return function(context) {
      context[reinsert] = fn(context[extract]);
      return context;
    };
  };

  it("passes a context property to a simple function and puts the result back", function() {
    var context = {
      duck: "quack",
      value: 32
    };

    var simple = function(x) {
      return x + 10;
    };

    var pipeline = ctx("value", simple, "result");
    context = pipeline(context);
    expect(context.value).to.equal(32);
    expect(context.result).to.equal(42);
  });
});<commit_msg>More pipeline scenarios.<commit_after>var expect = require("chai").expect;

var _ = require("lodash");
var fp = require("functional-pipeline");

var triple = function(x) {
  return 3 * x;
};
var add12 = function(x) {
  return x + 12;
};
var data = {
  age: 10,
  getAge: function() {
    return this.age;
  }
};

describe("basic pipeline", function() {
  it("doesn't look good inside-out", function() {
    var pipeline = function(obj) {
      return add12(triple(obj.getAge()));
    };
    var result = pipeline(data);
    expect(result).to.equal(42);
  });

  it("works with lodash flow", function() {
    var getAgeFn = function(obj) { return obj.getAge(); };
    var pipeline = _.flow(getAgeFn, triple, add12);

    var result = pipeline(data);
    expect(result).to.equal(42);
  });

  it("works with functional-pipeline - method", function() {
    var pipeline = fp("getAge", triple, add12);

    var result = pipeline(data);
    expect(result).to.equal(42);
  });

  it("works with functional-pipeline - property", function() {
    var pipeline = fp("age", triple, add12);

    var result = pipeline(data);
    expect(result).to.equal(42);
  });
});

var ctx = function(extract, fn, reinsert) {
  return function(context) {
    context[reinsert] = fn(context[extract]);
    return context;
  };
};

var ctxR = function(fn, reinsert) {
  return function(context) {
    context[reinsert] = fn(context);
    return context;
  };
};

var context = {
  duck: "quack",
  value: 32
};

var simple = function(x) {
  return x + 10;
};

describe("context pipeline", function() {
  it("passes a context property to a simple function and puts the result back", function() {
    var pipeline = ctx("value", simple, "result");
    var output = pipeline(context);
    expect(output.value).to.equal(32);
    expect(output.result).to.equal(42);
  });

  it("works within a pipeline", function() {
    var innerPipeline = fp("value", triple, add12);
    var pipeline = ctxR(innerPipeline, "result");
    var output = pipeline(context);
    expect(output.result).to.equal(108);
  });

  it("would work with an intermediate step", function() {
    var pipeline = fp("value", triple, add12);
    var output = {result: pipeline(context)};
    expect(output.result).to.equal(108);
  });
});
{'fraction_non_alphanumeric': 0.16579741970903103, 'fraction_numerical': 0.01674444139445512, 'mean_word_length': 3.454767726161369, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1840883', 'n_tokens_mistral': 1221, 'n_tokens_neox': 1150, 'n_words': 302}