Skip to content

Commit defb311

Browse files
committed
remove alias'
1 parent 3d9ea8e commit defb311

4 files changed

Lines changed: 25 additions & 48 deletions

File tree

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ console.log(rot.move(1)); // C
2929
3030
const set = new RotatableSet([1, 2, 3]);
3131
set.add(4); // Set-style add (unique, returns this)
32-
set.addItem(4); // false (already present)
32+
set.add(4); // no-op for duplicates
3333
console.log(set.next()); // 1
3434
console.log(set.getFurthestItem()); // 4
3535
```
@@ -77,17 +77,16 @@ The cursor always points at the item that `next()` will return.
7777
| method | summary |
7878
| --------------------- | ----------------------------------------------------------- |
7979
| `add(item)` | Set‑style add (unique), returns `this` |
80-
| `addItem(item)` | boolean add helper (`true` if inserted) |
8180
| `delete(item)` | Set‑style delete, returns `boolean` |
82-
| `removeItem(item)` | alias for `delete` |
8381
| `next()` | return current, advance (wraps) |
8482
| `peek()` | read current without advancing |
8583
| `getFurthestItem()` | item farthest from cursor in `next()` steps |
8684
| `has(item)` | membership check |
85+
| `isEmpty` | whether the set has zero items |
8786
| `clear()` | remove all items |
8887
| `toArray()` *(O(n))* | snapshot in rotation order starting from cursor |
8988
| `toSet()` *(O(n))* | finite snapshot as native `Set` (in insertion order) |
90-
| `size` / `length` | item count |
89+
| `size` | item count |
9190
| `Symbol.iterator` | infinite iterator (never `done`) |
9291
| `cycle()` *(O(n))* | one full pass starting at cursor |
9392

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rotatable-array",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "Allows you to rotate/iterate through an array in a circular manner.",
55
"scripts": {
66
"build": "tsdown",

src/rotatableSet.ts

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class RotatableSet<T> implements Iterable<T> {
2424

2525
constructor(items: readonly T[] = []) {
2626
for (const item of items) {
27-
this.addItem(item);
27+
this.add(item);
2828
}
2929
}
3030

@@ -33,11 +33,6 @@ export class RotatableSet<T> implements Iterable<T> {
3333
return this._size;
3434
}
3535

36-
/** Alias for `size` for parity with arrays. */
37-
get length(): number {
38-
return this._size;
39-
}
40-
4136
/** Whether the set is empty. */
4237
get isEmpty(): boolean {
4338
return this._size === 0;
@@ -86,16 +81,6 @@ export class RotatableSet<T> implements Iterable<T> {
8681
return this;
8782
}
8883

89-
/**
90-
* Alias for `add` that reports whether insertion happened.
91-
* @returns `true` if the item was added, `false` if it already existed.
92-
*/
93-
addItem(item: T): boolean {
94-
if (this.nodes.has(item)) return false;
95-
this.add(item);
96-
return true;
97-
}
98-
9984
/**
10085
* Remove `item` from the set. Matches native `Set.delete`.
10186
* @returns `true` if the item was removed.
@@ -122,11 +107,6 @@ export class RotatableSet<T> implements Iterable<T> {
122107
return true;
123108
}
124109

125-
/** Alias for `delete` for parity with earlier API. */
126-
removeItem(item: T): boolean {
127-
return this.delete(item);
128-
}
129-
130110
/**
131111
* Get the furthest item from the current cursor in terms of `next()` steps.
132112
* For non-empty sets this is always the previous item.

test/rotatableSet.test.ts

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,19 @@ describe("RotatableSet", () => {
55
it("starts empty and throws on next/furthest/peek", () => {
66
const ring = new RotatableSet<number>();
77
expect(ring.size).toBe(0);
8-
expect(ring.length).toBe(0);
98
expect(ring.isEmpty).toBe(true);
109
expect(() => ring.next()).toThrowError(/empty/i);
1110
expect(() => ring.getFurthestItem()).toThrowError(/empty/i);
1211
expect(() => ring.peek()).toThrowError(/empty/i);
13-
expect(ring.removeItem(1)).toBe(false);
12+
expect(ring.delete(1)).toBe(false);
1413
expect([...ring.cycle()]).toEqual([]);
1514
expect(Array.from(ring.toSet())).toEqual([]);
1615
});
1716

1817
it("preserves insertion order when adding to empty ring", () => {
1918
const ring = new RotatableSet<number>();
20-
expect(ring.addItem(1)).toBe(true);
21-
expect(ring.addItem(2)).toBe(true);
22-
expect(ring.addItem(3)).toBe(true);
19+
ring.add(1).add(2).add(3);
20+
expect(ring.size).toBe(3);
2321
expect(ring.toArray()).toEqual([1, 2, 3]);
2422
expect(ring.next()).toBe(1);
2523
expect(ring.next()).toBe(2);
@@ -42,25 +40,25 @@ describe("RotatableSet", () => {
4240
expect(ring.toArray()).toEqual([1, 2, 3]);
4341
});
4442

45-
it("addItem does not move the cursor", () => {
43+
it("add does not move the cursor", () => {
4644
const ring = new RotatableSet([1, 2, 3]);
4745
ring.next(); // 1, cursor at 2
48-
ring.addItem(4);
46+
ring.add(4);
4947
expect(ring.peek()).toBe(2);
5048
expect(ring.toArray()).toEqual([2, 3, 1, 4]);
5149
expect(ring.next()).toBe(2);
5250
});
5351

54-
it("addItem returns false for existing items and keeps state unchanged", () => {
52+
it("adding an existing item keeps state unchanged", () => {
5553
const ring = new RotatableSet([1, 2, 3]);
5654
ring.next(); // 1, cursor at 2
57-
expect(ring.addItem(2)).toBe(false);
55+
ring.add(2); // duplicate
5856
expect(ring.size).toBe(3);
5957
expect(ring.peek()).toBe(2);
6058
expect(ring.toArray()).toEqual([2, 3, 1]);
6159
});
6260

63-
it("add() matches Set semantics and is an alias for addItem", () => {
61+
it("add() matches Set semantics", () => {
6462
const ring = new RotatableSet<number>();
6563
const ret = ring.add(1).add(2);
6664
expect(ret).toBe(ring);
@@ -121,19 +119,19 @@ describe("RotatableSet", () => {
121119
expect([...ring.cycle()]).toEqual(["only"]);
122120
});
123121

124-
it("removeItem removes items and maintains cursor", () => {
122+
it("delete removes items and maintains cursor", () => {
125123
const ring = new RotatableSet([1, 2, 3]);
126124
ring.next(); // 1, cursor at 2
127-
expect(ring.removeItem(2)).toBe(true); // remove current
125+
expect(ring.delete(2)).toBe(true); // remove current
128126
expect(ring.peek()).toBe(3);
129127
expect(ring.size).toBe(2);
130128
expect([...ring.cycle()]).toEqual([3, 1]);
131129

132-
expect(ring.removeItem(99)).toBe(false);
130+
expect(ring.delete(99)).toBe(false);
133131
expect(ring.size).toBe(2);
134132
});
135133

136-
it("delete() matches Set semantics and is an alias for removeItem", () => {
134+
it("delete() matches Set semantics", () => {
137135
const ring = new RotatableSet([1, 2, 3]);
138136
expect(ring.delete(2)).toBe(true);
139137
expect(ring.delete(2)).toBe(false);
@@ -144,24 +142,24 @@ describe("RotatableSet", () => {
144142
it("removing current from a 2-item set leaves the other item current", () => {
145143
const ring = new RotatableSet(["A", "B"]);
146144
ring.next(); // A, cursor at B
147-
expect(ring.removeItem("B")).toBe(true);
145+
expect(ring.delete("B")).toBe(true);
148146
expect(ring.size).toBe(1);
149147
expect(ring.peek()).toBe("A");
150148
expect(ring.getFurthestItem()).toBe("A");
151149
});
152150

153-
it("removeItem of a non-current item does not move the cursor", () => {
151+
it("deleting a non-current item does not move the cursor", () => {
154152
const ring = new RotatableSet([1, 2, 3, 4]);
155153
ring.next(); // 1, cursor at 2
156-
expect(ring.removeItem(4)).toBe(true);
154+
expect(ring.delete(4)).toBe(true);
157155
expect(ring.peek()).toBe(2);
158156
expect(ring.toArray()).toEqual([2, 3, 1]);
159157
expect(ring.getFurthestItem()).toBe(1);
160158
});
161159

162-
it("enforces uniqueness on addItem", () => {
160+
it("enforces uniqueness on add", () => {
163161
const ring = new RotatableSet([1, 2, 3]);
164-
expect(ring.addItem(2)).toBe(false);
162+
ring.add(2);
165163
expect(ring.size).toBe(3);
166164
expect(ring.toArray()).toEqual([1, 2, 3]);
167165
});
@@ -178,11 +176,11 @@ describe("RotatableSet", () => {
178176

179177
it("removing the last item resets ring and allows fresh adds", () => {
180178
const ring = new RotatableSet<number>([10]);
181-
expect(ring.removeItem(10)).toBe(true);
179+
expect(ring.delete(10)).toBe(true);
182180
expect(ring.size).toBe(0);
183181
expect(() => ring.next()).toThrowError(/empty/i);
184-
ring.addItem(20);
185-
ring.addItem(30);
182+
ring.add(20);
183+
ring.add(30);
186184
expect(ring.toArray()).toEqual([20, 30]);
187185
expect(ring.next()).toBe(20);
188186
});

0 commit comments

Comments
 (0)