UNPKG

ts-prime

Version:

A utility library for JavaScript and Typescript.

58 lines (57 loc) 2.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var first_1 = require("./first"); var pipe_1 = require("./pipe"); var _counter_1 = require("./_counter"); var filter_1 = require("./filter"); function defaultTo(d) { return function (v) { return v == null ? d : v; }; } test('should return last', function () { expect(first_1.first([1, 2, 3])).toEqual(1); }); test('empty array', function () { expect(first_1.first([])).toEqual(undefined); }); describe('pipe', function () { test('as no-fn', function () { var counter = _counter_1.createCounter(); var result = pipe_1.pipe([1, 2, 3, 4, 5, 6], counter.fn(), first_1.first, function (x) { return x; }); expect(counter.count).toHaveBeenCalledTimes(1); expect(result).toEqual(1); }); test('as fn', function () { var counter = _counter_1.createCounter(); var result = pipe_1.pipe([1, 2, 3, 4, 5, 6], counter.fn(), first_1.first()); expect(counter.count).toHaveBeenCalledTimes(1); expect(result).toEqual(1); }); test('with filter', function () { var counter = _counter_1.createCounter(); var result = pipe_1.pipe([1, 2, 4, 8, 16], counter.fn(), filter_1.filter(function (x) { return x > 3; }), first_1.first(), defaultTo(0), function (x) { return x + 1; }); expect(counter.count).toHaveBeenCalledTimes(3); expect(result).toEqual(5); }); test('empty array', function () { var counter = _counter_1.createCounter(); var result = pipe_1.pipe([], counter.fn(), first_1.first()); expect(counter.count).toHaveBeenCalledTimes(0); expect(result).toEqual(undefined); }); test('2 x first()', function () { var counter = _counter_1.createCounter(); var result = pipe_1.pipe([[1, 2, 3], [4, 5], [6]], counter.fn(), first_1.first(), defaultTo([]), first_1.first()); expect(counter.count).toHaveBeenCalledTimes(1); expect(result).toEqual(1); }); test('complex', function () { var counter1 = _counter_1.createCounter(); var counter2 = _counter_1.createCounter(); var result = pipe_1.pipe([[1, 2, 3], [1], [4, 5, 6, 7], [1, 2, 3, 4]], counter1.fn(), filter_1.filter(function (arr) { return arr.length === 4; }), first_1.first(), defaultTo([]), counter2.fn(), filter_1.filter(function (x) { return x % 2 === 1; }), first_1.first()); expect(counter1.count).toHaveBeenCalledTimes(3); expect(counter2.count).toHaveBeenCalledTimes(2); expect(result).toEqual(5); }); });