functional/iterables.test.ts

47 lines
1.3 KiB
TypeScript

import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
import { at, first, fmap, last, second, third } from "./iterables.ts";
Deno.test("at", () => {
const second = at(1);
assertEquals(second([1, 4, 8]), 4);
assertEquals(second([1]), undefined);
const second_from_end = at(-2);
assertEquals(second_from_end([1, 4, 6, 8]), 6);
assertEquals(second_from_end([1]), undefined);
});
Deno.test("first", () => {
assertEquals(first([1, 4, 8]), 1);
assertEquals(first([1]), 1);
assertEquals(first([]), undefined);
});
Deno.test("second", () => {
assertEquals(second([1, 4, 8]), 4);
assertEquals(second([1]), undefined);
assertEquals(second([]), undefined);
});
Deno.test("third", () => {
assertEquals(third([1, 4, 8]), 8);
assertEquals(third([1, 4]), undefined);
assertEquals(third([]), undefined);
});
Deno.test("last", () => {
assertEquals(last([1, 4, 8]), 8);
assertEquals(last([1]), 1);
assertEquals(last([]), undefined);
});
Deno.test("fmap", () => {
assertEquals(fmap()([1, 2, 3]), [1, 2, 3]);
assertEquals(fmap((x: number) => x * 2)([1, 2, 3]), [2, 4, 6]);
assertEquals(fmap(undefined, (x: number) => x % 2 == 0)([1, 2, 3]), [2]);
assertEquals(fmap((x: number) => x * 2)([1, 2, 3]), [2, 4, 6]);
assertEquals(
fmap((x: number) => x * 2, (x: number) => x < 5)([1, 2, 3]),
[2, 4],
);
});