|
9 | 9 | lastInArray, |
10 | 10 | mapArray, |
11 | 11 | partitionArray, |
| 12 | + popArray, |
12 | 13 | prependToArray, |
13 | 14 | shiftArray, |
14 | 15 | type NonEmptyArray, |
@@ -398,4 +399,38 @@ describe("Mutations", () => { |
398 | 399 | shiftArray([1, 2, 3] as NonEmptyReadonlyArray<number>); |
399 | 400 | }); |
400 | 401 | }); |
| 402 | + |
| 403 | + describe("popArray", () => { |
| 404 | + test("pops last element from array", () => { |
| 405 | + const arr: NonEmptyArray<number> = [1, 2, 3]; |
| 406 | + const result = popArray(arr); |
| 407 | + expect(result).toBe(3); |
| 408 | + expect(arr).toEqual([1, 2]); |
| 409 | + }); |
| 410 | + |
| 411 | + test("pops from single element array", () => { |
| 412 | + const arr: NonEmptyArray<number> = [42]; |
| 413 | + const result = popArray(arr); |
| 414 | + expect(result).toBe(42); |
| 415 | + expect(arr).toEqual([]); |
| 416 | + }); |
| 417 | + |
| 418 | + test("mutates the original array", () => { |
| 419 | + const arr: NonEmptyArray<string> = ["a", "b", "c"]; |
| 420 | + popArray(arr); |
| 421 | + expect(arr).toEqual(["a", "b"]); |
| 422 | + }); |
| 423 | + |
| 424 | + test("only accepts mutable arrays", () => { |
| 425 | + const mutableArr: NonEmptyArray<number> = [1, 2, 3]; |
| 426 | + const result = popArray(mutableArr); |
| 427 | + expect(result).toBe(3); |
| 428 | + expect(mutableArr).toEqual([1, 2]); |
| 429 | + expectTypeOf(result).toEqualTypeOf<number>(); |
| 430 | + |
| 431 | + // Verify readonly arrays are NOT accepted by TypeScript |
| 432 | + // @ts-expect-error - readonly arrays cannot be mutated |
| 433 | + popArray([1, 2, 3] as NonEmptyReadonlyArray<number>); |
| 434 | + }); |
| 435 | + }); |
401 | 436 | }); |
0 commit comments