| 1 | import { assert, describe, it } from "@effect/vitest" |
| 2 | import { Array as Arr, Option } from "effect" |
| 3 | |
| 4 | const input = [1, 2, 3] |
| 5 | |
| 6 | describe("Array NaN indexes", () => { |
| 7 | it("rejects NaN in get", () => { |
| 8 | assert.deepStrictEqual(Arr.get(input, Number.NaN), Option.none()) |
| 9 | }) |
| 10 | |
| 11 | it("rejects NaN in getUnsafe", () => { |
| 12 | assert.throws(() => Arr.getUnsafe(input, Number.NaN), /Index out of bounds/) |
| 13 | }) |
| 14 | |
| 15 | it("rejects NaN in insertAt", () => { |
| 16 | assert.deepStrictEqual(Arr.insertAt(input, Number.NaN, 4), Option.none()) |
| 17 | }) |
| 18 | |
| 19 | it("rejects NaN in replace", () => { |
| 20 | assert.deepStrictEqual(Arr.replace(input, Number.NaN, 4), Option.none()) |
| 21 | }) |
| 22 | |
| 23 | it("rejects NaN in modify", () => { |
| 24 | assert.deepStrictEqual(Arr.modify(input, Number.NaN, (n) => n * 2), Option.none()) |
| 25 | }) |
| 26 | |
| 27 | it("treats removing NaN as an out-of-bounds no-op", () => { |
| 28 | assert.deepStrictEqual(Arr.remove(input, Number.NaN), input) |
| 29 | }) |
| 30 | }) |
| 31 | |
| 32 | describe("Array fractional indexes", () => { |
| 33 | it("floors the index in replace", () => { |
| 34 | assert.deepStrictEqual(Arr.replace(input, 1.5, 4), Option.some([1, 4, 3])) |
| 35 | }) |
| 36 | |
| 37 | it("floors the index in modify", () => { |
| 38 | assert.deepStrictEqual(Arr.modify(input, 1.5, (n) => n * 2), Option.some([1, 4, 3])) |
| 39 | }) |
| 40 | }) |