1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
import { Cons, ListZipper } from '../lib/index';
describe('types/collections/cons + zipper', () => {
test('Cons.from iterates in order', () => {
const list = Cons.from([1, 2, 3]);
expect(Array.from(list.get())).toEqual([1, 2, 3]);
});
test('Cons.replace replaces head value', () => {
const list = Cons.from([1, 2]).get();
expect(Array.from(list.replace(9))).toEqual([9, 2]);
});
test('Cons.before prepends a head chain', () => {
const tail = Cons.from([2, 3]);
const head = new Cons(1).before(tail);
expect(Array.from(head)).toEqual([1, 2, 3]);
});
test('Cons.addOnto appends onto tail optional', () => {
const tail = Cons.from([3]);
const list = Cons.addOnto([1, 2], tail).get();
expect(Array.from(list)).toEqual([1, 2, 3]);
});
test('ListZipper navigation and edits', () => {
const zipper = ListZipper.from([1, 2, 3]);
expect(zipper.read().get()).toBe(1);
const z2 = zipper.next().get() as ListZipper<number>;
expect(z2.read().get()).toBe(2);
const z3 = z2.replace(9) as ListZipper<number>;
expect(z3.collection()).toEqual([1, 9, 3]);
const z4 = z3.remove() as ListZipper<number>;
expect(z4.collection()).toEqual([1, 3]);
const z5 = z4.prependChunk([7, 8]) as ListZipper<number>;
expect(z5.collection()).toEqual([1, 7, 8, 3]);
const back = (z2.previous().get() as ListZipper<number>).read().get();
expect(back).toBe(1);
});
test('ListZipper iteration yields full list', () => {
const zipper = ListZipper.from([1, 2, 3]);
expect(Array.from(zipper)).toEqual([1, 2, 3]);
const moved = zipper.next().flatMap((z: any) => z.next());
expect(moved.present()).toBe(true);
expect(Array.from(moved.get())).toEqual([1, 2, 3]);
const empty = ListZipper.from<number>([]);
expect(empty.read().present()).toBe(false);
expect(Array.from(empty)).toEqual([]);
});
});
|