functional/iterables.test.ts

47 lines
1.3 KiB
TypeScript
Raw Normal View History

2020-05-13 09:46:10 +00:00
import { expect, test } from "./deps.test.ts";
2020-05-11 22:34:57 +00:00
import { at, first, fmap, last, second, third } from "./iterables.ts";
2020-05-13 09:46:10 +00:00
test("at", () => {
2020-05-11 22:34:57 +00:00
const second = at(1);
2020-05-13 09:46:10 +00:00
expect(second([1, 4, 8])).toBe(4);
expect(second([1])).toBeUndefined();
2020-05-11 22:34:57 +00:00
const second_from_end = at(-2);
2020-05-13 09:46:10 +00:00
expect(second_from_end([1, 4, 6, 8])).toBe(6);
expect(second_from_end([1])).toBeUndefined();
2020-05-11 22:34:57 +00:00
});
2020-05-13 09:46:10 +00:00
test("first", () => {
expect(first([1, 4, 8])).toBe(1);
expect(first([1])).toBe(1);
expect(first([])).toBeUndefined();
2020-05-11 22:34:57 +00:00
});
2020-05-13 09:46:10 +00:00
test("second", () => {
expect(second([1, 4, 8])).toBe(4);
expect(second([1])).toBeUndefined();
expect(second([])).toBeUndefined();
2020-05-11 22:34:57 +00:00
});
2020-05-13 09:46:10 +00:00
test("third", () => {
expect(third([1, 4, 8])).toBe(8);
expect(third([1, 4])).toBeUndefined();
expect(third([])).toBeUndefined();
2020-05-11 22:34:57 +00:00
});
2020-05-13 09:46:10 +00:00
test("last", () => {
expect(last([1, 4, 8])).toBe(8);
expect(last([1])).toBe(1);
expect(last([])).toBeUndefined();
2020-05-11 22:34:57 +00:00
});
2020-05-13 09:46:10 +00:00
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(
2020-05-11 22:34:57 +00:00
[2, 4],
);
});