@gabrielrufino/cube 1.0.20 → 1.0.23

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.
@@ -3,5 +3,5 @@ export default class BinarySearchTreeNode<T> {
3
3
  left: BinarySearchTreeNode<T> | null;
4
4
  right: BinarySearchTreeNode<T> | null;
5
5
  constructor(value: T);
6
- [Symbol.toPrimitive](type: string): string | number | null;
6
+ private [Symbol.toPrimitive];
7
7
  }
@@ -9,13 +9,12 @@ var BinarySearchTreeNode = /** @class */ (function () {
9
9
  this.right = null;
10
10
  }
11
11
  BinarySearchTreeNode.prototype[Symbol.toPrimitive] = function (type) {
12
- if (type === 'string') {
13
- return "[".concat(this.left, "] <= (").concat(this.value, ") => [").concat(this.right, "]");
14
- }
15
- if (type === 'number') {
16
- return 1 + (this.left ? 1 : 0) + (this.right ? 1 : 0);
17
- }
18
- return null;
12
+ var primitives = {
13
+ default: true,
14
+ number: 1 + (this.left ? 1 : 0) + (this.right ? 1 : 0),
15
+ string: "[".concat(this.left, "] <= (").concat(this.value, ") => [").concat(this.right, "]"),
16
+ };
17
+ return primitives[type];
19
18
  };
20
19
  return BinarySearchTreeNode;
21
20
  }());
@@ -17,7 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  var BinarySearchTreeNode_1 = __importDefault(require("./BinarySearchTreeNode"));
18
18
  var BinarySearchTree = /** @class */ (function () {
19
19
  function BinarySearchTree(_a) {
20
- var _b = _a === void 0 ? { inputs: [] } : _a, _c = _b.inputs, inputs = _c === void 0 ? [] : _c, lessThanOrEqualTo = _b.lessThanOrEqualTo;
20
+ var _b = _a === void 0 ? {} : _a, _c = _b.inputs, inputs = _c === void 0 ? [] : _c, lessThanOrEqualTo = _b.lessThanOrEqualTo;
21
21
  this._root = null;
22
22
  this._size = 0;
23
23
  if (lessThanOrEqualTo) {
@@ -183,13 +183,12 @@ var BinarySearchTree = /** @class */ (function () {
183
183
  }
184
184
  };
185
185
  BinarySearchTree.prototype[Symbol.toPrimitive] = function (type) {
186
- if (type === 'string') {
187
- return "".concat(this._root);
188
- }
189
- if (type === 'number') {
190
- return this.size;
191
- }
192
- return null;
186
+ var primitives = {
187
+ default: true,
188
+ number: this.size,
189
+ string: "".concat(this._root),
190
+ };
191
+ return primitives[type];
193
192
  };
194
193
  return BinarySearchTree;
195
194
  }());
@@ -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
  }
@@ -13,14 +13,12 @@ var LinkedList = /** @class */ (function () {
13
13
  this._FIRST_POSITION = 0;
14
14
  this._head = 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 - 1; i++) {
19
- nodes[i].next = nodes[i + 1];
20
- }
21
- this._head = nodes[0];
22
- this._size = nodes.length;
16
+ var nodes = inputs.map(function (input) { return new Node_1.default(input); });
17
+ for (var i = 0; i < inputs.length - 1; i++) {
18
+ nodes[i].next = nodes[i + 1];
23
19
  }
20
+ this._head = nodes[0];
21
+ this._size = nodes.length;
24
22
  }
25
23
  Object.defineProperty(LinkedList.prototype, "data", {
26
24
  get: function () {
@@ -57,7 +55,7 @@ var LinkedList = /** @class */ (function () {
57
55
  var current = this._head;
58
56
  var position = 0;
59
57
  while (current && current.value !== element) {
60
- current = current === null || current === void 0 ? void 0 : current.next;
58
+ current = current.next;
61
59
  position += 1;
62
60
  }
63
61
  if (current) {
@@ -83,6 +81,7 @@ var LinkedList = /** @class */ (function () {
83
81
  if (position) {
84
82
  return this.removeFromPosition(position);
85
83
  }
84
+ return null;
86
85
  };
87
86
  LinkedList.prototype.insertInPosition = function (element, position) {
88
87
  if (position < this._FIRST_POSITION || position > this.size) {
@@ -94,7 +93,7 @@ var LinkedList = /** @class */ (function () {
94
93
  this._head = node;
95
94
  }
96
95
  else {
97
- var before = this.getNodeFromPosition(position - 1);
96
+ var before = this._getNodeFromPosition(position - 1);
98
97
  var after = (before && before.next) || null;
99
98
  if (before) {
100
99
  before.next = node;
@@ -106,63 +105,56 @@ var LinkedList = /** @class */ (function () {
106
105
  };
107
106
  LinkedList.prototype.getFromPosition = function (position) {
108
107
  var _a;
109
- if (position < this._FIRST_POSITION || position > this.size - 1) {
110
- return undefined;
111
- }
112
- var node = this._head;
113
- for (var i = 0; i < position; i++) {
114
- node = (node === null || node === void 0 ? void 0 : node.next) || null;
115
- }
116
- if (node === null || node === void 0 ? void 0 : node.value) {
117
- return {
118
- value: node.value,
119
- next: ((_a = node.next) === null || _a === void 0 ? void 0 : _a.value) || null,
120
- };
121
- }
122
- return undefined;
108
+ var node = this._getNodeFromPosition(position);
109
+ if (!node) {
110
+ return null;
111
+ }
112
+ return {
113
+ value: node.value,
114
+ next: ((_a = node.next) === null || _a === void 0 ? void 0 : _a.value) || null,
115
+ };
123
116
  };
124
117
  LinkedList.prototype.removeFromPosition = function (position) {
125
- if (position < this._FIRST_POSITION || position > (this.size - 1)) {
126
- return undefined;
118
+ if (position < this._FIRST_POSITION || position >= this.size) {
119
+ return null;
127
120
  }
128
121
  var current = this._head;
129
122
  if (position === this._FIRST_POSITION) {
130
- this._head = (current === null || current === void 0 ? void 0 : current.next) || null;
123
+ this._head = current.next;
131
124
  }
132
125
  else {
133
126
  var previous = void 0;
134
- for (var i = 0; i < position; i++) {
127
+ for (var i = 0; i < position && current; i++) {
135
128
  previous = current;
136
- current = (current === null || current === void 0 ? void 0 : current.next) || null;
129
+ current = current.next;
137
130
  }
138
131
  if (previous) {
139
- previous.next = (current === null || current === void 0 ? void 0 : current.next) || null;
132
+ previous.next = current.next;
140
133
  }
141
134
  }
142
135
  this._size -= 1;
143
- return current === null || current === void 0 ? void 0 : current.value;
136
+ return current.value;
144
137
  };
145
- LinkedList.prototype.getNodeFromPosition = function (position) {
146
- if (position < this._FIRST_POSITION || position > this.size - 1) {
147
- return undefined;
138
+ LinkedList.prototype._getNodeFromPosition = function (position) {
139
+ if (position < this._FIRST_POSITION || position > (this.size - 1)) {
140
+ return null;
148
141
  }
149
142
  var node = this._head;
150
- for (var i = 0; i < position; i++) {
151
- 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;
152
145
  }
153
- return node || undefined;
146
+ return node;
154
147
  };
155
148
  LinkedList.prototype[Symbol.toPrimitive] = function (type) {
156
- if (type === 'string') {
157
- return "[Head] ".concat(this.data.map(function (_a) {
149
+ var primitives = {
150
+ default: true,
151
+ number: this.size,
152
+ string: "[Head] ".concat(this.data.map(function (_a) {
158
153
  var value = _a.value;
159
154
  return value;
160
- }).join(' => '));
161
- }
162
- if (type === 'number') {
163
- return this.size;
164
- }
165
- return null;
155
+ }).join(' => ')),
156
+ };
157
+ return primitives[type];
166
158
  };
167
159
  return LinkedList;
168
160
  }());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gabrielrufino/cube",
3
- "version": "1.0.20",
3
+ "version": "1.0.23",
4
4
  "description": "Data Structures and Algorithms made in Typescript",
5
5
  "main": "build/index.js",
6
6
  "scripts": {