import { expect, test } from "./deps.test.ts"; import { at, first, fmap, last, second, third } from "./iterables.ts"; test("at", () => { const second = at(1); expect(second([1, 4, 8])).toBe(4); expect(second([1])).toBeUndefined(); const second_from_end = at(-2); expect(second_from_end([1, 4, 6, 8])).toBe(6); expect(second_from_end([1])).toBeUndefined(); }); test("first", () => { expect(first([1, 4, 8])).toBe(1); expect(first([1])).toBe(1); expect(first([])).toBeUndefined(); }); test("second", () => { expect(second([1, 4, 8])).toBe(4); expect(second([1])).toBeUndefined(); expect(second([])).toBeUndefined(); }); test("third", () => { expect(third([1, 4, 8])).toBe(8); expect(third([1, 4])).toBeUndefined(); expect(third([])).toBeUndefined(); }); test("last", () => { expect(last([1, 4, 8])).toBe(8); expect(last([1])).toBe(1); expect(last([])).toBeUndefined(); }); test("fmap", () => { expect(fmap()([1, 2, 3])).toEqual([1, 2, 3]); expect(fmap((x: number) => x * 2)([1, 2, 3])).toEqual([2, 4, 6]); expect(fmap(undefined, (x: number) => x % 2 == 0)([1, 2, 3])).toEqual([2]); expect(fmap((x: number) => x * 2)([1, 2, 3])).toEqual([2, 4, 6]); expect(fmap((x: number) => x * 2, (x: number) => x < 5)([1, 2, 3])).toEqual( [2, 4], ); });