Skip to content

Commit

Permalink
Add new List#indexOf(value) class method (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
klaudiosinani authored Apr 6, 2019
1 parent a595a4b commit cf6ce8f
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/circular.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,24 @@ class Circular extends List {
return false;
}

indexOf(value) {
let counter = 0;
let {_head: node} = this;

if (node) {
do {
if (node.value === value) {
return counter;
}

counter++;
node = node.next;
} while (node !== this._head);
}

return -1;
}

insert({value, index}) {
this._arrayify(value).forEach(x => {
if (index === 0) {
Expand Down
16 changes: 16 additions & 0 deletions src/linear.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,22 @@ class Linear extends List {
return false;
}

indexOf(value) {
let counter = 0;
let {_head: node} = this;

while (node) {
if (node.value === value) {
return counter;
}

counter++;
node = node.next;
}

return -1;
}

insert({value, index}) {
this._arrayify(value).forEach(x => {
if (index === 0) {
Expand Down
5 changes: 5 additions & 0 deletions test/circular/empty.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,8 @@ test('includes', t => {
t.is(circular.includes(), false);
t.is(circular.includes(1), false);
});

test('indexOf', t => {
t.is(circular.indexOf(), -1);
t.is(circular.indexOf(15), -1);
});
7 changes: 7 additions & 0 deletions test/circular/multiple.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,10 @@ test('includes', t => {
t.is(circular.includes(0), false);
t.is(circular.includes(25), true);
});

test('indexOf', t => {
t.is(circular.indexOf(), -1);
t.is(circular.indexOf(0), -1);
t.is(circular.indexOf(15), 2);
t.is(circular.indexOf(30), 5);
});
6 changes: 6 additions & 0 deletions test/circular/single.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,9 @@ test('includes', t => {
t.is(circular.includes(0), false);
t.is(circular.includes(5), true);
});

test('indexOf', t => {
t.is(circular.indexOf(), -1);
t.is(circular.indexOf(0), -1);
t.is(circular.indexOf(5), 0);
});
5 changes: 5 additions & 0 deletions test/linear/empty.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,8 @@ test('includes', t => {
t.is(linear.includes(), false);
t.is(linear.includes(1), false);
});

test('indexOf', t => {
t.is(linear.indexOf(), -1);
t.is(linear.indexOf(0), -1);
});
7 changes: 7 additions & 0 deletions test/linear/multiple.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,10 @@ test('includes', t => {
t.is(linear.includes(0), false);
t.is(linear.includes(25), true);
});

test('indexOf', t => {
t.is(linear.indexOf(), -1);
t.is(linear.indexOf(0), -1);
t.is(linear.indexOf(15), 2);
t.is(linear.indexOf(30), 5);
});
5 changes: 5 additions & 0 deletions test/linear/single.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,8 @@ test('includes', t => {
t.is(linear.includes(0), false);
t.is(linear.includes(5), true);
});
test('indexOf', t => {
t.is(linear.indexOf(), -1);
t.is(linear.indexOf(0), -1);
t.is(linear.indexOf(5), 0);
});
3 changes: 3 additions & 0 deletions test/types/circular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ circular.clear(); // => Circular { head: null, last: null, length: 0 }
circular.append('R', 'O', 'G');
circular.includes('G'); //=> true
circular.includes('D'); //=> false

circular.indexOf('G'); //=> 2
circular.indexOf('D'); //=> -1
3 changes: 3 additions & 0 deletions test/types/linear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@ linear.clear(); // => Linear { head: null, last: null, length: 0 }
linear.append('R', 'O', 'G');
linear.includes('G'); //=> true
linear.includes('D'); //=> false

linear.indexOf('G'); //=> 2
linear.indexOf('D'); //=> -1
1 change: 1 addition & 0 deletions types/singlie.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ declare namespace list {
forEach(fn: (value: T) => any): this;
get(index: number): T;
includes(value: T): boolean;
indexOf(value: T): number;
insert(opts: { value: T | T[]; index: number }): this;
isCircular(): boolean;
isEmpty(): boolean;
Expand Down

0 comments on commit cf6ce8f

Please sign in to comment.