UNPKG

ts-prime

Version:

A utility library for JavaScript and Typescript.

56 lines (55 loc) 2.3 kB
import { first } from './first'; import { pipe } from './pipe'; import { createCounter } from './_counter'; import { filter } from './filter'; function defaultTo(d) { return function (v) { return v == null ? d : v; }; } test('should return last', function () { expect(first([1, 2, 3])).toEqual(1); }); test('empty array', function () { expect(first([])).toEqual(undefined); }); describe('pipe', function () { test('as no-fn', function () { var counter = createCounter(); var result = pipe([1, 2, 3, 4, 5, 6], counter.fn(), first, function (x) { return x; }); expect(counter.count).toHaveBeenCalledTimes(1); expect(result).toEqual(1); }); test('as fn', function () { var counter = createCounter(); var result = pipe([1, 2, 3, 4, 5, 6], counter.fn(), first()); expect(counter.count).toHaveBeenCalledTimes(1); expect(result).toEqual(1); }); test('with filter', function () { var counter = createCounter(); var result = pipe([1, 2, 4, 8, 16], counter.fn(), filter(function (x) { return x > 3; }), first(), defaultTo(0), function (x) { return x + 1; }); expect(counter.count).toHaveBeenCalledTimes(3); expect(result).toEqual(5); }); test('empty array', function () { var counter = createCounter(); var result = pipe([], counter.fn(), first()); expect(counter.count).toHaveBeenCalledTimes(0); expect(result).toEqual(undefined); }); test('2 x first()', function () { var counter = createCounter(); var result = pipe([[1, 2, 3], [4, 5], [6]], counter.fn(), first(), defaultTo([]), first()); expect(counter.count).toHaveBeenCalledTimes(1); expect(result).toEqual(1); }); test('complex', function () { var counter1 = createCounter(); var counter2 = createCounter(); var result = pipe([[1, 2, 3], [1], [4, 5, 6, 7], [1, 2, 3, 4]], counter1.fn(), filter(function (arr) { return arr.length === 4; }), first(), defaultTo([]), counter2.fn(), filter(function (x) { return x % 2 === 1; }), first()); expect(counter1.count).toHaveBeenCalledTimes(3); expect(counter2.count).toHaveBeenCalledTimes(2); expect(result).toEqual(5); }); });