Skip to content

Commit 372db49

Browse files
SiusarnaAMashoshyna
authored andcommitted
Data structures (#18)
* data-structures complete * review v2.0 * review v3.0 * Update README.md (#25) * restructured instructions so that student makes any file operations when in a feature branch context to avoid commits to `master` before student reads instructions on pull request initialization * added instructions on PR updates * Port sniffer (#11) * Add port-sniffer homework * Add code after prettier and eslint check * Fix eslint errors * Add dependencies: commander, async * Fixes respectively to PR 'Port sniffer' #11, #11 * Delete dependencies from package.json * Add all package-lock.jsons into .gitignore * Add .gitignore in port-sniffer subdirectory * Remove global package-lock.json * Resolve integration with CI manager * kissik/port-sniffer: integrate project into root package * chores: make eslint ignoring nested node_modules * change eslint+airbnb to semistandard (#24) * change eslint+airbnb to semistandard * fix linting errors * submissions/kissik/port-sniffer: Fix linting errors * data-structures complete * review v2.0 * review v3.0 * Change eslint
1 parent b478eef commit 372db49

7 files changed

Lines changed: 334 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* eslint-disable no-underscore-dangle */
2+
3+
module.exports = class Node {
4+
constructor (value, next = null) {
5+
this._value = value;
6+
this._next = next;
7+
}
8+
9+
getValue () {
10+
return this._value;
11+
}
12+
13+
setValue (newValue) {
14+
this._value = newValue;
15+
}
16+
17+
getNext () {
18+
return this._next;
19+
}
20+
21+
setNext (newNext) {
22+
this._next = newNext;
23+
}
24+
};
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/* eslint-disable no-underscore-dangle */
2+
3+
const Node = require('./Element.js');
4+
5+
function getSuccessorNode (prevNode, successor) {
6+
let successorNode = prevNode;
7+
if (!successor) {
8+
return successorNode;
9+
}
10+
// successor value saved in prevNode or prevNode._next() so we check these node
11+
if (prevNode.getValue() === successor) {
12+
successorNode = prevNode;
13+
} else if (prevNode.getNext()) {
14+
successorNode = prevNode.getNext();
15+
} else {
16+
throw new Error('not found this successor');
17+
}
18+
return successorNode;
19+
}
20+
21+
module.exports = class LinkedList {
22+
constructor () {
23+
this._head = null;
24+
this._size = 0;
25+
}
26+
27+
searchForPrevNode (inputValue) {
28+
let iteratorNode = this._head;
29+
while (
30+
iteratorNode.getNext() &&
31+
iteratorNode.getNext().getValue() !== inputValue
32+
) {
33+
iteratorNode = iteratorNode.getNext();
34+
}
35+
return iteratorNode;
36+
}
37+
38+
insert (inputValue, successor = null) {
39+
if (this._head === null) {
40+
this._head = new Node(inputValue);
41+
} else {
42+
const prevNode = this.searchForPrevNode(successor);
43+
const successorNode = getSuccessorNode(prevNode, successor);
44+
const currentNode = new Node(inputValue, successorNode.getNext());
45+
successorNode.setNext(currentNode);
46+
}
47+
this._size += 1;
48+
}
49+
50+
remove (inputValue) {
51+
if (this._head.getValue() === inputValue) {
52+
this._head = this._head.getNext();
53+
} else {
54+
const prevNodeForInputValue = this.searchForPrevNode(inputValue);
55+
if (!prevNodeForInputValue.getNext()) {
56+
throw new Error('not found this value');
57+
}
58+
const nextForRemovedNode = prevNodeForInputValue.getNext().getNext();
59+
prevNodeForInputValue.setNext(nextForRemovedNode);
60+
}
61+
this._size -= 1;
62+
}
63+
64+
show () {
65+
let iteratorNode = this._head;
66+
const allValueInList = [];
67+
if (this._head) {
68+
while (iteratorNode) {
69+
allValueInList.push(iteratorNode.getValue());
70+
iteratorNode = iteratorNode.getNext();
71+
}
72+
}
73+
return allValueInList.reverse();
74+
}
75+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* eslint-disable no-underscore-dangle */
2+
3+
const Node = require('./Element.js');
4+
5+
module.exports = class Stack {
6+
constructor () {
7+
this._head = null;
8+
this._size = 0;
9+
}
10+
11+
push (inputValue) {
12+
this._head = new Node(inputValue, this._head);
13+
this._size += 1;
14+
}
15+
16+
pop () {
17+
let removedValue;
18+
if (this._size) {
19+
removedValue = this._head.getValue();
20+
this._head = this._head.getNext() || null;
21+
this._size -= 1;
22+
}
23+
return removedValue;
24+
}
25+
26+
getSize () {
27+
return this._size;
28+
}
29+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const server = require('./server/server.js');
2+
const LinkedList = require('./data-structures/linkedList.js');
3+
const Stack = require('./data-structures/stack.js');
4+
5+
server(new LinkedList(), new Stack());
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
function isJSON (req) {
2+
return req.headers['content-type'] === 'application/json';
3+
}
4+
5+
function showList (res, list) {
6+
const allNodeFromList = list.show();
7+
const jsonObjWithAllNode = JSON.stringify({ data: allNodeFromList });
8+
const result = {
9+
status: 'success',
10+
data: jsonObjWithAllNode
11+
};
12+
return result;
13+
}
14+
15+
function deleteElementsFromStack (res, stack) {
16+
const removedValue = stack.pop();
17+
const jsonObjWithRemovedValue = JSON.stringify({ data: removedValue });
18+
const result = {
19+
status: 'success',
20+
data: jsonObjWithRemovedValue
21+
};
22+
return result;
23+
}
24+
25+
function isValidData (inputData) {
26+
return typeof inputData === 'number' || typeof inputData === 'string';
27+
}
28+
29+
function sendWrongTypeOfContentResponse () {
30+
const result = {
31+
status: 'error',
32+
data: 'wrong type of content'
33+
};
34+
return result;
35+
}
36+
37+
function insertInList (res, list, inputData) {
38+
const { data, successor } = inputData;
39+
if (!isValidData(data)) {
40+
throw new Error('wrong type of input value');
41+
}
42+
if (successor && isValidData(successor)) {
43+
try {
44+
list.insert(data, successor);
45+
} catch (err) {
46+
throw new Error(err.message);
47+
}
48+
} else if (!successor) {
49+
list.insert(data);
50+
} else {
51+
throw new Error('wrong type of input value');
52+
}
53+
}
54+
55+
function processOfInsertInList (res, list, inputData) {
56+
const result = {};
57+
try {
58+
insertInList(res, list, inputData);
59+
result.status = 'success';
60+
} catch (err) {
61+
result.status = 'error';
62+
result.data = err.message;
63+
}
64+
return result;
65+
}
66+
67+
function removeFromList (res, list, inputData) {
68+
const result = {};
69+
try {
70+
list.remove(inputData.data);
71+
result.status = 'success';
72+
} catch (err) {
73+
result.status = 'error';
74+
result.data = err.message;
75+
}
76+
return result;
77+
}
78+
79+
function pushOnStack (res, stack, inputData) {
80+
stack.push(inputData.data);
81+
const result = {
82+
status: 'success'
83+
};
84+
return result;
85+
}
86+
87+
function notFound () {
88+
const result = {
89+
status: 'error',
90+
data: 'Url not found'
91+
};
92+
return result;
93+
}
94+
95+
function makeStackResponse (method, stack, inputData, res) {
96+
let result;
97+
if (method === 'DELETE') {
98+
result = deleteElementsFromStack(res, stack);
99+
} else if (method === 'POST') {
100+
result = pushOnStack(res, stack, inputData);
101+
}
102+
return result;
103+
}
104+
105+
function makeListResponse (method, list, inputData, res) {
106+
let result;
107+
if (method === 'GET') {
108+
result = showList(res, list);
109+
} else if (method === 'POST') {
110+
result = processOfInsertInList(res, list, inputData);
111+
} else if (method === 'DELETE') {
112+
result = removeFromList(res, list, inputData);
113+
}
114+
return result;
115+
}
116+
117+
function responseMakers (typeOfStructures, method, inputData, res, list, stack) {
118+
let result;
119+
if (typeOfStructures === 'list') {
120+
result = makeListResponse(method, list, inputData, res);
121+
} else if (typeOfStructures === 'stack') {
122+
result = makeStackResponse(method, stack, inputData, res);
123+
} else {
124+
result = notFound();
125+
}
126+
return result;
127+
}
128+
129+
async function readBody (req) {
130+
return new Promise(resolve => {
131+
let body = '';
132+
req.on('data', chunk => {
133+
body += chunk.toString();
134+
});
135+
req.on('end', () => {
136+
resolve(body);
137+
});
138+
});
139+
}
140+
141+
module.exports = (list, stack) => {
142+
return function controller (req, res) {
143+
const typeOfStructeres = req.url.substr(1);
144+
return new Promise(resolve => {
145+
if (!isJSON(req)) {
146+
resolve(sendWrongTypeOfContentResponse(res));
147+
} else {
148+
readBody(req).then(completeBody => {
149+
const inputData = JSON.parse(completeBody);
150+
const result = responseMakers(
151+
typeOfStructeres,
152+
req.method,
153+
inputData,
154+
res,
155+
list,
156+
stack
157+
);
158+
resolve(result);
159+
});
160+
}
161+
});
162+
};
163+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function success (res, data = null) {
2+
if (data) {
3+
res.writeHead(200, { 'Content-Type': 'application/json' });
4+
} else {
5+
res.writeHead(200);
6+
}
7+
res.end(data);
8+
}
9+
10+
function error (res, message) {
11+
res.writeHead(400, message);
12+
res.end();
13+
}
14+
15+
const responseSender = {
16+
success,
17+
error
18+
};
19+
20+
module.exports = responseSender;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const http = require('http');
2+
const controllers = require('./controllers.js');
3+
const responseSender = require('./httpResponce.js');
4+
5+
module.exports = (list, stack) => {
6+
http
7+
.createServer((req, res) => {
8+
const promiseWithResult = controllers(list, stack)(req, res);
9+
promiseWithResult.then(objWithStatusAndData => {
10+
if (objWithStatusAndData.status === 'error') {
11+
responseSender.error(res, objWithStatusAndData.data);
12+
} else {
13+
responseSender.success(res, objWithStatusAndData.data);
14+
}
15+
});
16+
})
17+
.listen(3000);
18+
};

0 commit comments

Comments
 (0)