Skip to content

Commit b47f36f

Browse files
committed
feat: add isFirst and isLast methods
1 parent 8a44cef commit b47f36f

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ function TodoList() {
125125
<button onClick={() => removeTodo(index)}>Delete</button>
126126
<button
127127
onClick={() => moveTodo(index, 'up')}
128-
disabled={index === 0}
128+
disabled={todos.isFirst(index)}
129129
>
130130
131131
</button>
132132
<button
133133
onClick={() => moveTodo(index, 'down')}
134-
disabled={index === todos.size - 1}
134+
disabled={todos.isLast(index)}
135135
>
136136
137137
</button>

src/index.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,38 @@ export class List<T> implements Iterable<T> {
139139
return resolved < 0 || resolved >= this.size
140140
}
141141

142+
/**
143+
* Checks if the given index corresponds to the first element in the List.
144+
* Does not resolve negative indices.
145+
*
146+
* @example
147+
* ```ts
148+
* const list = List.of(1, 2, 3);
149+
* list.isFirst(0); // true
150+
* list.isFirst(1); // false
151+
* list.isFirst(-1); // false
152+
* ```
153+
*/
154+
isFirst(index: number): boolean {
155+
return !this.isEmpty() && index === 0
156+
}
157+
158+
/**
159+
* Checks if the given index corresponds to the last element in the List.
160+
* Does not resolve negative indices.
161+
*
162+
* @example
163+
* ```ts
164+
* const list = List.of(1, 2, 3);
165+
* list.isLast(2); // true
166+
* list.isLast(1); // false
167+
* list.isLast(-1); // false
168+
* ```
169+
*/
170+
isLast(index: number): boolean {
171+
return index === this.size - 1
172+
}
173+
142174
/**
143175
* Appends items to the end of the List.
144176
*

tests/index.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,44 @@ describe('List', () => {
248248
})
249249
})
250250

251+
describe('isFirst', () => {
252+
it('should return true for index 0', () => {
253+
const list = List.of(1, 2, 3)
254+
expect(list.isFirst(0)).toBe(true)
255+
})
256+
257+
it('should return false for other indices', () => {
258+
const list = List.of(1, 2, 3)
259+
expect(list.isFirst(1)).toBe(false)
260+
expect(list.isFirst(2)).toBe(false)
261+
expect(list.isFirst(-1)).toBe(false)
262+
})
263+
264+
it('should handle empty list', () => {
265+
const list = List.empty()
266+
expect(list.isFirst(0)).toBe(false)
267+
})
268+
})
269+
270+
describe('isLast', () => {
271+
it('should return true for last index', () => {
272+
const list = List.of(1, 2, 3)
273+
expect(list.isLast(2)).toBe(true)
274+
})
275+
276+
it('should return false for other indices', () => {
277+
const list = List.of(1, 2, 3)
278+
expect(list.isLast(0)).toBe(false)
279+
expect(list.isLast(1)).toBe(false)
280+
expect(list.isLast(-1)).toBe(false)
281+
})
282+
283+
it('should handle empty list', () => {
284+
const list = List.empty()
285+
expect(list.isLast(0)).toBe(false)
286+
})
287+
})
288+
251289
describe('difference', () => {
252290
it('should return items in first list but not in second', () => {
253291
const list1 = List.of(1, 2, 3, 4)

0 commit comments

Comments
 (0)