@@ -12,105 +12,69 @@ describe('LinkedList', () => {
12
12
expect ( linkedList . head ) . toBeNull ( ) ;
13
13
expect ( linkedList . tail ) . toBeNull ( ) ;
14
14
15
- const node1 = linkedList . append ( { value : 1 } ) ;
16
- const node2 = linkedList . append ( { value : 2 , key : 'test' } ) ;
15
+ linkedList . append ( 1 ) ;
16
+ linkedList . append ( 2 ) ;
17
17
18
- expect ( node1 . value ) . toBe ( 1 ) ;
19
- expect ( node2 . value ) . toBe ( 2 ) ;
20
- expect ( node2 . key ) . toBe ( 'test' ) ;
21
-
22
- expect ( linkedList . head . toString ( ) ) . toBe ( '1' ) ;
23
- expect ( linkedList . tail . toString ( ) ) . toBe ( 'test:2' ) ;
24
-
25
- expect ( linkedList . toString ( ) ) . toBe ( '1,test:2' ) ;
18
+ expect ( linkedList . toString ( ) ) . toBe ( '1,2' ) ;
26
19
} ) ;
27
20
28
21
it ( 'should prepend node to linked list' , ( ) => {
29
22
const linkedList = new LinkedList ( ) ;
30
23
31
- const node1 = linkedList . append ( { value : 1 } ) ;
32
- const node2 = linkedList . prepend ( { value : 2 } ) ;
33
-
34
- expect ( node1 . value ) . toBe ( 1 ) ;
35
- expect ( node2 . value ) . toBe ( 2 ) ;
36
-
37
- expect ( linkedList . head . toString ( ) ) . toBe ( '2' ) ;
38
- expect ( linkedList . tail . toString ( ) ) . toBe ( '1' ) ;
24
+ linkedList . append ( 1 ) ;
25
+ linkedList . prepend ( 2 ) ;
39
26
40
27
expect ( linkedList . toString ( ) ) . toBe ( '2,1' ) ;
41
28
} ) ;
42
29
43
30
it ( 'should delete node by value from linked list' , ( ) => {
44
31
const linkedList = new LinkedList ( ) ;
45
32
46
- expect ( linkedList . deleteByValue ( 5 ) ) . toBeNull ( ) ;
33
+ expect ( linkedList . delete ( 5 ) ) . toBeNull ( ) ;
47
34
48
- linkedList . append ( { value : 1 } ) ;
49
- linkedList . append ( { value : 2 } ) ;
50
- linkedList . append ( { value : 3 } ) ;
51
- linkedList . append ( { value : 3 } ) ;
52
- linkedList . append ( { value : 3 } ) ;
53
- linkedList . append ( { value : 4 } ) ;
54
- linkedList . append ( { value : 5 } ) ;
35
+ linkedList . append ( 1 ) ;
36
+ linkedList . append ( 2 ) ;
37
+ linkedList . append ( 3 ) ;
38
+ linkedList . append ( 3 ) ;
39
+ linkedList . append ( 3 ) ;
40
+ linkedList . append ( 4 ) ;
41
+ linkedList . append ( 5 ) ;
55
42
56
43
expect ( linkedList . head . toString ( ) ) . toBe ( '1' ) ;
57
44
expect ( linkedList . tail . toString ( ) ) . toBe ( '5' ) ;
58
45
59
- const deletedNode = linkedList . deleteByValue ( 3 ) ;
46
+ const deletedNode = linkedList . delete ( 3 ) ;
60
47
expect ( deletedNode . value ) . toBe ( 3 ) ;
61
48
expect ( linkedList . toString ( ) ) . toBe ( '1,2,4,5' ) ;
62
49
63
- linkedList . deleteByValue ( 3 ) ;
50
+ linkedList . delete ( 3 ) ;
64
51
expect ( linkedList . toString ( ) ) . toBe ( '1,2,4,5' ) ;
65
52
66
- linkedList . deleteByValue ( 1 ) ;
53
+ linkedList . delete ( 1 ) ;
67
54
expect ( linkedList . toString ( ) ) . toBe ( '2,4,5' ) ;
68
55
69
56
expect ( linkedList . head . toString ( ) ) . toBe ( '2' ) ;
70
57
expect ( linkedList . tail . toString ( ) ) . toBe ( '5' ) ;
71
58
72
- linkedList . deleteByValue ( 5 ) ;
59
+ linkedList . delete ( 5 ) ;
73
60
expect ( linkedList . toString ( ) ) . toBe ( '2,4' ) ;
74
61
75
62
expect ( linkedList . head . toString ( ) ) . toBe ( '2' ) ;
76
63
expect ( linkedList . tail . toString ( ) ) . toBe ( '4' ) ;
77
64
78
- linkedList . deleteByValue ( 4 ) ;
65
+ linkedList . delete ( 4 ) ;
79
66
expect ( linkedList . toString ( ) ) . toBe ( '2' ) ;
80
67
81
68
expect ( linkedList . head . toString ( ) ) . toBe ( '2' ) ;
82
69
expect ( linkedList . tail . toString ( ) ) . toBe ( '2' ) ;
83
70
} ) ;
84
71
85
- it ( 'should delete node by key from linked list' , ( ) => {
86
- const linkedList = new LinkedList ( ) ;
87
-
88
- expect ( linkedList . deleteByKey ( 'key' ) ) . toBeNull ( ) ;
89
-
90
- linkedList . append ( { value : 1 , key : 'test1' } ) ;
91
- linkedList . append ( { value : 2 , key : 'test2' } ) ;
92
- linkedList . append ( { value : 3 , key : 'test3' } ) ;
93
- linkedList . append ( { value : 4 , key : 'test4' } ) ;
94
-
95
- const deletedNode1 = linkedList . deleteByKey ( 'test2' ) ;
96
- expect ( deletedNode1 . key ) . toBe ( 'test2' ) ;
97
- expect ( linkedList . toString ( ) ) . toBe ( 'test1:1,test3:3,test4:4' ) ;
98
-
99
- const deletedNode2 = linkedList . deleteByKey ( 'test1' ) ;
100
- expect ( deletedNode2 . key ) . toBe ( 'test1' ) ;
101
- expect ( linkedList . toString ( ) ) . toBe ( 'test3:3,test4:4' ) ;
102
-
103
- const deletedNode3 = linkedList . deleteByKey ( 'test4' ) ;
104
- expect ( deletedNode3 . key ) . toBe ( 'test4' ) ;
105
- expect ( linkedList . toString ( ) ) . toBe ( 'test3:3' ) ;
106
- } ) ;
107
-
108
72
it ( 'should delete linked list tail' , ( ) => {
109
73
const linkedList = new LinkedList ( ) ;
110
74
111
- linkedList . append ( { value : 1 } ) ;
112
- linkedList . append ( { value : 2 } ) ;
113
- linkedList . append ( { value : 3 } ) ;
75
+ linkedList . append ( 1 ) ;
76
+ linkedList . append ( 2 ) ;
77
+ linkedList . append ( 3 ) ;
114
78
115
79
expect ( linkedList . head . toString ( ) ) . toBe ( '1' ) ;
116
80
expect ( linkedList . tail . toString ( ) ) . toBe ( '3' ) ;
@@ -142,8 +106,8 @@ describe('LinkedList', () => {
142
106
143
107
expect ( linkedList . deleteHead ( ) ) . toBeNull ( ) ;
144
108
145
- linkedList . append ( { value : 1 } ) ;
146
- linkedList . append ( { value : 2 } ) ;
109
+ linkedList . append ( 1 ) ;
110
+ linkedList . append ( 2 ) ;
147
111
148
112
expect ( linkedList . head . toString ( ) ) . toBe ( '1' ) ;
149
113
expect ( linkedList . tail . toString ( ) ) . toBe ( '2' ) ;
@@ -163,26 +127,52 @@ describe('LinkedList', () => {
163
127
expect ( linkedList . tail ) . toBeNull ( ) ;
164
128
} ) ;
165
129
166
- it ( 'should append unique nodes ' , ( ) => {
130
+ it ( 'should be possible to store objects in the list and to print them out ' , ( ) => {
167
131
const linkedList = new LinkedList ( ) ;
168
132
169
- linkedList . appendUnique ( { value : 1 , key : 'test1' } ) ;
170
- linkedList . appendUnique ( { value : 2 , key : 'test2' } ) ;
171
- linkedList . appendUnique ( { value : 3 , key : 'test2' } ) ;
172
- linkedList . appendUnique ( { value : 5 , key : 'test1' } ) ;
133
+ const nodeValue1 = { value : 1 , key : 'key1' } ;
134
+ const nodeValue2 = { value : 2 , key : 'key2' } ;
135
+
136
+ linkedList
137
+ . append ( nodeValue1 )
138
+ . prepend ( nodeValue2 ) ;
139
+
140
+ const nodeStringifier = value => `${ value . key } :${ value . value } ` ;
141
+
142
+ expect ( linkedList . toString ( nodeStringifier ) ) . toBe ( 'key2:2,key1:1' ) ;
143
+ } ) ;
144
+
145
+ it ( 'should find node by value' , ( ) => {
146
+ const linkedList = new LinkedList ( ) ;
147
+
148
+ expect ( linkedList . find ( { value : 5 } ) ) . toBeNull ( ) ;
149
+
150
+ linkedList . append ( 1 ) ;
151
+ expect ( linkedList . find ( { value : 1 } ) ) . toBeDefined ( ) ;
152
+
153
+ linkedList
154
+ . append ( 2 )
155
+ . append ( 3 ) ;
156
+
157
+ const node = linkedList . find ( { value : 2 } ) ;
173
158
174
- expect ( linkedList . toString ( ) ) . toBe ( 'test1:5,test2:3' ) ;
159
+ expect ( node . value ) . toBe ( 2 ) ;
160
+ expect ( linkedList . find ( { value : 5 } ) ) . toBeNull ( ) ;
175
161
} ) ;
176
162
177
- it ( 'should find node by its key ' , ( ) => {
163
+ it ( 'should find node by callback ' , ( ) => {
178
164
const linkedList = new LinkedList ( ) ;
179
165
180
- expect ( linkedList . findByKey ( 'test' ) ) . toBeNull ( ) ;
166
+ linkedList
167
+ . append ( { value : 1 , key : 'test1' } )
168
+ . append ( { value : 2 , key : 'test2' } )
169
+ . append ( { value : 3 , key : 'test3' } ) ;
181
170
182
- linkedList . appendUnique ( { value : 1 , key : 'test1' } ) ;
183
- linkedList . appendUnique ( { value : 2 , key : 'test2' } ) ;
184
- linkedList . appendUnique ( { value : 3 , key : 'test3' } ) ;
171
+ const node = linkedList . find ( { callback : value => value . key === 'test2' } ) ;
185
172
186
- expect ( linkedList . findByKey ( 'test3' ) . toString ( ) ) . toBe ( 'test3:3' ) ;
173
+ expect ( node ) . toBeDefined ( ) ;
174
+ expect ( node . value . value ) . toBe ( 2 ) ;
175
+ expect ( node . value . key ) . toBe ( 'test2' ) ;
176
+ expect ( linkedList . find ( { callback : value => value . key === 'test5' } ) ) . toBeNull ( ) ;
187
177
} ) ;
188
178
} ) ;
0 commit comments