@gabrielrufino/cube 1.0.21 → 1.0.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -116,7 +116,7 @@ var BinarySearchTree = /** @class */ (function () {
116
116
  .slice(0, path.length - 1)
117
117
  .reduce(function (accumulator, current) { return accumulator && accumulator[current]; }, this._root);
118
118
  var child = path[path.length - 1];
119
- if ((current === null || current === void 0 ? void 0 : current.left) && (current === null || current === void 0 ? void 0 : current.right) && parent) {
119
+ if ((current === null || current === void 0 ? void 0 : current.left) && current.right && parent) {
120
120
  var head = current.right;
121
121
  while (head.left) {
122
122
  head = head.left;
@@ -13,16 +13,14 @@ var DoublyLinkedList = /** @class */ (function () {
13
13
  this._head = null;
14
14
  this._tail = null;
15
15
  this._size = 0;
16
- if (inputs.length) {
17
- var nodes = inputs.map(function (input) { return new Node_1.default(input); });
18
- for (var i = 0; i < inputs.length; i++) {
19
- nodes[i].previous = nodes[i - 1] || null;
20
- nodes[i].next = nodes[i + 1] || null;
21
- }
22
- this._head = nodes[0];
23
- this._tail = nodes[nodes.length - 1];
24
- this._size = inputs.length;
25
- }
16
+ var nodes = inputs.map(function (input) { return new Node_1.default(input); });
17
+ for (var i = 0; i < inputs.length; i++) {
18
+ nodes[i].previous = nodes[i - 1] || null;
19
+ nodes[i].next = nodes[i + 1] || null;
20
+ }
21
+ this._head = nodes[0];
22
+ this._tail = nodes[nodes.length - 1];
23
+ this._size = inputs.length;
26
24
  }
27
25
  Object.defineProperty(DoublyLinkedList.prototype, "data", {
28
26
  get: function () {
@@ -70,7 +68,7 @@ var DoublyLinkedList = /** @class */ (function () {
70
68
  */
71
69
  DoublyLinkedList.prototype.getFromPosition = function (position) {
72
70
  var _a, _b;
73
- if (position < 0 || position >= this._size) {
71
+ if (position < 0 || position >= this.size) {
74
72
  return undefined;
75
73
  }
76
74
  var distanceToTheHead = position;
@@ -90,9 +88,9 @@ var DoublyLinkedList = /** @class */ (function () {
90
88
  }
91
89
  if (current === null || current === void 0 ? void 0 : current.value) {
92
90
  return {
93
- previous: ((_a = current === null || current === void 0 ? void 0 : current.previous) === null || _a === void 0 ? void 0 : _a.value) || null,
94
- value: current === null || current === void 0 ? void 0 : current.value,
95
- next: ((_b = current === null || current === void 0 ? void 0 : current.next) === null || _b === void 0 ? void 0 : _b.value) || null,
91
+ previous: ((_a = current.previous) === null || _a === void 0 ? void 0 : _a.value) || null,
92
+ value: current.value,
93
+ next: ((_b = current.next) === null || _b === void 0 ? void 0 : _b.value) || null,
96
94
  };
97
95
  }
98
96
  };
@@ -205,16 +203,15 @@ var DoublyLinkedList = /** @class */ (function () {
205
203
  return 'DESC';
206
204
  };
207
205
  DoublyLinkedList.prototype[Symbol.toPrimitive] = function (type) {
208
- if (type === 'string') {
209
- return "[Head] ".concat(this.data.map(function (_a) {
206
+ var primitives = {
207
+ default: true,
208
+ number: this.size,
209
+ string: "[Head] ".concat(this.data.map(function (_a) {
210
210
  var value = _a.value;
211
211
  return value;
212
- }).join(' <=> '), " [Tail]");
213
- }
214
- if (type === 'number') {
215
- return this.size;
216
- }
217
- return null;
212
+ }).join(' <=> '), " [Tail]"),
213
+ };
214
+ return primitives[type];
218
215
  };
219
216
  return DoublyLinkedList;
220
217
  }());
@@ -4,10 +4,10 @@ interface ILinkedList<T> {
4
4
  get size(): number;
5
5
  get isEmpty(): boolean;
6
6
  push(_element: T): T;
7
- getFromPosition(_position: number): ILinkedListItem<T> | undefined;
7
+ getFromPosition(_position: number): ILinkedListItem<T> | null;
8
8
  positionOf(_element: T): number | undefined;
9
9
  insertInPosition(_element: T, _position: number): T | undefined;
10
- remove(_element: T): T | undefined;
11
- removeFromPosition(_position: number): T | undefined;
10
+ remove(_element: T): T | null;
11
+ removeFromPosition(_position: number): T | null;
12
12
  }
13
13
  export default ILinkedList;
@@ -1,4 +1,5 @@
1
1
  import ILinkedList from './ILinkedList';
2
+ import ILinkedListItem from './ILinkedListItem';
2
3
  export default class LinkedList<T = number> implements ILinkedList<T> {
3
4
  private readonly _FIRST_POSITION;
4
5
  private _head;
@@ -12,13 +13,10 @@ export default class LinkedList<T = number> implements ILinkedList<T> {
12
13
  get isEmpty(): boolean;
13
14
  positionOf(element: T): number | undefined;
14
15
  push(element: T): T;
15
- remove(element: T): T | undefined;
16
+ remove(element: T): T | null;
16
17
  insertInPosition(element: T, position: number): T | undefined;
17
- getFromPosition(position: number): {
18
- value: T;
19
- next: T | null;
20
- } | undefined;
21
- removeFromPosition(position: number): T | undefined;
22
- private getNodeFromPosition;
18
+ getFromPosition(position: number): ILinkedListItem<T> | null;
19
+ removeFromPosition(position: number): T | null;
20
+ private _getNodeFromPosition;
23
21
  private [Symbol.toPrimitive];
24
22
  }
@@ -55,7 +55,7 @@ var LinkedList = /** @class */ (function () {
55
55
  var current = this._head;
56
56
  var position = 0;
57
57
  while (current && current.value !== element) {
58
- current = current === null || current === void 0 ? void 0 : current.next;
58
+ current = current.next;
59
59
  position += 1;
60
60
  }
61
61
  if (current) {
@@ -81,6 +81,7 @@ var LinkedList = /** @class */ (function () {
81
81
  if (position) {
82
82
  return this.removeFromPosition(position);
83
83
  }
84
+ return null;
84
85
  };
85
86
  LinkedList.prototype.insertInPosition = function (element, position) {
86
87
  if (position < this._FIRST_POSITION || position > this.size) {
@@ -92,7 +93,7 @@ var LinkedList = /** @class */ (function () {
92
93
  this._head = node;
93
94
  }
94
95
  else {
95
- var before = this.getNodeFromPosition(position - 1);
96
+ var before = this._getNodeFromPosition(position - 1);
96
97
  var after = (before && before.next) || null;
97
98
  if (before) {
98
99
  before.next = node;
@@ -104,51 +105,45 @@ var LinkedList = /** @class */ (function () {
104
105
  };
105
106
  LinkedList.prototype.getFromPosition = function (position) {
106
107
  var _a;
107
- if (position < this._FIRST_POSITION || position > this.size - 1) {
108
- return undefined;
109
- }
110
- var node = this._head;
111
- for (var i = 0; i < position; i++) {
112
- node = (node === null || node === void 0 ? void 0 : node.next) || null;
108
+ var node = this._getNodeFromPosition(position);
109
+ if (!node) {
110
+ return null;
113
111
  }
114
- if (node === null || node === void 0 ? void 0 : node.value) {
115
- return {
116
- value: node.value,
117
- next: ((_a = node.next) === null || _a === void 0 ? void 0 : _a.value) || null,
118
- };
119
- }
120
- return undefined;
112
+ return {
113
+ value: node.value,
114
+ next: ((_a = node.next) === null || _a === void 0 ? void 0 : _a.value) || null,
115
+ };
121
116
  };
122
117
  LinkedList.prototype.removeFromPosition = function (position) {
123
- if (position < this._FIRST_POSITION || position > (this.size - 1)) {
124
- return undefined;
118
+ if (position < this._FIRST_POSITION || position >= this.size) {
119
+ return null;
125
120
  }
126
121
  var current = this._head;
127
122
  if (position === this._FIRST_POSITION) {
128
- this._head = (current === null || current === void 0 ? void 0 : current.next) || null;
123
+ this._head = current.next;
129
124
  }
130
125
  else {
131
126
  var previous = void 0;
132
- for (var i = 0; i < position; i++) {
127
+ for (var i = 0; i < position && current; i++) {
133
128
  previous = current;
134
- current = (current === null || current === void 0 ? void 0 : current.next) || null;
129
+ current = current.next;
135
130
  }
136
131
  if (previous) {
137
- previous.next = (current === null || current === void 0 ? void 0 : current.next) || null;
132
+ previous.next = current.next;
138
133
  }
139
134
  }
140
135
  this._size -= 1;
141
- return current === null || current === void 0 ? void 0 : current.value;
136
+ return current.value;
142
137
  };
143
- LinkedList.prototype.getNodeFromPosition = function (position) {
144
- if (position < this._FIRST_POSITION || position > this.size - 1) {
145
- return undefined;
138
+ LinkedList.prototype._getNodeFromPosition = function (position) {
139
+ if (position < this._FIRST_POSITION || position > (this.size - 1)) {
140
+ return null;
146
141
  }
147
142
  var node = this._head;
148
- for (var i = 0; i < position; i++) {
149
- node = (node === null || node === void 0 ? void 0 : node.next) || null;
143
+ for (var i = 0; i < position && (node === null || node === void 0 ? void 0 : node.next); i++) {
144
+ node = node.next;
150
145
  }
151
- return node || undefined;
146
+ return node;
152
147
  };
153
148
  LinkedList.prototype[Symbol.toPrimitive] = function (type) {
154
149
  var primitives = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gabrielrufino/cube",
3
- "version": "1.0.21",
3
+ "version": "1.0.24",
4
4
  "description": "Data Structures and Algorithms made in Typescript",
5
5
  "main": "build/index.js",
6
6
  "scripts": {