Skip to content

Commit 4321649

Browse files
committed
rename for consistency
1 parent 16ae055 commit 4321649

3 files changed

Lines changed: 26 additions & 26 deletions

File tree

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# rotatable-array
22

3-
Two tiny, zero‑dependency circular rotators for TypeScript / JavaScript:
3+
Three tiny, zero‑dependency circular rotators for TypeScript / JavaScript:
44

55
- `RotatableArray<T>`: an **immutable**, **non‑empty** round‑robin view over a backing array.
6-
- `MutatableRotatableArray<T>`: a **mutable**, **non-empty** round-robin array with add/remove.
6+
- `RotatableMutatableArray<T>`: a **mutable**, **non-empty** round-robin array with add/remove.
77
- `RotatableSet<T>`: a **mutable**, **unique‑item** round‑robin set with O(1) add/remove/next.
88

99
---
@@ -19,7 +19,7 @@ npm i rotatable-array
1919
## Quick Start
2020

2121
```ts
22-
import { MutatableRotatableArray, RotatableArray, RotatableSet } from "rotatable-array";
22+
import { RotatableMutatableArray, RotatableArray, RotatableSet } from "rotatable-array";
2323
2424
const rot = new RotatableArray(["A", "B", "C"]);
2525
console.log(rot.next()); // A
@@ -28,7 +28,7 @@ console.log(rot.next()); // B
2828
console.log(rot.previous()); // B
2929
console.log(rot.move(1)); // C
3030
31-
const mut = new MutatableRotatableArray(["A", "B"]);
31+
const mut = new RotatableMutatableArray(["A", "B"]);
3232
mut.add("C");
3333
mut.removeAt(1);
3434
console.log(mut.next()); // A
@@ -102,7 +102,7 @@ The cursor always points at the item that `next()` will return.
102102
103103
Note: because the iterator is infinite, use `toArray()`, `toSet()`, or `cycle()` for finite snapshots.
104104
105-
### MutatableRotatableArray
105+
### RotatableMutatableArray
106106
107107
Mutable, ordered, circular array. Inherits all `RotatableArray` methods and adds:
108108

src/rotatableArray.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export class RotatableArray<T> extends RotatableArrayBase<T> {
128128
}
129129
}
130130

131-
export class MutatableRotatableArray<T> extends RotatableArrayBase<T> {
131+
export class RotatableMutatableArray<T> extends RotatableArrayBase<T> {
132132
/**
133133
* @param src A **non-empty** array used as backing store.
134134
* @param copy If `true` (default) we clone `src`; otherwise we reference it
@@ -177,7 +177,7 @@ export class MutatableRotatableArray<T> extends RotatableArrayBase<T> {
177177
removeAt(index: number): T {
178178
this.assertRemoveIndex(index);
179179
if (this.length === 1) {
180-
throw new Error("MutatableRotatableArray must contain at least one element");
180+
throw new Error("RotatableMutatableArray must contain at least one element");
181181
}
182182
if (index < this.index) {
183183
this.index -= 1;
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { describe, it, expect } from "vitest";
2-
import { MutatableRotatableArray } from "../src/index";
2+
import { RotatableMutatableArray } from "../src/index";
33

4-
describe("MutatableRotatableArray", () => {
4+
describe("RotatableMutatableArray", () => {
55
it("throws on empty source array", () => {
6-
expect(() => new MutatableRotatableArray([])).toThrowError(/non-empty/i);
6+
expect(() => new RotatableMutatableArray([])).toThrowError(/non-empty/i);
77
});
88

99
it("push/add appends without moving the cursor", () => {
10-
const rot = new MutatableRotatableArray(["A", "B"]);
10+
const rot = new RotatableMutatableArray(["A", "B"]);
1111
rot.setIndex(1); // B
1212
expect(rot.push("C")).toBe(3);
1313
expect(rot.add("D")).toBe(4);
@@ -16,7 +16,7 @@ describe("MutatableRotatableArray", () => {
1616
});
1717

1818
it("next() reflects new items appended mid-rotation", () => {
19-
const rot = new MutatableRotatableArray([1, 2, 3]);
19+
const rot = new RotatableMutatableArray([1, 2, 3]);
2020
expect(rot.next()).toBe(1);
2121
rot.push(4);
2222
rot.push(5);
@@ -28,21 +28,21 @@ describe("MutatableRotatableArray", () => {
2828
});
2929

3030
it("insert() before or at the cursor keeps the same current item", () => {
31-
const rot = new MutatableRotatableArray(["A", "B", "C"]);
31+
const rot = new RotatableMutatableArray(["A", "B", "C"]);
3232
rot.setIndex(1); // B
3333
rot.insert(0, "X");
3434
expect(rot.peek()).toBe("B");
3535
expect([...rot.cycle()]).toEqual(["B", "C", "X", "A"]);
3636

37-
const rot2 = new MutatableRotatableArray(["A", "B", "C"]);
37+
const rot2 = new RotatableMutatableArray(["A", "B", "C"]);
3838
rot2.setIndex(1); // B
3939
rot2.insert(1, "Y");
4040
expect(rot2.peek()).toBe("B");
4141
expect([...rot2.cycle()]).toEqual(["B", "C", "A", "Y"]);
4242
});
4343

4444
it("insert() after the cursor does not move the cursor", () => {
45-
const rot = new MutatableRotatableArray(["A", "B", "C"]);
45+
const rot = new RotatableMutatableArray(["A", "B", "C"]);
4646
rot.setIndex(1); // B
4747
rot.insert(3, "D"); // append
4848
expect(rot.peek()).toBe("B");
@@ -54,7 +54,7 @@ describe("MutatableRotatableArray", () => {
5454
});
5555

5656
it("insert() at index 0 keeps the current item when cursor is 0", () => {
57-
const rot = new MutatableRotatableArray(["A", "B", "C"]);
57+
const rot = new RotatableMutatableArray(["A", "B", "C"]);
5858
rot.setIndex(0); // A
5959
rot.insert(0, "X");
6060
expect(rot.peek()).toBe("A");
@@ -66,7 +66,7 @@ describe("MutatableRotatableArray", () => {
6666
});
6767

6868
it("insert() returns the new length and allows index == length", () => {
69-
const rot = new MutatableRotatableArray(["A"]);
69+
const rot = new RotatableMutatableArray(["A"]);
7070
expect(rot.insert(1, "B")).toBe(2);
7171
expect(rot.length).toBe(2);
7272
expect(rot.next()).toBe("A");
@@ -75,28 +75,28 @@ describe("MutatableRotatableArray", () => {
7575
});
7676

7777
it("removeAt() shifts the cursor to keep the same current item", () => {
78-
const rot = new MutatableRotatableArray(["A", "B", "C", "D"]);
78+
const rot = new RotatableMutatableArray(["A", "B", "C", "D"]);
7979
rot.setIndex(2); // C
8080
expect(rot.removeAt(0)).toBe("A");
8181
expect(rot.peek()).toBe("C");
8282
expect([...rot.cycle()]).toEqual(["C", "D", "B"]);
8383
});
8484

8585
it("removeAt() advances when removing the current item and wraps on last", () => {
86-
const rot = new MutatableRotatableArray(["A", "B", "C"]);
86+
const rot = new RotatableMutatableArray(["A", "B", "C"]);
8787
rot.setIndex(1); // B
8888
expect(rot.removeAt(1)).toBe("B");
8989
expect(rot.peek()).toBe("C");
9090
expect([...rot.cycle()]).toEqual(["C", "A"]);
9191

92-
const rot2 = new MutatableRotatableArray(["A", "B", "C"]);
92+
const rot2 = new RotatableMutatableArray(["A", "B", "C"]);
9393
rot2.setIndex(2); // C
9494
rot2.removeAt(2);
9595
expect(rot2.peek()).toBe("A");
9696
});
9797

9898
it("removeAt() after the cursor keeps the cursor position", () => {
99-
const rot = new MutatableRotatableArray(["A", "B", "C", "D"]);
99+
const rot = new RotatableMutatableArray(["A", "B", "C", "D"]);
100100
rot.setIndex(1); // B
101101
expect(rot.removeAt(3)).toBe("D");
102102
expect(rot.peek()).toBe("B");
@@ -107,7 +107,7 @@ describe("MutatableRotatableArray", () => {
107107
});
108108

109109
it("removeAt() on a 2-item array leaves the remaining item current", () => {
110-
const rot = new MutatableRotatableArray(["A", "B"]);
110+
const rot = new RotatableMutatableArray(["A", "B"]);
111111
rot.setIndex(0); // A
112112
expect(rot.removeAt(0)).toBe("A");
113113
expect(rot.length).toBe(1);
@@ -117,12 +117,12 @@ describe("MutatableRotatableArray", () => {
117117
});
118118

119119
it("removeAt() throws when removing the last element", () => {
120-
const rot = new MutatableRotatableArray(["only"]);
120+
const rot = new RotatableMutatableArray(["only"]);
121121
expect(() => rot.removeAt(0)).toThrowError(/at least one/i);
122122
});
123123

124124
it("validates indices for insert/remove", () => {
125-
const rot = new MutatableRotatableArray(["A", "B"]);
125+
const rot = new RotatableMutatableArray(["A", "B"]);
126126
expect(() => rot.insert(-1, "X")).toThrow(RangeError);
127127
expect(() => rot.insert(3, "X")).toThrow(RangeError);
128128
expect(() => rot.insert(0.5, "X")).toThrow(RangeError);
@@ -136,13 +136,13 @@ describe("MutatableRotatableArray", () => {
136136

137137
it("respects copy=true vs copy=false semantics", () => {
138138
const srcForCopy = [1, 2];
139-
const copied = new MutatableRotatableArray(srcForCopy, true);
139+
const copied = new RotatableMutatableArray(srcForCopy, true);
140140
copied.push(3);
141141
expect(srcForCopy).toEqual([1, 2]);
142142
expect(copied.toArray()).toEqual([1, 2, 3]);
143143

144144
const srcForShare = [1, 2];
145-
const shared = new MutatableRotatableArray(srcForShare, false);
145+
const shared = new RotatableMutatableArray(srcForShare, false);
146146
shared.push(3);
147147
expect(srcForShare).toEqual([1, 2, 3]);
148148
srcForShare.push(4);

0 commit comments

Comments
 (0)