-
Notifications
You must be signed in to change notification settings - Fork 0
/
readme-example.spec.ts
52 lines (38 loc) · 1.28 KB
/
readme-example.spec.ts
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
import { describe, it, expect } from "vitest";
import { BasedArray } from "../src";
const funkyReduce = (arr: number[]) => {
return arr.reduce((accum, item, index) => {
return accum + item * index;
}, 0);
};
const funkyReduceVanillaOffset = (arr: number[]) => {
return arr.reduce((accum, item, index) => {
return accum + item * (index + 1); // NOTE: plus one because vanilla
}, 0);
};
describe("README example", () => {
it("works with BasedArray", () => {
let array = new BasedArray<number>(5).fill(1);
let result = funkyReduce(array);
expect(result).toBe(15);
array = BasedArray.from([1, 2, 3, 4, 5]);
result = funkyReduce(array);
expect(result).toBe(55);
});
it("works with vanilla", () => {
let array = new Array<number>(5).fill(1);
let result = funkyReduce(array);
expect(result).toBe(10);
array = Array.from([1, 2, 3, 4, 5]);
result = funkyReduce(array);
expect(result).toBe(40);
});
it("works with vanilla and is the same as based if you do more work", () => {
let array = new Array<number>(5).fill(1);
let result = funkyReduceVanillaOffset(array);
expect(result).toBe(15);
array = Array.from([1, 2, 3, 4, 5]);
result = funkyReduceVanillaOffset(array);
expect(result).toBe(55);
});
});