@gabrielrufino/cube 1.0.1
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.
- package/.github/workflows/cd.yml +26 -0
- package/.github/workflows/ci.yml +23 -0
- package/LICENSE +24 -0
- package/README.md +1695 -0
- package/build/Algorithms/index.d.ts +3 -0
- package/build/Algorithms/index.js +24 -0
- package/build/Array/IArray.d.ts +9 -0
- package/build/Array/IArray.js +2 -0
- package/build/Array/index.d.ts +12 -0
- package/build/Array/index.js +79 -0
- package/build/BinarySearchTree/BinarySearchTreeNode.d.ts +7 -0
- package/build/BinarySearchTree/BinarySearchTreeNode.js +22 -0
- package/build/BinarySearchTree/IBinarySearchNodeOptions.d.ts +5 -0
- package/build/BinarySearchTree/IBinarySearchNodeOptions.js +2 -0
- package/build/BinarySearchTree/IBinarySearchTree.d.ts +14 -0
- package/build/BinarySearchTree/IBinarySearchTree.js +2 -0
- package/build/BinarySearchTree/IBinarySearchTreeData.d.ts +7 -0
- package/build/BinarySearchTree/IBinarySearchTreeData.js +2 -0
- package/build/BinarySearchTree/index.d.ts +24 -0
- package/build/BinarySearchTree/index.js +199 -0
- package/build/DataStructure/index.d.ts +6 -0
- package/build/DataStructure/index.js +32 -0
- package/build/Deck/IDeck.d.ts +9 -0
- package/build/Deck/IDeck.js +2 -0
- package/build/Deck/index.d.ts +12 -0
- package/build/Deck/index.js +76 -0
- package/build/Dictionary/IDictionary.d.ts +16 -0
- package/build/Dictionary/IDictionary.js +2 -0
- package/build/Dictionary/IDictionaryData.d.ts +4 -0
- package/build/Dictionary/IDictionaryData.js +2 -0
- package/build/Dictionary/index.d.ts +21 -0
- package/build/Dictionary/index.js +110 -0
- package/build/DoublyLinkedList/IDoublyLinkedList.d.ts +16 -0
- package/build/DoublyLinkedList/IDoublyLinkedList.js +2 -0
- package/build/DoublyLinkedList/Node.d.ts +6 -0
- package/build/DoublyLinkedList/Node.js +11 -0
- package/build/DoublyLinkedList/index.d.ts +32 -0
- package/build/DoublyLinkedList/index.js +221 -0
- package/build/Graph/GraphNodeNotFoundError.d.ts +3 -0
- package/build/Graph/GraphNodeNotFoundError.js +27 -0
- package/build/Graph/GraphSearchNodeStates.d.ts +6 -0
- package/build/Graph/GraphSearchNodeStates.js +10 -0
- package/build/Graph/IGraph.d.ts +16 -0
- package/build/Graph/IGraph.js +2 -0
- package/build/Graph/IGraphOptions.d.ts +7 -0
- package/build/Graph/IGraphOptions.js +2 -0
- package/build/Graph/index.d.ts +20 -0
- package/build/Graph/index.js +187 -0
- package/build/HashTable/IHashTable.d.ts +9 -0
- package/build/HashTable/IHashTable.js +2 -0
- package/build/HashTable/IHashTableData.d.ts +4 -0
- package/build/HashTable/IHashTableData.js +2 -0
- package/build/HashTable/IHashTableInputs.d.ts +4 -0
- package/build/HashTable/IHashTableInputs.js +2 -0
- package/build/HashTable/IHashTableOptions.d.ts +4 -0
- package/build/HashTable/IHashTableOptions.js +2 -0
- package/build/HashTable/index.d.ts +16 -0
- package/build/HashTable/index.js +83 -0
- package/build/HashTableLinearProbing/HashTableLinearProbingElement.d.ts +6 -0
- package/build/HashTableLinearProbing/HashTableLinearProbingElement.js +19 -0
- package/build/HashTableLinearProbing/IHashTableLinearProbing.d.ts +9 -0
- package/build/HashTableLinearProbing/IHashTableLinearProbing.js +2 -0
- package/build/HashTableLinearProbing/IHashTableLinearProbingData.d.ts +5 -0
- package/build/HashTableLinearProbing/IHashTableLinearProbingData.js +2 -0
- package/build/HashTableLinearProbing/IHashTableLinearProbingInputs.d.ts +4 -0
- package/build/HashTableLinearProbing/IHashTableLinearProbingInputs.js +2 -0
- package/build/HashTableLinearProbing/IHashTableLinearProbingOptions.d.ts +4 -0
- package/build/HashTableLinearProbing/IHashTableLinearProbingOptions.js +2 -0
- package/build/HashTableLinearProbing/index.d.ts +20 -0
- package/build/HashTableLinearProbing/index.js +120 -0
- package/build/HashTableSeparateChaining/HashTableSeparateChainingElement.d.ts +6 -0
- package/build/HashTableSeparateChaining/HashTableSeparateChainingElement.js +21 -0
- package/build/HashTableSeparateChaining/IHashTableSeparateChaining.d.ts +10 -0
- package/build/HashTableSeparateChaining/IHashTableSeparateChaining.js +2 -0
- package/build/HashTableSeparateChaining/IHashTableSeparateChainingData.d.ts +5 -0
- package/build/HashTableSeparateChaining/IHashTableSeparateChainingData.js +2 -0
- package/build/HashTableSeparateChaining/IHashTableSeparateChainingInputs.d.ts +4 -0
- package/build/HashTableSeparateChaining/IHashTableSeparateChainingInputs.js +2 -0
- package/build/HashTableSeparateChaining/IHashTableSeparateChainingOptions.d.ts +4 -0
- package/build/HashTableSeparateChaining/IHashTableSeparateChainingOptions.js +2 -0
- package/build/HashTableSeparateChaining/index.d.ts +19 -0
- package/build/HashTableSeparateChaining/index.js +104 -0
- package/build/LinkedList/ILinkedList.d.ts +13 -0
- package/build/LinkedList/ILinkedList.js +2 -0
- package/build/LinkedList/ILinkedListItem.d.ts +5 -0
- package/build/LinkedList/ILinkedListItem.js +2 -0
- package/build/LinkedList/Node.d.ts +5 -0
- package/build/LinkedList/Node.js +10 -0
- package/build/LinkedList/index.d.ts +24 -0
- package/build/LinkedList/index.js +169 -0
- package/build/List/index.d.ts +1 -0
- package/build/List/index.js +8 -0
- package/build/MaxHeap/IMaxHeap.d.ts +9 -0
- package/build/MaxHeap/IMaxHeap.js +2 -0
- package/build/MaxHeap/IMaxHeapOptions.d.ts +5 -0
- package/build/MaxHeap/IMaxHeapOptions.js +2 -0
- package/build/MaxHeap/index.d.ts +19 -0
- package/build/MaxHeap/index.js +102 -0
- package/build/MinHeap/IMinHeap.d.ts +9 -0
- package/build/MinHeap/IMinHeap.js +2 -0
- package/build/MinHeap/IMinHeapOptions.d.ts +5 -0
- package/build/MinHeap/IMinHeapOptions.js +2 -0
- package/build/MinHeap/index.d.ts +19 -0
- package/build/MinHeap/index.js +102 -0
- package/build/Queue/IQueue.d.ts +8 -0
- package/build/Queue/IQueue.js +2 -0
- package/build/Queue/index.d.ts +11 -0
- package/build/Queue/index.js +74 -0
- package/build/Set/ISet.d.ts +15 -0
- package/build/Set/ISet.js +2 -0
- package/build/Set/index.d.ts +21 -0
- package/build/Set/index.js +132 -0
- package/build/Stack/IStack.d.ts +8 -0
- package/build/Stack/IStack.js +2 -0
- package/build/Stack/index.d.ts +11 -0
- package/build/Stack/index.js +64 -0
- package/build/index.d.ts +17 -0
- package/build/index.js +40 -0
- package/coverage/clover.xml +706 -0
- package/coverage/coverage-final.json +19 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +327 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +196 -0
- package/coverage/lcov-report/src/Algorithms/index.html +117 -0
- package/coverage/lcov-report/src/Algorithms/index.ts.html +149 -0
- package/coverage/lcov-report/src/Array/index.html +117 -0
- package/coverage/lcov-report/src/Array/index.ts.html +290 -0
- package/coverage/lcov-report/src/DataStructure/index.html +117 -0
- package/coverage/lcov-report/src/DataStructure/index.ts.html +131 -0
- package/coverage/lcov-report/src/Deck/index.html +117 -0
- package/coverage/lcov-report/src/Deck/index.ts.html +239 -0
- package/coverage/lcov-report/src/Dictionary/index.html +117 -0
- package/coverage/lcov-report/src/Dictionary/index.ts.html +350 -0
- package/coverage/lcov-report/src/DoublyLinkedList/Node.ts.html +113 -0
- package/coverage/lcov-report/src/DoublyLinkedList/index.html +132 -0
- package/coverage/lcov-report/src/DoublyLinkedList/index.ts.html +836 -0
- package/coverage/lcov-report/src/HashTable/index.html +117 -0
- package/coverage/lcov-report/src/HashTable/index.ts.html +320 -0
- package/coverage/lcov-report/src/HashTableLinearProbing/index.html +117 -0
- package/coverage/lcov-report/src/HashTableLinearProbing/index.ts.html +95 -0
- package/coverage/lcov-report/src/HashTableSeparateChaining/HashTableSeparateChainingElement.ts.html +149 -0
- package/coverage/lcov-report/src/HashTableSeparateChaining/index.html +132 -0
- package/coverage/lcov-report/src/HashTableSeparateChaining/index.ts.html +398 -0
- package/coverage/lcov-report/src/LinkedList/Node.ts.html +110 -0
- package/coverage/lcov-report/src/LinkedList/index.html +132 -0
- package/coverage/lcov-report/src/LinkedList/index.ts.html +641 -0
- package/coverage/lcov-report/src/List/index.html +117 -0
- package/coverage/lcov-report/src/List/index.ts.html +89 -0
- package/coverage/lcov-report/src/Queue/index.html +117 -0
- package/coverage/lcov-report/src/Queue/index.ts.html +218 -0
- package/coverage/lcov-report/src/Set/index.html +117 -0
- package/coverage/lcov-report/src/Set/index.ts.html +488 -0
- package/coverage/lcov-report/src/Stack/index.html +117 -0
- package/coverage/lcov-report/src/Stack/index.ts.html +215 -0
- package/coverage/lcov-report/src/index.html +117 -0
- package/coverage/lcov-report/src/index.ts.html +125 -0
- package/coverage/lcov.info +1346 -0
- package/package.json +42 -0
- package/src/Algorithms/index.spec.ts +15 -0
- package/src/Algorithms/index.ts +21 -0
- package/src/Array/IArray.ts +10 -0
- package/src/Array/index.spec.ts +151 -0
- package/src/Array/index.ts +68 -0
- package/src/BinarySearchTree/BinarySearchTreeNode.spec.ts +29 -0
- package/src/BinarySearchTree/BinarySearchTreeNode.ts +23 -0
- package/src/BinarySearchTree/IBinarySearchNodeOptions.ts +6 -0
- package/src/BinarySearchTree/IBinarySearchTree.ts +16 -0
- package/src/BinarySearchTree/IBinarySearchTreeData.ts +9 -0
- package/src/BinarySearchTree/index.spec.ts +486 -0
- package/src/BinarySearchTree/index.ts +198 -0
- package/src/DataStructure/index.spec.ts +25 -0
- package/src/DataStructure/index.ts +15 -0
- package/src/Deck/IDeck.ts +10 -0
- package/src/Deck/index.spec.ts +131 -0
- package/src/Deck/index.ts +51 -0
- package/src/Dictionary/IDictionary.ts +18 -0
- package/src/Dictionary/IDictionaryData.ts +5 -0
- package/src/Dictionary/index.spec.ts +310 -0
- package/src/Dictionary/index.ts +88 -0
- package/src/DoublyLinkedList/IDoublyLinkedList.ts +18 -0
- package/src/DoublyLinkedList/Node.ts +9 -0
- package/src/DoublyLinkedList/index.spec.ts +478 -0
- package/src/DoublyLinkedList/index.ts +250 -0
- package/src/Graph/GraphNodeNotFoundError.ts +7 -0
- package/src/Graph/GraphSearchNodeStates.ts +8 -0
- package/src/Graph/IGraph.ts +15 -0
- package/src/Graph/IGraphOptions.ts +6 -0
- package/src/Graph/index.spec.ts +318 -0
- package/src/Graph/index.ts +170 -0
- package/src/HashTable/IHashTable.ts +11 -0
- package/src/HashTable/IHashTableData.ts +5 -0
- package/src/HashTable/IHashTableInputs.ts +5 -0
- package/src/HashTable/IHashTableOptions.ts +5 -0
- package/src/HashTable/index.spec.ts +136 -0
- package/src/HashTable/index.ts +78 -0
- package/src/HashTableLinearProbing/HashTableLinearProbingElement.spec.ts +28 -0
- package/src/HashTableLinearProbing/HashTableLinearProbingElement.ts +18 -0
- package/src/HashTableLinearProbing/IHashTableLinearProbing.ts +11 -0
- package/src/HashTableLinearProbing/IHashTableLinearProbingData.ts +7 -0
- package/src/HashTableLinearProbing/IHashTableLinearProbingInputs.ts +5 -0
- package/src/HashTableLinearProbing/IHashTableLinearProbingOptions.ts +5 -0
- package/src/HashTableLinearProbing/index.spec.ts +266 -0
- package/src/HashTableLinearProbing/index.ts +128 -0
- package/src/HashTableSeparateChaining/HashTableSeparateChainingElement.ts +21 -0
- package/src/HashTableSeparateChaining/IHashTableSeparateChaining.ts +12 -0
- package/src/HashTableSeparateChaining/IHashTableSeparateChainingData.ts +7 -0
- package/src/HashTableSeparateChaining/IHashTableSeparateChainingInputs.ts +5 -0
- package/src/HashTableSeparateChaining/IHashTableSeparateChainingOptions.ts +5 -0
- package/src/HashTableSeparateChaining/index.spec.ts +173 -0
- package/src/HashTableSeparateChaining/index.ts +104 -0
- package/src/LinkedList/ILinkedList.ts +15 -0
- package/src/LinkedList/ILinkedListItem.ts +6 -0
- package/src/LinkedList/Node.ts +8 -0
- package/src/LinkedList/index.spec.ts +355 -0
- package/src/LinkedList/index.ts +185 -0
- package/src/List/index.ts +1 -0
- package/src/MaxHeap/IMaxHeap.ts +10 -0
- package/src/MaxHeap/IMaxHeapOptions.ts +6 -0
- package/src/MaxHeap/index.spec.ts +161 -0
- package/src/MaxHeap/index.ts +91 -0
- package/src/MinHeap/IMinHeap.ts +10 -0
- package/src/MinHeap/IMinHeapOptions.ts +6 -0
- package/src/MinHeap/index.spec.ts +161 -0
- package/src/MinHeap/index.ts +91 -0
- package/src/Queue/IQueue.ts +9 -0
- package/src/Queue/index.spec.ts +92 -0
- package/src/Queue/index.ts +44 -0
- package/src/Set/ISet.ts +17 -0
- package/src/Set/index.spec.ts +246 -0
- package/src/Set/index.ts +134 -0
- package/src/Stack/IStack.ts +9 -0
- package/src/Stack/index.spec.ts +108 -0
- package/src/Stack/index.ts +43 -0
- package/src/index.ts +17 -0
- package/tsconfig.json +104 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import {describe, it, expect} from '@jest/globals';
|
|
2
|
+
|
|
3
|
+
import Deck from './';
|
|
4
|
+
|
|
5
|
+
describe('Deck', () => {
|
|
6
|
+
it('Should create an empty deck without problems', () => {
|
|
7
|
+
const deck = new Deck();
|
|
8
|
+
|
|
9
|
+
expect(deck.data).toEqual([]);
|
|
10
|
+
expect(deck.size).toBe(0);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('Should create a filled deck without problems', () => {
|
|
14
|
+
const deck = new Deck(1, 2, 3, 4);
|
|
15
|
+
|
|
16
|
+
expect(deck.data).toEqual([1, 2, 3, 4]);
|
|
17
|
+
expect(deck.size).toBe(4);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
describe('.addFront()', () => {
|
|
21
|
+
it('Should add an element in the front of the deck', () => {
|
|
22
|
+
const deck = new Deck(2, 3, 4);
|
|
23
|
+
deck.addFront(1);
|
|
24
|
+
|
|
25
|
+
expect(deck.data).toEqual([1, 2, 3, 4]);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('Should return the inserted element', () => {
|
|
29
|
+
const deck = new Deck(2, 3, 4);
|
|
30
|
+
const returned = deck.addFront(1);
|
|
31
|
+
|
|
32
|
+
expect(returned).toBe(1);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
describe('.addBack()', () => {
|
|
37
|
+
it('Should add an element in the back of the deck', () => {
|
|
38
|
+
const deck = new Deck(1, 2, 3);
|
|
39
|
+
deck.addBack(4);
|
|
40
|
+
|
|
41
|
+
expect(deck.data).toEqual([1, 2, 3, 4]);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('Should return the inserted element', () => {
|
|
45
|
+
const deck = new Deck(1, 2, 3);
|
|
46
|
+
const returned = deck.addBack(4);
|
|
47
|
+
|
|
48
|
+
expect(returned).toBe(4);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe('.removeFront()', () => {
|
|
53
|
+
it('Should remove the element in the front of the deck', () => {
|
|
54
|
+
const deck = new Deck(1, 2, 3, 4);
|
|
55
|
+
deck.removeFront();
|
|
56
|
+
|
|
57
|
+
expect(deck.data).toEqual([2, 3, 4]);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('Should return the removed element', () => {
|
|
61
|
+
const deck = new Deck(1, 2, 3, 4);
|
|
62
|
+
const returned = deck.removeFront();
|
|
63
|
+
|
|
64
|
+
expect(returned).toBe(1);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe('.removeBack()', () => {
|
|
69
|
+
it('Should remove the element in the back of the deck', () => {
|
|
70
|
+
const deck = new Deck(1, 2, 3, 4);
|
|
71
|
+
deck.removeBack();
|
|
72
|
+
|
|
73
|
+
expect(deck.data).toEqual([1, 2, 3]);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('Should return the removed element', () => {
|
|
77
|
+
const deck = new Deck(1, 2, 3, 4);
|
|
78
|
+
const returned = deck.removeBack();
|
|
79
|
+
|
|
80
|
+
expect(returned).toBe(4);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe('.peekFront()', () => {
|
|
85
|
+
it('Should peek the element in the front of the deck', () => {
|
|
86
|
+
const deck = new Deck(1, 2, 3, 4);
|
|
87
|
+
const returned = deck.peekFront();
|
|
88
|
+
|
|
89
|
+
expect(returned).toBe(1);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('Should not remove the element in the front os the deck', () => {
|
|
93
|
+
const deck = new Deck(1, 2, 3, 4);
|
|
94
|
+
deck.peekFront();
|
|
95
|
+
|
|
96
|
+
expect(deck.data).toEqual([1, 2, 3, 4]);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe('.peekBack()', () => {
|
|
101
|
+
it('Should peek the element in the back of the deck', () => {
|
|
102
|
+
const deck = new Deck(1, 2, 3, 4);
|
|
103
|
+
const returned = deck.peekBack();
|
|
104
|
+
|
|
105
|
+
expect(returned).toBe(4);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('Should not remove the element in the back os the deck', () => {
|
|
109
|
+
const deck = new Deck(1, 2, 3, 4);
|
|
110
|
+
deck.peekBack();
|
|
111
|
+
|
|
112
|
+
expect(deck.data).toEqual([1, 2, 3, 4]);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe('Conversion to primitive', () => {
|
|
117
|
+
it('Should return comma-separated elements in string conversion', () => {
|
|
118
|
+
const deck = new Deck(1, 2, 3, 4);
|
|
119
|
+
const string = String(deck);
|
|
120
|
+
|
|
121
|
+
expect(string).toBe('[Front] 1, 2, 3, 4 [Back]');
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('Should return the deck size in number conversion', () => {
|
|
125
|
+
const deck = new Deck(1, 2, 3, 4);
|
|
126
|
+
const number = Number(deck);
|
|
127
|
+
|
|
128
|
+
expect(number).toBe(4);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import DataStructure from '../DataStructure';
|
|
2
|
+
import IDeck from './IDeck';
|
|
3
|
+
|
|
4
|
+
export default class Deck<T = number> extends DataStructure<T> implements IDeck<T> {
|
|
5
|
+
constructor(...inputs: T[]) {
|
|
6
|
+
super(inputs);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
public addFront(element: T): T {
|
|
10
|
+
this._data = [element, ...this.data];
|
|
11
|
+
return element;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public addBack(element: T): T {
|
|
15
|
+
this._data = [...this.data, element];
|
|
16
|
+
return element;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public removeFront(): T | undefined {
|
|
20
|
+
const [element, ...rest] = this.data;
|
|
21
|
+
this._data = rest;
|
|
22
|
+
return element;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public removeBack(): T | undefined {
|
|
26
|
+
const element = this._data.pop();
|
|
27
|
+
return element;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public peekFront(): T | undefined {
|
|
31
|
+
const [element] = this.data;
|
|
32
|
+
return element;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public peekBack(): T | undefined {
|
|
36
|
+
const element = this.data[this.size - 1];
|
|
37
|
+
return element;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
private [Symbol.toPrimitive](type: string): string | number | null {
|
|
41
|
+
if (type === 'string') {
|
|
42
|
+
return `[Front] ${this.data.join(', ')} [Back]`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (type === 'number') {
|
|
46
|
+
return this.size;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import IDictionaryData from './IDictionaryData';
|
|
2
|
+
|
|
3
|
+
interface IDictionary<T> {
|
|
4
|
+
get data(): IDictionaryData<T>;
|
|
5
|
+
get size(): number;
|
|
6
|
+
get isEmpty(): boolean;
|
|
7
|
+
get keys(): string[];
|
|
8
|
+
get values(): T[];
|
|
9
|
+
get pairs(): [string, T][]
|
|
10
|
+
set(_key: string, _value: T): [string, T];
|
|
11
|
+
remove(_key: string): [string, T] | null;
|
|
12
|
+
hasKey(_key: string): boolean;
|
|
13
|
+
get(_key: string): T | null;
|
|
14
|
+
clear(): IDictionaryData<T>
|
|
15
|
+
forEach(_func: (_key: string, _value: T) => any): void;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default IDictionary;
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import {describe, it, expect, jest} from '@jest/globals';
|
|
2
|
+
|
|
3
|
+
import Dictionary from './';
|
|
4
|
+
|
|
5
|
+
describe('Dictionary', () => {
|
|
6
|
+
it('Should create an empty dictionary without problems', () => {
|
|
7
|
+
const dictionary = new Dictionary<number>();
|
|
8
|
+
|
|
9
|
+
expect(dictionary.data).toEqual({});
|
|
10
|
+
expect(dictionary.size).toBe(0);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('Should create a filled dictionary without problems', () => {
|
|
14
|
+
const dictionary = new Dictionary<number>({
|
|
15
|
+
first: 1,
|
|
16
|
+
second: 2,
|
|
17
|
+
third: 3,
|
|
18
|
+
fourth: 4,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
expect(dictionary.data).toEqual({
|
|
22
|
+
first: 1,
|
|
23
|
+
second: 2,
|
|
24
|
+
third: 3,
|
|
25
|
+
fourth: 4,
|
|
26
|
+
});
|
|
27
|
+
expect(dictionary.size).toBe(4);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe('.isEmpty', () => {
|
|
31
|
+
it('Should return true when the dictionary has no key value pairs', () => {
|
|
32
|
+
const dictionary = new Dictionary({});
|
|
33
|
+
|
|
34
|
+
expect(dictionary.isEmpty).toBe(true);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('Should return false when the dictionary has some key value pair', () => {
|
|
38
|
+
const dictionary = new Dictionary({
|
|
39
|
+
first: 1,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
expect(dictionary.isEmpty).toBe(false);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe('.keys', () => {
|
|
47
|
+
it('Should return an array with all the dictionary keys', () => {
|
|
48
|
+
const dictionary = new Dictionary<number>({
|
|
49
|
+
first: 1,
|
|
50
|
+
second: 2,
|
|
51
|
+
third: 3,
|
|
52
|
+
fourth: 4,
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
expect(dictionary.keys.sort()).toEqual(['first', 'second', 'third', 'fourth'].sort());
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe('.values', () => {
|
|
60
|
+
it('Should return an array with all the dictionary values', () => {
|
|
61
|
+
const dictionary = new Dictionary<number>({
|
|
62
|
+
first: 1,
|
|
63
|
+
second: 2,
|
|
64
|
+
third: 3,
|
|
65
|
+
fourth: 4,
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
expect(dictionary.values.sort()).toEqual([1, 2, 3, 4].sort());
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
describe('.pairs', () => {
|
|
73
|
+
it('Should return an array of key value pairs', () => {
|
|
74
|
+
const dictionary = new Dictionary<number>({
|
|
75
|
+
first: 1,
|
|
76
|
+
second: 2,
|
|
77
|
+
third: 3,
|
|
78
|
+
fourth: 4,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
expect(dictionary.pairs.sort())
|
|
82
|
+
.toEqual([['first', 1], ['second', 2], ['third', 3], ['fourth', 4]].sort());
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
describe('.set()', () => {
|
|
87
|
+
it('Should set a new key with a value in the dictionary', () => {
|
|
88
|
+
const dictionary = new Dictionary({
|
|
89
|
+
first: 1,
|
|
90
|
+
});
|
|
91
|
+
dictionary.set('second', 2);
|
|
92
|
+
|
|
93
|
+
expect(dictionary.data).toEqual({
|
|
94
|
+
first: 1,
|
|
95
|
+
second: 2,
|
|
96
|
+
});
|
|
97
|
+
expect(dictionary.size).toBe(2);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('Should replace a key value pair when receive an already existent key', () => {
|
|
101
|
+
const dictionary = new Dictionary({
|
|
102
|
+
first: 1,
|
|
103
|
+
});
|
|
104
|
+
dictionary.set('first', 2);
|
|
105
|
+
|
|
106
|
+
expect(dictionary.data).toEqual({
|
|
107
|
+
first: 2,
|
|
108
|
+
});
|
|
109
|
+
expect(dictionary.size).toBe(1);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('Should return the inserted key valur pair', () => {
|
|
113
|
+
const dictionary = new Dictionary();
|
|
114
|
+
const returned = dictionary.set('first', 1);
|
|
115
|
+
|
|
116
|
+
expect(returned).toEqual(['first', 1]);
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
describe('.remove()', () => {
|
|
121
|
+
it('Should remove an existent key value pair', () => {
|
|
122
|
+
const dictionary = new Dictionary({
|
|
123
|
+
first: 1,
|
|
124
|
+
second: 2,
|
|
125
|
+
third: 3,
|
|
126
|
+
fourth: 4,
|
|
127
|
+
});
|
|
128
|
+
dictionary.remove('third');
|
|
129
|
+
|
|
130
|
+
expect(dictionary.data).toEqual({
|
|
131
|
+
first: 1,
|
|
132
|
+
second: 2,
|
|
133
|
+
fourth: 4,
|
|
134
|
+
});
|
|
135
|
+
expect(dictionary.size).toBe(3);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('Should change nothing when receive a non existent key', () => {
|
|
139
|
+
const dictionary = new Dictionary({
|
|
140
|
+
first: 1,
|
|
141
|
+
second: 2,
|
|
142
|
+
third: 3,
|
|
143
|
+
fourth: 4,
|
|
144
|
+
});
|
|
145
|
+
dictionary.remove('fifth');
|
|
146
|
+
|
|
147
|
+
expect(dictionary.data).toEqual({
|
|
148
|
+
first: 1,
|
|
149
|
+
second: 2,
|
|
150
|
+
third: 3,
|
|
151
|
+
fourth: 4,
|
|
152
|
+
});
|
|
153
|
+
expect(dictionary.size).toBe(4);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('Should return the removed key value pair', () => {
|
|
157
|
+
const dictionary = new Dictionary({
|
|
158
|
+
first: 1,
|
|
159
|
+
second: 2,
|
|
160
|
+
third: 3,
|
|
161
|
+
fourth: 4,
|
|
162
|
+
});
|
|
163
|
+
const returned = dictionary.remove('second');
|
|
164
|
+
|
|
165
|
+
expect(returned).toEqual(['second', 2]);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it('Should return null when the dictionary has not the received key', () => {
|
|
169
|
+
const dictionary = new Dictionary({
|
|
170
|
+
first: 1,
|
|
171
|
+
second: 2,
|
|
172
|
+
third: 3,
|
|
173
|
+
fourth: 4,
|
|
174
|
+
});
|
|
175
|
+
const returned = dictionary.remove('fifth');
|
|
176
|
+
|
|
177
|
+
expect(returned).toBeNull();
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
describe('.hasKey()', () => {
|
|
182
|
+
it('Should return truw when the dictionary has the received key', () => {
|
|
183
|
+
const dictionary = new Dictionary({
|
|
184
|
+
first: 1,
|
|
185
|
+
second: 2,
|
|
186
|
+
third: 3,
|
|
187
|
+
fourth: 4,
|
|
188
|
+
});
|
|
189
|
+
const returned = dictionary.hasKey('first');
|
|
190
|
+
|
|
191
|
+
expect(returned).toBe(true);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it('Should return false when the dictionary has not the received key', () => {
|
|
195
|
+
const dictionary = new Dictionary({
|
|
196
|
+
first: 1,
|
|
197
|
+
second: 2,
|
|
198
|
+
third: 3,
|
|
199
|
+
fourth: 4,
|
|
200
|
+
});
|
|
201
|
+
const returned = dictionary.hasKey('fifth');
|
|
202
|
+
|
|
203
|
+
expect(returned).toBe(false);
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
describe('.get()', () => {
|
|
208
|
+
it('Should return the value of the received key', () => {
|
|
209
|
+
const dictionary = new Dictionary<number>({
|
|
210
|
+
first: 1,
|
|
211
|
+
second: 2,
|
|
212
|
+
third: 3,
|
|
213
|
+
fourth: 4,
|
|
214
|
+
});
|
|
215
|
+
const returned = dictionary.get('third');
|
|
216
|
+
|
|
217
|
+
expect(returned).toBe(3);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it('Should return null when the dictionary has not the received key', () => {
|
|
221
|
+
const dictionary = new Dictionary<number>({
|
|
222
|
+
first: 1,
|
|
223
|
+
second: 2,
|
|
224
|
+
third: 3,
|
|
225
|
+
fourth: 4,
|
|
226
|
+
});
|
|
227
|
+
const returned = dictionary.get('fifth');
|
|
228
|
+
|
|
229
|
+
expect(returned).toBeNull();
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
describe('.clear()', () => {
|
|
234
|
+
it('Should clear the dictionary', () => {
|
|
235
|
+
const dictionary = new Dictionary({
|
|
236
|
+
first: 1,
|
|
237
|
+
second: 2,
|
|
238
|
+
third: 3,
|
|
239
|
+
fourth: 3,
|
|
240
|
+
});
|
|
241
|
+
dictionary.clear();
|
|
242
|
+
|
|
243
|
+
expect(dictionary.data).toEqual({});
|
|
244
|
+
expect(dictionary.size).toBe(0);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it('Should return the dictionary key values pairs befores the clear', () => {
|
|
248
|
+
const dictionary = new Dictionary({
|
|
249
|
+
first: 1,
|
|
250
|
+
second: 2,
|
|
251
|
+
third: 3,
|
|
252
|
+
fourth: 3,
|
|
253
|
+
});
|
|
254
|
+
const returned = dictionary.clear();
|
|
255
|
+
|
|
256
|
+
expect(returned).toEqual({
|
|
257
|
+
first: 1,
|
|
258
|
+
second: 2,
|
|
259
|
+
third: 3,
|
|
260
|
+
fourth: 3,
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
describe('.forEach()', () => {
|
|
266
|
+
it('Should call the received function for each elements', () => {
|
|
267
|
+
const dictionary = new Dictionary<number>({
|
|
268
|
+
first: 1,
|
|
269
|
+
second: 2,
|
|
270
|
+
third: 3,
|
|
271
|
+
fourth: 4,
|
|
272
|
+
});
|
|
273
|
+
const func = jest.fn();
|
|
274
|
+
|
|
275
|
+
dictionary.forEach(func);
|
|
276
|
+
|
|
277
|
+
expect(func).toBeCalledTimes(4);
|
|
278
|
+
expect(func).toBeCalledWith('first', 1);
|
|
279
|
+
expect(func).toBeCalledWith('second', 2);
|
|
280
|
+
expect(func).toBeCalledWith('third', 3);
|
|
281
|
+
expect(func).toBeCalledWith('fourth', 4);
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
describe('Conversion to primitive', () => {
|
|
286
|
+
it('Should return a comma separated key value pairs in string conversion', () => {
|
|
287
|
+
const dictionary = new Dictionary<number>({
|
|
288
|
+
first: 1,
|
|
289
|
+
second: 2,
|
|
290
|
+
third: 3,
|
|
291
|
+
fourth: 4,
|
|
292
|
+
});
|
|
293
|
+
const string = String(dictionary);
|
|
294
|
+
|
|
295
|
+
expect(string).toBe('{ first => 1, second => 2, third => 3, fourth => 4 }');
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it('Should return the dictionary size in number conversion', () => {
|
|
299
|
+
const dictionary = new Dictionary<number>({
|
|
300
|
+
first: 1,
|
|
301
|
+
second: 2,
|
|
302
|
+
third: 3,
|
|
303
|
+
fourth: 4,
|
|
304
|
+
});
|
|
305
|
+
const number = Number(dictionary);
|
|
306
|
+
|
|
307
|
+
expect(number).toBe(4);
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
});
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import IDictionary from './IDictionary';
|
|
2
|
+
import IDictionaryData from './IDictionaryData';
|
|
3
|
+
|
|
4
|
+
export default class Dictionary<T = number> implements IDictionary<T> {
|
|
5
|
+
private _data: IDictionaryData<T> = {};
|
|
6
|
+
|
|
7
|
+
constructor(inputs: { [key: string]: T } = {}) {
|
|
8
|
+
this._data = inputs;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
get data(): IDictionaryData<T> {
|
|
12
|
+
return {...this._data};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
get size(): number {
|
|
16
|
+
return Reflect.ownKeys(this.data).length;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
get isEmpty(): boolean {
|
|
20
|
+
return this.size === 0;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
get keys(): string[] {
|
|
24
|
+
return Reflect
|
|
25
|
+
.ownKeys(this.data)
|
|
26
|
+
.map(key => String(key));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
get values(): T[] {
|
|
30
|
+
return Object.values(this.data);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
get pairs(): [string, T][] {
|
|
34
|
+
return Object.entries(this.data);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public set(key: string, value: T): [string, T] {
|
|
38
|
+
Reflect.set(this._data, key, value);
|
|
39
|
+
return [key, value];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public remove(key: string): [string, T] | null {
|
|
43
|
+
if (this.hasKey(key)) {
|
|
44
|
+
const value = Reflect.get(this.data, key);
|
|
45
|
+
Reflect.deleteProperty(this._data, key);
|
|
46
|
+
|
|
47
|
+
return [key, value];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
public hasKey(key: string): boolean {
|
|
54
|
+
return Reflect.has(this.data, key);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
public get(key: string): T | null {
|
|
58
|
+
if (this.hasKey(key)) {
|
|
59
|
+
return Reflect.get(this.data, key);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
public clear(): IDictionaryData<T> {
|
|
66
|
+
const dictionary = {...this.data};
|
|
67
|
+
this._data = {};
|
|
68
|
+
return dictionary;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public forEach(func: (_key: string, _value: T) => any): void {
|
|
72
|
+
for (const [key, value] of this.pairs) {
|
|
73
|
+
func(key, value);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private[Symbol.toPrimitive](type: string): string | number | null {
|
|
78
|
+
if (type === 'string') {
|
|
79
|
+
return `{ ${this.pairs.map(([key, value]) => `${key} => ${value}`).join(', ')} }`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (type === 'number') {
|
|
83
|
+
return this.size;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface IDoublyLinkedListItem<T> {
|
|
2
|
+
previous: T | null;
|
|
3
|
+
value: T;
|
|
4
|
+
next: T | null;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
interface IDoublyLinkedList<T> {
|
|
8
|
+
get data(): IDoublyLinkedListItem<T>[];
|
|
9
|
+
get size(): number;
|
|
10
|
+
push(_element: T): T;
|
|
11
|
+
getFromPosition(_position: number): IDoublyLinkedListItem<T> | undefined;
|
|
12
|
+
positionOf(_element: T): number | undefined;
|
|
13
|
+
insertInPosition(_element: T, _position: number): T | undefined;
|
|
14
|
+
remove(_element: T): T | undefined;
|
|
15
|
+
removeFromPosition(_position: number): T | undefined;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default IDoublyLinkedList;
|