Skip to content

Commit e64c6c1

Browse files
committed
Caret functions now return the index that was inserted, also removed the direction type variable
1 parent 03daa7d commit e64c6c1

File tree

4 files changed

+49
-33
lines changed

4 files changed

+49
-33
lines changed

lib/ArrayFixed.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ class ArrayFixed<item> {
212212
return;
213213
}
214214

215-
caretLeft (index: number, value: item): void {
215+
caretLeft (index: number, value: item): number {
216216
if (index >= this._array.length || index < 0) {
217217
throw new RangeError('Out of range index');
218218
}
@@ -222,7 +222,7 @@ class ArrayFixed<item> {
222222
if (!this._array.hasOwnProperty(index)) {
223223
this._array[index] = value;
224224
++this._count;
225-
return;
225+
return index;
226226
}
227227
let emptyIndex = null;
228228
for (let i = index - 1; i >= 0; --i) {
@@ -237,10 +237,10 @@ class ArrayFixed<item> {
237237
this._array.copyWithin(emptyIndex, emptyIndex + 1, index + 1);
238238
this._array[index] = value;
239239
++this._count;
240-
return;
240+
return index;
241241
}
242242

243-
caretRight (index: number, value: item) {
243+
caretRight (index: number, value: item): number {
244244
if (index >= this._array.length || index < 0) {
245245
throw new RangeError('Out of range index');
246246
}
@@ -250,7 +250,7 @@ class ArrayFixed<item> {
250250
if (!this._array.hasOwnProperty(index)) {
251251
this._array[index] = value;
252252
++this._count;
253-
return;
253+
return index;
254254
}
255255
let emptyIndex = null;
256256
for (let i = index + 1; i < this._array.length; ++i) {
@@ -265,14 +265,14 @@ class ArrayFixed<item> {
265265
this._array.copyWithin(index + 1, index, emptyIndex);
266266
this._array[index] = value;
267267
++this._count;
268-
return;
268+
return index;
269269
}
270270

271271
caret (
272272
index: number,
273273
value: item,
274274
preferredDirection: boolean = true,
275-
): void {
275+
): number {
276276
if (index >= this._array.length || index < 0) {
277277
throw new RangeError('Out of range index');
278278
}
@@ -282,7 +282,7 @@ class ArrayFixed<item> {
282282
if (!this._array.hasOwnProperty(index)) {
283283
this._array[index] = value;
284284
++this._count;
285-
return;
285+
return index;
286286
}
287287
let emptyDirectionAndIndex = null;
288288
let i = index - 1, j = index + 1;
@@ -356,7 +356,7 @@ class ArrayFixed<item> {
356356
}
357357
this._array[index] = value;
358358
++this._count;
359-
return;
359+
return index;
360360
}
361361

362362
}

lib/ArrayFixedDense.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ import ArrayFixed from './ArrayFixed.js';
77
* Class representing a fixed size dense array.
88
* This ensures that mutation always results in a dense array.
99
*/
10-
class ArrayFixedDense<item, direction: boolean> extends ArrayFixed<item> {
10+
class ArrayFixedDense<item> extends ArrayFixed<item> {
1111

12-
_direction: direction;
12+
_direction: boolean;
1313

1414
constructor (
1515
sizeOrArray: number|Array<item> = 0,
16-
direction_: direction = true
16+
direction: boolean = true
1717
) {
1818
if (Array.isArray(sizeOrArray)) {
1919
// $FlowFixMe: Arrays are objects
2020
const arrayNew = Object.keys(sizeOrArray).map((k) => sizeOrArray[k]);
21-
if (direction_) {
21+
if (direction) {
2222
arrayNew.length = sizeOrArray.length;
2323
sizeOrArray = arrayNew;
2424
} else {
@@ -28,38 +28,38 @@ class ArrayFixedDense<item, direction: boolean> extends ArrayFixed<item> {
2828
}
2929
}
3030
super(sizeOrArray);
31-
this._direction = direction_;
31+
this._direction = direction;
3232
}
3333

3434
/**
3535
* Construct from reference.
3636
* This skips the integrity process in the normal constructor.
3737
* The array must already be dense, and have the correct count and direction.
3838
*/
39-
static fromArray<direction: boolean> (
39+
static fromArray (
4040
array: Array<item>,
4141
count: number,
42-
direction_: direction = true
43-
): ArrayFixedDense<item, direction> {
42+
direction: boolean = true
43+
): ArrayFixedDense<item> {
4444
const arrayFixedDense = new ArrayFixedDense(array.length);
4545
arrayFixedDense._array = array;
4646
arrayFixedDense._count = count;
47-
arrayFixedDense._direction = direction_;
47+
arrayFixedDense._direction = direction;
4848
return arrayFixedDense;
4949
}
5050

5151
get direction (): boolean {
5252
return this._direction;
5353
}
5454

55-
switchDirection (direction_: direction): void {
56-
if (direction_ !== this._direction) {
57-
if (direction_) {
55+
switchDirection (direction: boolean): void {
56+
if (direction !== this._direction) {
57+
if (direction) {
5858
super.collapseLeft();
5959
} else {
6060
super.collapseRight();
6161
}
62-
this._direction = direction_;
62+
this._direction = direction;
6363
}
6464
}
6565

@@ -109,7 +109,7 @@ class ArrayFixedDense<item, direction: boolean> extends ArrayFixed<item> {
109109
}
110110
}
111111

112-
reverse (): ArrayFixedDense<item, direction> {
112+
reverse (): ArrayFixedDense<item> {
113113
let swapStart, swapMid;
114114
if (this._direction) {
115115
swapStart = 0;
@@ -148,7 +148,7 @@ class ArrayFixedDense<item, direction: boolean> extends ArrayFixed<item> {
148148
slice (
149149
begin: number = 0,
150150
end: number = this._array.length
151-
): ArrayFixedDense<item, direction> {
151+
): ArrayFixedDense<item> {
152152
begin = Math.trunc(begin);
153153
end = Math.trunc(end);
154154
if (begin < 0) {
@@ -174,7 +174,7 @@ class ArrayFixedDense<item, direction: boolean> extends ArrayFixed<item> {
174174
indexStart: number = 0,
175175
deleteCount: ?number,
176176
...items: Array<item>
177-
): ArrayFixedDense<item, direction> {
177+
): ArrayFixedDense<item> {
178178
indexStart = Math.trunc(indexStart);
179179
if (indexStart < 0) {
180180
indexStart = Math.max(indexStart + this._array.length, 0);
@@ -213,19 +213,19 @@ class ArrayFixedDense<item, direction: boolean> extends ArrayFixed<item> {
213213
}
214214

215215
map<itemNew> (
216-
callback: (item, number, ArrayFixedDense<item, direction>) => itemNew
217-
): ArrayFixedDense<itemNew, direction> {
216+
callback: (item, number, ArrayFixedDense<item>) => itemNew
217+
): ArrayFixedDense<itemNew> {
218218
const arrayNew = this._array.map((v, i) => callback(v, i, this));
219219
return ArrayFixedDense.fromArray(arrayNew, this._count, this._direction);
220220
}
221221

222222
forEach (
223-
callback: (item, number, ArrayFixedDense<item, direction>) => any
223+
callback: (item, number, ArrayFixedDense<item>) => any
224224
): void {
225225
this._array.forEach((v, i) => callback(v, i, this));
226226
}
227227

228-
caretLeft (index: number, value: item): void {
228+
caretLeft (index: number, value: item): number {
229229
if (index >= this._array.length || index < 0) {
230230
throw new RangeError('Out of range index');
231231
}
@@ -254,10 +254,10 @@ class ArrayFixedDense<item, direction: boolean> extends ArrayFixed<item> {
254254
}
255255
this._array[index] = value;
256256
++this._count;
257-
return;
257+
return index;
258258
}
259259

260-
caretRight (index: number, value: item): void {
260+
caretRight (index: number, value: item): number {
261261
if (index >= this._array.length || index < 0) {
262262
throw new RangeError('Out of range index');
263263
}
@@ -285,10 +285,10 @@ class ArrayFixedDense<item, direction: boolean> extends ArrayFixed<item> {
285285
}
286286
this._array[index] = value;
287287
++this._count;
288-
return;
288+
return index;
289289
}
290290

291-
caret (index: number, value: item): void {
291+
caret (index: number, value: item): number {
292292
// the preferred direction is the opposite direction of this dense array
293293
if (this._direction) {
294294
return this.caretRight(index, value);

test/ArrayFixed.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,9 @@ test('caret', t => {
208208
}, RangeError);
209209
t.deepEqual(arr.toArray(), [ ,2, ,]);
210210
});
211+
212+
test('caret returns back inserted index', t => {
213+
const arr = new ArrayFixed([1, ,2,3,4, ,5]);
214+
t.is(arr.caretRight(0, 0), 0);
215+
t.is(arr.caretLeft(6, 6), 6);
216+
});

test/ArrayFixedDense.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,3 +292,13 @@ test('caret', t => {
292292
t.is(arr.length, origLength);
293293
t.deepEqual(arr.toArray(), [1,2,3,4,5]);
294294
});
295+
296+
test('caret returns back inserted index', t => {
297+
let arr;
298+
arr = new ArrayFixedDense([1,2,3, , ,], true);
299+
t.is(arr.caret(4, 4), 3);
300+
t.is(arr.caret(4, 4), 4);
301+
arr = new ArrayFixedDense([ , , 4, 5, 6], false);
302+
t.is(arr.caretRight(0, 3), 1);
303+
t.is(arr.caretLeft(1, 3), 1);
304+
});

0 commit comments

Comments
 (0)