{"size": 1618, "ext": "js", "max_stars_repo_path": "src/core/__tests__/compose.spec.js", "max_stars_repo_name": "ascartabelli/lamb", "max_stars_repo_stars_event_min_datetime": "2015-04-10T13:58:27.000Z", "max_stars_repo_stars_event_max_datetime": "2021-09-23T19:37:48.000Z", "max_issues_repo_path": "src/core/__tests__/compose.spec.js", "max_issues_repo_name": "ascartabelli/lamb", "max_issues_repo_issues_event_min_datetime": "2016-09-03T05:58:05.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-28T22:35:17.000Z", "max_forks_repo_path": "src/core/__tests__/compose.spec.js", "max_forks_repo_name": "ascartabelli/lamb", "max_forks_repo_forks_event_min_datetime": "2015-04-10T10:22:08.000Z", "max_forks_repo_forks_event_max_datetime": "2018-02-06T20:45:56.000Z"}
{"max_stars_count": 19, "max_issues_count": 8, "max_forks_count": 7, "avg_line_length": 36.7727272727, "max_line_length": 166, "alphanum_fraction": 0.6291718171}
import * as lamb from "../..";
import { nonFunctions } from "../../__tests__/commons";

describe("compose", function () {
    var double = function (n) { return n * 2; };
    var cube = function (n) { return Math.pow(n, 3); };
    var changeSign = function (n) { return -n; };

    var cubeAndDouble = lamb.compose(double, cube);
    var doubleAndCube = lamb.compose(cube, double);

    it("should return a function that is the composition of the given functions; the first one consuming the return value of the function that follows", function () {
        expect(cubeAndDouble(5)).toBe(250);
        expect(doubleAndCube(5)).toBe(1000);
    });

    it("should be possible to reuse composed functions", function () {
        var changeSignCubeAndDouble = lamb.compose(cubeAndDouble, changeSign);

        expect(changeSignCubeAndDouble(2)).toBe(-16);
    });

    it("should behave like `identity` if no functions are passed", function () {
        var obj = {};

        expect(lamb.compose()(obj)).toBe(obj);
        expect(lamb.compose()()).toBeUndefined();
        expect(lamb.compose()(2, 3, 4)).toBe(2);
    });

    it("should ignore extra arguments", function () {
        expect(lamb.compose(double, cube, changeSign)(5)).toBe(250);
    });

    it("should build a function throwing an exception if any parameter is not a function", function () {
        nonFunctions.forEach(function (value) {
            expect(lamb.compose(lamb.identity, value)).toThrow();
            expect(lamb.compose(value, lamb.identity)).toThrow();
        });

        expect(lamb.compose(lamb.identity)).toThrow();
    });
});
