functional/iterables.test.ts

47 lines
1.3 KiB
TypeScript
Raw Permalink Normal View History

2021-08-26 23:01:08 +00:00
import { expect, it } from "./deps.testing.ts";
2020-05-11 22:34:57 +00:00
import { at, first, fmap, last, second, third } from "./iterables.ts";
2020-12-02 20:58:26 +00:00
it("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-12-02 20:58:26 +00:00
it("first", () => {
2020-05-13 09:46:10 +00:00
expect(first([1, 4, 8])).toBe(1);
expect(first([1])).toBe(1);
expect(first([])).toBeUndefined();
2020-05-11 22:34:57 +00:00
});
2020-12-02 20:58:26 +00:00
it("second", () => {
2020-05-13 09:46:10 +00:00
expect(second([1, 4, 8])).toBe(4);
expect(second([1])).toBeUndefined();
expect(second([])).toBeUndefined();
2020-05-11 22:34:57 +00:00
});
2020-12-02 20:58:26 +00:00
it("third", () => {
2020-05-13 09:46:10 +00:00
expect(third([1, 4, 8])).toBe(8);
expect(third([1, 4])).toBeUndefined();
expect(third([])).toBeUndefined();
2020-05-11 22:34:57 +00:00
});
2020-12-02 20:58:26 +00:00
it("last", () => {
2020-05-13 09:46:10 +00:00
expect(last([1, 4, 8])).toBe(8);
expect(last([1])).toBe(1);
expect(last([])).toBeUndefined();
2020-05-11 22:34:57 +00:00
});
2020-12-02 20:58:26 +00:00
it("fmap", () => {
2020-05-13 09:46:10 +00:00
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],
);
});