diff --git a/npm/ng-packs/packages/core/src/lib/tests/linked-list.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/linked-list.spec.ts index d52050c0ad..518956deba 100644 --- a/npm/ng-packs/packages/core/src/lib/tests/linked-list.spec.ts +++ b/npm/ng-packs/packages/core/src/lib/tests/linked-list.spec.ts @@ -212,7 +212,7 @@ describe('Linked List (Doubly)', () => { // "a" <-> "x" <-> "b" <-> "x" <-> "c" - const node1 = list.find(value => value === 'x'); + const node1 = list.find(node => node.previous && node.previous.value === 'a'); expect(node1.value).toBe('x'); expect(node1.previous.value).toBe('a'); @@ -220,7 +220,7 @@ describe('Linked List (Doubly)', () => { // "a" <-> "x" <-> "b" <-> "x" <-> "c" - const node2 = list.find((_, index) => index === 3); + const node2 = list.find(node => node.next && node.next.value === 'c'); expect(node2.value).toBe('x'); expect(node2.previous.value).toBe('b'); @@ -228,9 +228,9 @@ describe('Linked List (Doubly)', () => { }); it('should return undefined when list is empty', () => { - const node = list.find(value => value === 'x'); + const found = list.find(node => node.value === 'x'); - expect(node).toBeUndefined(); + expect(found).toBeUndefined(); }); it('should return undefined when predicate finds no match', () => { @@ -240,9 +240,9 @@ describe('Linked List (Doubly)', () => { // "a" <-> "b" <-> "c" - const node = list.find(value => value === 'x'); + const found = list.find(node => node.value === 'x'); - expect(node).toBeUndefined(); + expect(found).toBeUndefined(); expect(list.length).toBe(3); expect(list.head.value).toBe('a'); expect(list.head.next.value).toBe('b'); @@ -263,26 +263,19 @@ describe('Linked List (Doubly)', () => { // "a" <-> "x" <-> "b" <-> "x" <-> "c" - const index1 = list.findIndex(value => value === 'x'); + const index1 = list.findIndex(node => node.previous && node.previous.value === 'a'); expect(index1).toBe(1); // "a" <-> "x" <-> "b" <-> "x" <-> "c" - let timesFound = 0; - const index2 = list.findIndex(value => { - if (timesFound > 1) return false; - - timesFound += Number(value === 'x'); - - return timesFound > 1; - }); + const index2 = list.findIndex(node => node.next && node.next.value === 'c'); expect(index2).toBe(3); }); it('should return -1 when list is empty', () => { - const index = list.findIndex(value => value === 'x'); + const index = list.findIndex(node => node.value === 'x'); expect(index).toBe(-1); }); @@ -294,7 +287,7 @@ describe('Linked List (Doubly)', () => { // "a" <-> "b" <-> "c" - const index = list.findIndex(value => value === 'x'); + const index = list.findIndex(node => node.value === 'x'); expect(index).toBe(-1); expect(list.length).toBe(3); @@ -309,9 +302,9 @@ describe('Linked List (Doubly)', () => { describe('#forEach', () => { it('should call given function for each node of the list', () => { - list.add('a').tail(); - list.add('b').tail(); - list.add('c').tail(); + const a = list.add('a').tail(); + const b = list.add('b').tail(); + const c = list.add('c').tail(); // "a" <-> "b" <-> "c" @@ -319,9 +312,9 @@ describe('Linked List (Doubly)', () => { list.forEach(spy); expect(spy.mock.calls).toEqual([ - ['a', 0, list], - ['b', 1, list], - ['c', 2, list], + [a, 0, list], + [b, 1, list], + [c, 2, list], ]); }); diff --git a/npm/ng-packs/packages/core/src/lib/utils/linked-list.ts b/npm/ng-packs/packages/core/src/lib/utils/linked-list.ts index 6d2435acdb..42cc5bc941 100644 --- a/npm/ng-packs/packages/core/src/lib/utils/linked-list.ts +++ b/npm/ng-packs/packages/core/src/lib/utils/linked-list.ts @@ -68,13 +68,13 @@ export class LinkedList { } addAfter(value: T, previousValue: T, compareFn = compare): ListNode { - const previous = this.find(currentValue => compareFn(currentValue, previousValue)); + const previous = this.find(node => compareFn(node.value, previousValue)); return previous ? this.linkWith(value, previous, previous.next) : this.addTail(value); } addBefore(value: T, nextValue: T, compareFn = compare): ListNode { - const next = this.find(currentValue => compareFn(currentValue, nextValue)); + const next = this.find(node => compareFn(node.value, nextValue)); return next ? this.linkWith(value, next.previous, next) : this.addHead(value); } @@ -148,7 +148,7 @@ export class LinkedList { } dropByValue(value: T, compareFn = compare): ListNode | undefined { - const position = this.findIndex(currentValue => compareFn(currentValue, value)); + const position = this.findIndex(node => compareFn(node.value, value)); if (position < 0) return undefined; @@ -203,7 +203,7 @@ export class LinkedList { find(predicate: ListIteratorFunction): ListNode | undefined { for (let current = this.first, position = 0; current; position += 1, current = current.next) { - if (predicate(current.value, position, this)) return current; + if (predicate(current, position, this)) return current; } return undefined; @@ -211,7 +211,7 @@ export class LinkedList { findIndex(predicate: ListIteratorFunction): number { for (let current = this.first, position = 0; current; position += 1, current = current.next) { - if (predicate(current.value, position, this)) return position; + if (predicate(current, position, this)) return position; } return -1; @@ -219,7 +219,7 @@ export class LinkedList { forEach(callback: ListIteratorFunction) { for (let node = this.first, position = 0; node; position += 1, node = node.next) { - callback(node.value, position, this); + callback(node, position, this); } } @@ -228,13 +228,13 @@ export class LinkedList { } indexOf(value: T, compareFn = compare): number { - return this.findIndex(currentValue => compareFn(currentValue, value)); + return this.findIndex(node => compareFn(node.value, value)); } toArray(): T[] { const array = new Array(this.size); - this.forEach((value, index) => (array[index!] = value)); + this.forEach((node, index) => (array[index!] = node.value)); return array; } @@ -253,7 +253,7 @@ export class LinkedList { } export type ListIteratorFunction = ( - value: T, + node: ListNode, index?: number, list?: LinkedList, ) => R;