From cf6ce8fcee724016f83e1ae49f88d058dadacbb2 Mon Sep 17 00:00:00 2001 From: Klaus Sinani Date: Sat, 6 Apr 2019 03:10:11 +0300 Subject: [PATCH] Add new `List#indexOf(value)` class method (#15) --- src/circular.js | 18 ++++++++++++++++++ src/linear.js | 16 ++++++++++++++++ test/circular/empty.js | 5 +++++ test/circular/multiple.js | 7 +++++++ test/circular/single.js | 6 ++++++ test/linear/empty.js | 5 +++++ test/linear/multiple.js | 7 +++++++ test/linear/single.js | 5 +++++ test/types/circular.ts | 3 +++ test/types/linear.ts | 3 +++ types/singlie.d.ts | 1 + 11 files changed, 76 insertions(+) diff --git a/src/circular.js b/src/circular.js index 2f335e7..668bdf4 100644 --- a/src/circular.js +++ b/src/circular.js @@ -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) { diff --git a/src/linear.js b/src/linear.js index 704e1f3..faa3891 100644 --- a/src/linear.js +++ b/src/linear.js @@ -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) { diff --git a/test/circular/empty.js b/test/circular/empty.js index 71f09a2..a4ae41f 100644 --- a/test/circular/empty.js +++ b/test/circular/empty.js @@ -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); +}); diff --git a/test/circular/multiple.js b/test/circular/multiple.js index 785e1d4..552c52c 100644 --- a/test/circular/multiple.js +++ b/test/circular/multiple.js @@ -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); +}); diff --git a/test/circular/single.js b/test/circular/single.js index 85791ca..2a2733a 100644 --- a/test/circular/single.js +++ b/test/circular/single.js @@ -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); +}); diff --git a/test/linear/empty.js b/test/linear/empty.js index 9ec5e8b..289e45e 100644 --- a/test/linear/empty.js +++ b/test/linear/empty.js @@ -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); +}); diff --git a/test/linear/multiple.js b/test/linear/multiple.js index c4d3719..2fb9431 100644 --- a/test/linear/multiple.js +++ b/test/linear/multiple.js @@ -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); +}); diff --git a/test/linear/single.js b/test/linear/single.js index 7a15f55..c9e2973 100644 --- a/test/linear/single.js +++ b/test/linear/single.js @@ -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); +}); diff --git a/test/types/circular.ts b/test/types/circular.ts index 9e1373f..a050593 100644 --- a/test/types/circular.ts +++ b/test/types/circular.ts @@ -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 diff --git a/test/types/linear.ts b/test/types/linear.ts index 38921dd..3e9d666 100644 --- a/test/types/linear.ts +++ b/test/types/linear.ts @@ -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 diff --git a/types/singlie.d.ts b/types/singlie.d.ts index 17442e5..245eda8 100644 --- a/types/singlie.d.ts +++ b/types/singlie.d.ts @@ -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;