doublylinked
Advanced tools
Comparing version 2.5.4 to 2.5.5
/// <reference lib="es2015.symbol" /> | ||
declare module 'doublylinked' { | ||
type Maybe<T> = T | void; | ||
export namespace DoublyLinked { | ||
export interface Node { | ||
value: any; | ||
readonly prev?: Node; | ||
readonly next?: Node; | ||
export interface Node<T> { | ||
value: T; | ||
readonly prev?: Node<T>; | ||
readonly next?: Node<T>; | ||
@@ -20,9 +19,9 @@ remove(): void; | ||
readonly cursor: DoublyLinked.Node; | ||
readonly cursor: DoublyLinked.Node<T>; | ||
readonly head: DoublyLinked.Node; | ||
readonly head: DoublyLinked.Node<T>; | ||
readonly length: number; | ||
readonly tail: DoublyLinked.Node; | ||
readonly tail: DoublyLinked.Node<T>; | ||
@@ -37,13 +36,31 @@ concat(...element: T[]): DoublyLinked<T>; | ||
every(callback: (element: T, index?: number, thisArg?: any) => Maybe<boolean>, thisArg?: any): boolean; | ||
every( | ||
callback: (element: T, index?: number, thisArg?: any) => Maybe<boolean>, | ||
thisArg?: any, | ||
): boolean; | ||
everyRight(callback: (element: T, index?: number, thisArg?: any) => Maybe<boolean>, thisArg?: any): boolean; | ||
everyRight( | ||
callback: (element: T, index?: number, thisArg?: any) => Maybe<boolean>, | ||
thisArg?: any, | ||
): boolean; | ||
filter(callback: (element: T, index?: number, thisArg?: any) => Maybe<boolean>, thisArg?: any): DoublyLinked<T>; | ||
filter( | ||
callback: (element: T, index?: number, thisArg?: any) => Maybe<boolean>, | ||
thisArg?: any, | ||
): DoublyLinked<T>; | ||
find(callback: (element: T, index?: number, thisArg?: any) => Maybe<boolean>, thisArg?: any): T; | ||
find( | ||
callback: (element: T, index?: number, thisArg?: any) => Maybe<boolean>, | ||
thisArg?: any, | ||
): T; | ||
forEach(callback: (element: T, index?: number, thisArg?: any) => void, thisArg?: any): void; | ||
forEach( | ||
callback: (element: T, index?: number, thisArg?: any) => void, | ||
thisArg?: any, | ||
): void; | ||
forEachRight(callback: (element: T, index?: number, thisArg?: any) => void, thisArg?: any): void; | ||
forEachRight( | ||
callback: (element: T, index?: number, thisArg?: any) => void, | ||
thisArg?: any, | ||
): void; | ||
@@ -56,3 +73,5 @@ includes(element: T, fromIndex?: number): boolean; | ||
map(callback: (element: T, index?: number, thisArg?: any) => void): DoublyLinked<T>; | ||
map( | ||
callback: (element: T, index?: number, thisArg?: any) => void, | ||
): DoublyLinked<T>; | ||
@@ -67,5 +86,21 @@ next(): T; | ||
reduce(callback: (accumulator: any, element: T, index?: number, thisArg?: any) => any, initialValue?: any): any; | ||
reduce( | ||
callback: ( | ||
accumulator: any, | ||
element: T, | ||
index?: number, | ||
thisArg?: any, | ||
) => any, | ||
initialValue?: any, | ||
): any; | ||
reduceRight(callback: (accumulator: any, element: T, index?: number, thisArg?: any) => any, initialValue?: any): any; | ||
reduceRight( | ||
callback: ( | ||
accumulator: any, | ||
element: T, | ||
index?: number, | ||
thisArg?: any, | ||
) => any, | ||
initialValue?: any, | ||
): any; | ||
@@ -82,5 +117,11 @@ remove(element: T, fromIndex?: number): any; | ||
some(callback: (element: T, index?: number, thisArg?: any) => boolean, thisArg?: any): boolean; | ||
some( | ||
callback: (element: T, index?: number, thisArg?: any) => boolean, | ||
thisArg?: any, | ||
): boolean; | ||
someRight(callback: (element: T, index?: number, thisArg?: any) => boolean, thisArg?: any): boolean; | ||
someRight( | ||
callback: (element: T, index?: number, thisArg?: any) => boolean, | ||
thisArg?: any, | ||
): boolean; | ||
@@ -92,5 +133,3 @@ toArray(): T[]; | ||
unshift(...element: T[]): number; | ||
} | ||
} |
@@ -15,3 +15,2 @@ /* doublylinked | ||
class DoublyLinked { | ||
/** | ||
@@ -27,4 +26,5 @@ * @param {*} element... - The elements to add to the end of the list | ||
this._eof = undefined; | ||
if (arguments.length) | ||
this.push.apply(this, arguments); | ||
if (element.length) { | ||
this.push.apply(this, element); | ||
} | ||
} | ||
@@ -79,5 +79,5 @@ | ||
for (const arg of element) { | ||
if (arg instanceof DoublyLinked) | ||
if (arg instanceof DoublyLinked) { | ||
arg.reduce(mergeFn, result); | ||
else result.push(arg); | ||
} else result.push(arg); | ||
} | ||
@@ -103,7 +103,7 @@ | ||
value: _cursor && [i++, _cursor.value], | ||
done: !_cursor | ||
done: !_cursor, | ||
}; | ||
} | ||
}, | ||
}; | ||
} | ||
}, | ||
}; | ||
@@ -127,7 +127,7 @@ } | ||
value: _cursor && i++, | ||
done: !_cursor | ||
done: !_cursor, | ||
}; | ||
} | ||
}, | ||
}; | ||
} | ||
}, | ||
}; | ||
@@ -150,7 +150,7 @@ } | ||
value: _cursor && _cursor.value, | ||
done: !_cursor | ||
done: !_cursor, | ||
}; | ||
} | ||
}, | ||
}; | ||
} | ||
}, | ||
}; | ||
@@ -169,6 +169,8 @@ } | ||
every(callback, thisArg) { | ||
if (typeof callback !== 'function') | ||
if (typeof callback !== 'function') { | ||
throw new TypeError('You must provide a function as first argument'); | ||
if (!(this._length && callback)) | ||
} | ||
if (!(this._length && callback)) { | ||
return true; | ||
} | ||
thisArg = thisArg !== undefined ? thisArg : this; | ||
@@ -180,4 +182,5 @@ let tmp = this._head; | ||
nxt = tmp.next; | ||
if (!callback.call(thisArg, tmp.value, i++, thisArg)) | ||
if (!callback.call(thisArg, tmp.value, i++, thisArg)) { | ||
return false; | ||
} | ||
tmp = nxt; | ||
@@ -198,11 +201,14 @@ } | ||
everyRight(callback, thisArg) { | ||
if (typeof callback !== 'function') | ||
if (typeof callback !== 'function') { | ||
throw new TypeError('You must provide a function as first argument'); | ||
if (!(this._length && callback)) | ||
} | ||
if (!(this._length && callback)) { | ||
return true; | ||
} | ||
thisArg = thisArg !== undefined ? thisArg : this; | ||
let tmp = this.tail; | ||
for (let i = 0; i < this._length; i++) { | ||
if (!callback.call(thisArg, tmp.value, this._length - i - 1, thisArg)) | ||
if (!callback.call(thisArg, tmp.value, this._length - i - 1, thisArg)) { | ||
return false; | ||
} | ||
tmp = tmp.prev; | ||
@@ -223,9 +229,11 @@ } | ||
filter(callback, thisArg) { | ||
if (typeof callback !== 'function') | ||
if (typeof callback !== 'function') { | ||
throw new TypeError('You must provide a function as first argument'); | ||
} | ||
thisArg = thisArg !== undefined ? thisArg : this; | ||
let index = 0; | ||
return this.reduce((acc, value) => { | ||
if (callback.call(thisArg, value, index++, thisArg)) | ||
if (callback.call(thisArg, value, index++, thisArg)) { | ||
acc.push(value); | ||
} | ||
return acc; | ||
@@ -245,6 +253,8 @@ }, new DoublyLinked()); | ||
find(callback, thisArg) { | ||
if (typeof callback !== 'function') | ||
if (typeof callback !== 'function') { | ||
throw new TypeError('You must provide a function as first argument'); | ||
if (!this._length) | ||
} | ||
if (!this._length) { | ||
return; | ||
} | ||
thisArg = thisArg !== undefined ? thisArg : this; | ||
@@ -301,14 +311,14 @@ let tmp = this.head; | ||
includes(searchElement, fromIndex) { | ||
const sameValueZero = (x, y) => | ||
x === y || | ||
(typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y)); | ||
const sameValueZero = (x, y) => { | ||
return x === y || | ||
(typeof x === 'number' && typeof y === 'number' && | ||
isNaN(x) && isNaN(y)); | ||
}; | ||
fromIndex = fromIndex || 0; | ||
if (fromIndex < 0) | ||
if (fromIndex < 0) { | ||
fromIndex = this.length + fromIndex; | ||
this.find((element, index) => | ||
(index >= fromIndex && sameValueZero(element, searchElement))); | ||
} | ||
this.find( | ||
(element, index) => | ||
index >= fromIndex && sameValueZero(element, searchElement), | ||
); | ||
return !!this.cursor; | ||
@@ -353,3 +363,3 @@ } | ||
let out = ''; | ||
this.forEach((value) => { | ||
this.forEach(value => { | ||
out += (out ? separator : '') + value; | ||
@@ -369,6 +379,9 @@ }); | ||
map(callback) { | ||
if (typeof callback !== 'function') | ||
if (typeof callback !== 'function') { | ||
throw new TypeError('You must provide a function as first argument'); | ||
} | ||
const out = new DoublyLinked(); | ||
this.forEach((value, index, instance) => out.push(callback(value, index, instance))); | ||
this.forEach((value, index, instance) => | ||
out.push(callback(value, index, instance)), | ||
); | ||
return out.reset(); | ||
@@ -434,4 +447,5 @@ } | ||
push(...element) { | ||
if (element.length) | ||
if (element.length) { | ||
this._eof = false; | ||
} | ||
for (const arg of element) { | ||
@@ -462,4 +476,5 @@ const node = new Node(this, arg); | ||
reduce(callback, initialValue) { | ||
if (typeof callback !== 'function') | ||
if (typeof callback !== 'function') { | ||
throw new TypeError('You must provide a function as first argument'); | ||
} | ||
let accumulator = initialValue; | ||
@@ -482,4 +497,5 @@ this.forEach((value, index) => { | ||
reduceRight(callback, initialValue) { | ||
if (typeof callback !== 'function') | ||
if (typeof callback !== 'function') { | ||
throw new TypeError('You must provide a function as first argument'); | ||
} | ||
let accumulator = initialValue; | ||
@@ -528,3 +544,4 @@ this.forEachRight((value, index) => { | ||
let cur = this._head; | ||
let p, n; | ||
let p; | ||
let n; | ||
for (let i = 0; i < this._length; i++) { | ||
@@ -573,4 +590,5 @@ p = cur.prev; | ||
this.every((value, index) => { | ||
if (index >= start) | ||
if (index >= start) { | ||
acc.push(value); | ||
} | ||
return !end || index < end; | ||
@@ -590,4 +608,7 @@ }); | ||
some(callback, thisArg) { | ||
return !this.every((element, index, instance) => | ||
!callback.call(this, element, index, instance), thisArg); | ||
return !this.every( | ||
(element, index, instance) => | ||
!callback.call(this, element, index, instance), | ||
thisArg, | ||
); | ||
} | ||
@@ -604,4 +625,7 @@ | ||
someRight(callback, thisArg) { | ||
return !this.everyRight((element, index, instance) => | ||
!callback.call(this, element, index, instance), thisArg); | ||
return !this.everyRight( | ||
(element, index, instance) => | ||
!callback.call(this, element, index, instance), | ||
thisArg, | ||
); | ||
} | ||
@@ -663,8 +687,7 @@ | ||
value: _cursor && _cursor.value, | ||
done: !_cursor | ||
done: !_cursor, | ||
}; | ||
} | ||
}, | ||
}; | ||
} | ||
} | ||
@@ -685,16 +708,22 @@ | ||
remove() { | ||
if (!this.list) | ||
if (!this.list) { | ||
return; | ||
if (this.prev) | ||
// noinspection JSUnresolvedVariable | ||
} | ||
if (this.prev) { | ||
// noinspection JSUnresolvedVariable | ||
this.prev.next = this.next; | ||
if (this.next) | ||
// noinspection JSUnresolvedVariable | ||
} | ||
if (this.next) { | ||
// noinspection JSUnresolvedVariable | ||
this.next.prev = this.prev; | ||
if (this === this.list._cursor) | ||
} | ||
if (this === this.list._cursor) { | ||
this.list._cursor = this.next || this.prev; | ||
if (this === this.list._head) | ||
} | ||
if (this === this.list._head) { | ||
this.list._head = this.next; | ||
if (this === this.list._tail) | ||
} | ||
if (this === this.list._tail) { | ||
this.list._tail = this.prev; | ||
} | ||
this.list._length--; | ||
@@ -701,0 +730,0 @@ this.prev = undefined; |
{ | ||
"name": "doublylinked", | ||
"description": "Doubly linked list implementation for JavaScript with iterator and array-like interface", | ||
"version": "2.5.4", | ||
"version": "2.5.5", | ||
"author": "Panates Ltd.", | ||
@@ -21,6 +21,8 @@ "contributors": [ | ||
"devDependencies": { | ||
"eslint": "^8.56.0", | ||
"eslint-config-google": "^0.14.0", | ||
"mocha": "^10.2.0", | ||
"nyc": "^15.1.0" | ||
"@panates/eslint-config": "^1.0.21", | ||
"@panates/eslint-config-ts": "^1.0.21", | ||
"@panates/tsconfig": "^1.0.21", | ||
"@types/jest": "^29.5.14", | ||
"husky": "^9.1.7", | ||
"jest": "^29.7.0" | ||
}, | ||
@@ -38,10 +40,13 @@ "engines": { | ||
], | ||
"nyc": { | ||
"temp-directory": "./coverage/.nyc_output" | ||
}, | ||
"scripts": { | ||
"test": "mocha --require ./test/support/env --reporter spec --bail --check-leaks test/", | ||
"cover": "nyc --reporter html --reporter text npm run test", | ||
"travis-cover": "nyc --reporter lcovonly npm run test" | ||
"lint": "eslint . --max-warnings=0", | ||
"lint:fix": "eslint . --max-warnings=0 --fix", | ||
"format": "prettier . --write --log-level=warn", | ||
"test": "jest", | ||
"precover": "rimraf coverage", | ||
"cover": "jest --runInBand --detectOpenHandles --coverage", | ||
"precitest": "rimraf coverage", | ||
"citest": "jest --coverage --coverageReporters=lcov", | ||
"prepare": "husky" | ||
} | ||
} |
39109
766
6