Coverage

100%
106
106
0

linkedlist.js

100%
106
106
0
LineHitsSource
11module.exports = function () {
222 Object.defineProperty(this, '_head', {
3 value: undefined,
4 writable: true,
5 enumerable: false,
6 configurable: false
7 })
822 Object.defineProperty(this, '_tail', {
9 value: undefined,
10 writable: true,
11 enumerable: false,
12 configurable: false
13 })
1422 Object.defineProperty(this, '_next', {
15 value: undefined,
16 writable: true,
17 enumerable: false,
18 configurable: false
19 })
2022 Object.defineProperty(this, '_length', {
21 value: 0,
22 writable: true,
23 enumerable: false,
24 configurable: false
25 })
26}
27
281module.exports.prototype.__defineGetter__('head', function () {
2921 return this._head && this._head.data
30})
31
321module.exports.prototype.__defineGetter__('tail', function () {
3321 return this._tail && this._tail.data
34})
35
361module.exports.prototype.__defineGetter__('current', function () {
3727 return this._current && this._current.data
38})
39
401module.exports.prototype.__defineGetter__('length', function () {
4124 return this._length
42})
43
441module.exports.prototype.push = function (data) {
45131 this._tail = new Item(data, this._tail)
46131 if (this._length === 0) {
4716 this._head = this._tail
4816 this._current = this._head
4916 this._next = this._head
50 }
51131 this._length++
52}
53
541module.exports.prototype.pop = function () {
5515 var tail = this._tail
5615 if (this._length === 0) {
573 return
58 }
5912 this._length--
6012 if (this._length === 0) {
611 this._head = this._tail = this._current = this._next = undefined
621 return tail.data
63 }
6411 this._tail = tail.prev
6511 this._tail.next = undefined
6611 if (this._current === tail) {
672 this._current = this._tail
682 this._next = undefined
69 }
7011 return tail.data
71}
72
731module.exports.prototype.shift = function () {
7414 var head = this._head
7514 if (this._length === 0) {
763 return
77 }
7811 this._length--
7911 if (this._length === 0) {
801 this._head = this._tail = this._current = this._next = undefined
811 return head.data
82 }
8310 this._head = this._head.next
8410 if (this._current === head) {
855 this._current = this._head
865 this._next = this._current.next
87 }
8810 return head.data
89}
90
911module.exports.prototype.unshift = function (data) {
922 this._head = new Item(data, undefined, this._head)
932 if (this._length === 0) {
941 this._tail = this._head
951 this._next = this._head
96 }
972 this._length++
98}
99
1001module.exports.prototype.unshiftCurrent = function () {
10124 var current = this._current
10224 if (current === this._head || this._length < 2) {
1032 return current && current.data
104 }
105 // remove
10622 if (current === this._tail) {
10721 this._tail = current.prev
10821 this._tail.next = undefined
10921 this._current = this._tail
110 } else {
1111 current.next.prev = current.prev
1121 current.prev.next = current.next
1131 this._current = current.prev
114 }
11522 this._next = this._current.next
116 // unshift
11722 current.next = this._head
11822 current.prev = undefined
11922 this._head.prev = current
12022 this._head = current
12122 return current.data
122}
123
1241module.exports.prototype.removeCurrent = function (data) {
12534 var current = this._current
12634 if (this._length === 0) {
1274 return
128 }
12930 this._length--
13030 if (this._length === 0) {
1313 this._head = this._tail = this._current = this._next = undefined
1323 return current.data
133 }
13427 if (current === this._tail) {
1359 this._tail = current.prev
1369 this._tail.next = undefined
1379 this._current = this._tail
13818 } else if (current === this._head) {
13912 this._head = current.next
14012 this._head.prev = undefined
14112 this._current = this._head
142 } else {
1436 current.next.prev = current.prev
1446 current.prev.next = current.next
1456 this._current = current.prev
146 }
14727 this._next = this._current.next
14827 return current.data
149}
150
1511module.exports.prototype.next = function () {
15284 var next = this._next
15384 if (next !== undefined) {
15469 this._next = next.next
15569 this._current = next
15669 return next.data
157 }
158}
159
1601module.exports.prototype.resetCursor = function () {
1611 this._current = this._next = this._head
1621 return this
163}
164
1651function Item (data, prev, next) {
166133 this.next = next
167134 if (next) next.prev = this
168133 this.prev = prev
169248 if (prev) prev.next = this
170133 this.data = data
171}