Repository navigation
Expand file tree
/
Copy patharray.test.ts
More file actions
91 lines (80 loc) · 2.75 KB
/
Copy patharray.test.ts
File metadata and controls
91 lines (80 loc) · 2.75 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { describe, it } from "mocha";
import "should";
import { findLast, findLastIndex } from "./array";
describe("Array Utilities", () => {
describe("findLastIndex", () => {
it("should find last matching element's index", () => {
const array = [1, 2, 3, 2, 1];
const index = findLastIndex(array, (x) => x === 2);
index.should.equal(3); // last '2' is at index 3
});
it("should return -1 when no element matches", () => {
const array = [1, 2, 3];
const index = findLastIndex(array, (x) => x === 4);
index.should.equal(-1);
});
it("should handle empty arrays", () => {
const array: number[] = [];
const index = findLastIndex(array, (x) => x === 1);
index.should.equal(-1);
});
it("should work with different types", () => {
const array = ["a", "b", "c", "b", "a"];
const index = findLastIndex(array, (x) => x === "b");
index.should.equal(3);
});
it("should provide correct index in predicate", () => {
const array = [1, 2, 3];
const indices: number[] = [];
findLastIndex(array, (_, index) => {
indices.push(index);
return false;
});
indices.should.deepEqual([2, 1, 0]); // Should iterate in reverse
});
it("should provide array reference in predicate", () => {
const array = [1, 2, 3];
findLastIndex(array, (_, __, arr) => {
arr.should.equal(array); // Should pass original array
return false;
});
});
});
describe("findLast", () => {
it("should find last matching element", () => {
const array = [1, 2, 3, 2, 1];
const element = findLast(array, (x) => x === 2);
should(element).not.be.undefined();
element?.should.equal(2);
});
it("should return undefined when no element matches", () => {
const array = [1, 2, 3];
const element = findLast(array, (x) => x === 4);
should(element).be.undefined();
});
it("should handle empty arrays", () => {
const array: number[] = [];
const element = findLast(array, (x) => x === 1);
should(element).be.undefined();
});
it("should work with object arrays", () => {
const array = [
{ id: 1, value: "a" },
{ id: 2, value: "b" },
{ id: 3, value: "a" },
];
const element = findLast(array, (x) => x.value === "a");
should(element).not.be.undefined();
element?.should.deepEqual({ id: 3, value: "a" });
});
it("should provide correct index in predicate", () => {
const array = [1, 2, 3];
const indices: number[] = [];
findLast(array, (_, index) => {
indices.push(index);
return false;
});
indices.should.deepEqual([2, 1, 0]); // Should iterate in reverse
});
});
});