Skip to content

Commit 81754b7

Browse files
author
Kevin K. Lee
authored
Add a custom stringifier to linked lists (#5)
* Update singly linked list exercise to accept custom stringifier * Update doubly linked list exercise to accept custom stringifier
1 parent d7b64a5 commit 81754b7

21 files changed

+160
-24
lines changed

Diff for: skeletons/data-structures/doubly-linked-list/DoublyLinkedList.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export default class DoublyLinkedList {
6161
* @param {function} [callback]
6262
* @return {string}
6363
*/
64-
toString() {
65-
return this.toArray().map(node => node.toString()).toString();
64+
toString(callback) {
65+
return this.toArray().map(node => node.toString(callback)).toString();
6666
}
6767
}

Diff for: skeletons/data-structures/doubly-linked-list/DoublyLinkedListNode.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export default class DoublyLinkedListNode {
33
this.value = value;
44
}
55

6-
toString() {
7-
return this.value.toString();
6+
toString(callback) {
7+
return callback ? callback(this.value) : `${this.value}`;
88
}
99
}

Diff for: skeletons/data-structures/doubly-linked-list/__test__/DoublyLinkedList.test.js

+21
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,25 @@ describe('DoublyLinkedList', () => {
199199
expect(linkedList.head).toBeNull();
200200
expect(linkedList.tail).toBeNull();
201201
});
202+
203+
it('should be possible to store objects in the list and to print them out', () => {
204+
const linkedList = new DoublyLinkedList();
205+
206+
const nodeValue1 = {
207+
value: 1,
208+
key: 'key1',
209+
};
210+
const nodeValue2 = {
211+
value: 2,
212+
key: 'key2',
213+
};
214+
215+
linkedList
216+
.append(nodeValue1)
217+
.prepend(nodeValue2);
218+
219+
const nodeStringifier = value => `${value.key}:${value.value}`;
220+
221+
expect(linkedList.toString(nodeStringifier)).toBe('key2:2,key1:1');
222+
});
202223
});

Diff for: skeletons/data-structures/doubly-linked-list/__test__/DoublyLinkedListNode.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,16 @@ describe('DoublyLinkedListNode', () => {
4747
node.value = 'string value';
4848
expect(node.toString()).toBe('string value');
4949
});
50+
51+
it('should convert node to string with custom stringifier', () => {
52+
const nodeValue = {
53+
value: 1,
54+
key: 'test'
55+
};
56+
57+
const node = new DoublyLinkedListNode(nodeValue);
58+
const toStringCallback = value => `value: ${value.value}, key: ${value.key}`;
59+
60+
expect(node.toString(toStringCallback)).toBe('value: 1, key: test');
61+
});
5062
});

Diff for: skeletons/data-structures/linked-list/LinkedList.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export default class LinkedList {
6666
* @param {function} [callback]
6767
* @return {string}
6868
*/
69-
toString() {
70-
return this.toArray().map(node => node.toString()).toString();
69+
toString(callback) {
70+
return this.toArray().map(node => node.toString(callback)).toString();
7171
}
7272
}

Diff for: skeletons/data-structures/linked-list/LinkedListNode.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export default class LinkedListNode {
33
this.value = value;
44
}
55

6-
toString() {
7-
return this.value.toString();
6+
toString(callback) {
7+
return callback ? callback(this.value) : `${this.value}`;
88
}
99
}

Diff for: skeletons/data-structures/linked-list/__test__/LinkedListNode.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,16 @@ describe('LinkedListNode', () => {
3838
node.value = 'string value';
3939
expect(node.toString()).toBe('string value');
4040
});
41+
42+
it('should convert node to string with custom stringifier', () => {
43+
const nodeValue = {
44+
value: 1,
45+
key: 'test'
46+
};
47+
48+
const node = new LinkedListNode(nodeValue);
49+
const toStringCallback = value => `value: ${value.value}, key: ${value.key}`;
50+
51+
expect(node.toString(toStringCallback)).toBe('value: 1, key: test');
52+
});
4153
});

Diff for: solutions/data-structures/doubly-linked-list/DoublyLinkedList.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export default class DoublyLinkedList {
161161
* @param {function} [callback]
162162
* @return {string}
163163
*/
164-
toString() {
165-
return this.toArray().map(node => node.toString()).toString();
164+
toString(callback) {
165+
return this.toArray().map(node => node.toString(callback)).toString();
166166
}
167167
}

Diff for: solutions/data-structures/doubly-linked-list/DoublyLinkedListNode.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default class DoublyLinkedListNode {
55
this.previous = previous;
66
}
77

8-
toString() {
9-
return this.value.toString();
8+
toString(callback) {
9+
return callback ? callback(this.value) : `${this.value}`;
1010
}
1111
}

Diff for: solutions/data-structures/doubly-linked-list/__test__/DoublyLinkedList.test.js

+21
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,25 @@ describe('DoublyLinkedList', () => {
199199
expect(linkedList.head).toBeNull();
200200
expect(linkedList.tail).toBeNull();
201201
});
202+
203+
it('should be possible to store objects in the list and to print them out', () => {
204+
const linkedList = new DoublyLinkedList();
205+
206+
const nodeValue1 = {
207+
value: 1,
208+
key: 'key1',
209+
};
210+
const nodeValue2 = {
211+
value: 2,
212+
key: 'key2',
213+
};
214+
215+
linkedList
216+
.append(nodeValue1)
217+
.prepend(nodeValue2);
218+
219+
const nodeStringifier = value => `${value.key}:${value.value}`;
220+
221+
expect(linkedList.toString(nodeStringifier)).toBe('key2:2,key1:1');
222+
});
202223
});

Diff for: solutions/data-structures/doubly-linked-list/__test__/DoublyLinkedListNode.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,16 @@ describe('DoublyLinkedListNode', () => {
4747
node.value = 'string value';
4848
expect(node.toString()).toBe('string value');
4949
});
50+
51+
it('should convert node to string with custom stringifier', () => {
52+
const nodeValue = {
53+
value: 1,
54+
key: 'test'
55+
};
56+
57+
const node = new DoublyLinkedListNode(nodeValue);
58+
const toStringCallback = value => `value: ${value.value}, key: ${value.key}`;
59+
60+
expect(node.toString(toStringCallback)).toBe('value: 1, key: test');
61+
});
5062
});

Diff for: solutions/data-structures/linked-list/LinkedList.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ export default class LinkedList {
198198
* @param {function} [callback]
199199
* @return {string}
200200
*/
201-
toString() {
202-
return this.toArray().map(node => node.toString()).toString();
201+
toString(callback) {
202+
return this.toArray().map(node => node.toString(callback)).toString();
203203
}
204204
}
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
export default class LinkedListNode {
22
constructor(value, next = null) {
33
this.value = value;
4+
this.next = next;
45
}
56

6-
toString() {
7-
return this.value.toString();
7+
toString(callback) {
8+
return callback ? callback(this.value) : `${this.value}`;
89
}
910
}

Diff for: solutions/data-structures/linked-list/__test__/LinkedListNode.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,16 @@ describe('LinkedListNode', () => {
3838
node.value = 'string value';
3939
expect(node.toString()).toBe('string value');
4040
});
41+
42+
it('should convert node to string with custom stringifier', () => {
43+
const nodeValue = {
44+
value: 1,
45+
key: 'test'
46+
};
47+
48+
const node = new LinkedListNode(nodeValue);
49+
const toStringCallback = value => `value: ${value.value}, key: ${value.key}`;
50+
51+
expect(node.toString(toStringCallback)).toBe('value: 1, key: test');
52+
});
4153
});

Diff for: src/data-structures/doubly-linked-list/DoublyLinkedList.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export default class DoublyLinkedList {
6161
* @param {function} [callback]
6262
* @return {string}
6363
*/
64-
toString() {
65-
return this.toArray().map(node => node.toString()).toString();
64+
toString(callback) {
65+
return this.toArray().map(node => node.toString(callback)).toString();
6666
}
6767
}

Diff for: src/data-structures/doubly-linked-list/DoublyLinkedListNode.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export default class DoublyLinkedListNode {
33
this.value = value;
44
}
55

6-
toString() {
7-
return this.value.toString();
6+
toString(callback) {
7+
return callback ? callback(this.value) : `${this.value}`;
88
}
99
}

Diff for: src/data-structures/doubly-linked-list/__test__/DoublyLinkedList.test.js

+21
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,25 @@ describe('DoublyLinkedList', () => {
199199
expect(linkedList.head).toBeNull();
200200
expect(linkedList.tail).toBeNull();
201201
});
202+
203+
it('should be possible to store objects in the list and to print them out', () => {
204+
const linkedList = new DoublyLinkedList();
205+
206+
const nodeValue1 = {
207+
value: 1,
208+
key: 'key1',
209+
};
210+
const nodeValue2 = {
211+
value: 2,
212+
key: 'key2',
213+
};
214+
215+
linkedList
216+
.append(nodeValue1)
217+
.prepend(nodeValue2);
218+
219+
const nodeStringifier = value => `${value.key}:${value.value}`;
220+
221+
expect(linkedList.toString(nodeStringifier)).toBe('key2:2,key1:1');
222+
});
202223
});

Diff for: src/data-structures/doubly-linked-list/__test__/DoublyLinkedListNode.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,16 @@ describe('DoublyLinkedListNode', () => {
4747
node.value = 'string value';
4848
expect(node.toString()).toBe('string value');
4949
});
50+
51+
it('should convert node to string with custom stringifier', () => {
52+
const nodeValue = {
53+
value: 1,
54+
key: 'test'
55+
};
56+
57+
const node = new DoublyLinkedListNode(nodeValue);
58+
const toStringCallback = value => `value: ${value.value}, key: ${value.key}`;
59+
60+
expect(node.toString(toStringCallback)).toBe('value: 1, key: test');
61+
});
5062
});

Diff for: src/data-structures/linked-list/LinkedList.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export default class LinkedList {
6666
* @param {function} [callback]
6767
* @return {string}
6868
*/
69-
toString() {
70-
return this.toArray().map(node => node.toString()).toString();
69+
toString(callback) {
70+
return this.toArray().map(node => node.toString(callback)).toString();
7171
}
7272
}

Diff for: src/data-structures/linked-list/LinkedListNode.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default class LinkedListNode {
44
this.next = next;
55
}
66

7-
toString() {
8-
return this.value.toString();
7+
toString(callback) {
8+
return callback ? callback(this.value) : `${this.value}`;
99
}
1010
}

Diff for: src/data-structures/linked-list/__test__/LinkedListNode.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,16 @@ describe('LinkedListNode', () => {
3838
node.value = 'string value';
3939
expect(node.toString()).toBe('string value');
4040
});
41+
42+
it('should convert node to string with custom stringifier', () => {
43+
const nodeValue = {
44+
value: 1,
45+
key: 'test'
46+
};
47+
48+
const node = new LinkedListNode(nodeValue);
49+
const toStringCallback = value => `value: ${value.value}, key: ${value.key}`;
50+
51+
expect(node.toString(toStringCallback)).toBe('value: 1, key: test');
52+
});
4153
});

0 commit comments

Comments
 (0)