Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/convertToCase/convertToCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function convertToCase(text, caseName) {
const words = toWords(text, originalCase);
const convertedText = wordsToCase(words, caseName);

return { originalCase, convertedText };
return { convertedText, originalCase };
}

module.exports = {
Expand Down
75 changes: 75 additions & 0 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,78 @@
/* eslint-disable no-console */
// Write code here
// Also, you can create additional files in the src folder
// and import (require) them here
function createServer() {
const http = require('http');
const { convertToCase } = require('./convertToCase/convertToCase');

// const PORT = process.env.PORT || 3000;

const server = http.createServer((req, res) => {
const [textToConvert, query = ''] = req.url.slice(1).split('?');
const toCase = new URLSearchParams(query).get('toCase');

res.setHeader('Content-Type', 'application/json');

const errors = [];

if (textToConvert.length === 0) {
errors.push({
message:
// eslint-disable-next-line max-len
'Text to convert is required. Correct request is: "/<TEXT_TO_CONVERT>?toCase=<CASE_NAME>".',
});
}

if (toCase === null) {
errors.push({
message:
// eslint-disable-next-line max-len
'"toCase" query param is required. Correct request is: "/<TEXT_TO_CONVERT>?toCase=<CASE_NAME>".',
});
}

if (
toCase !== null &&
toCase !== 'SNAKE' &&
toCase !== 'KEBAB' &&
toCase !== 'CAMEL' &&
toCase !== 'PASCAL' &&
toCase !== 'UPPER'
) {
errors.push({
message:
// eslint-disable-next-line max-len
'This case is not supported. Available cases: SNAKE, KEBAB, CAMEL, PASCAL, UPPER.',
});
}

if (errors.length > 0) {
res.statusCode = 400;

res.end(
JSON.stringify({
errors: [...errors],
}),
);

return;
}

res.statusCode = 200;

res.end(
JSON.stringify({
...convertToCase(textToConvert, toCase),
originalText: textToConvert,
targetCase: toCase,
}),
Comment on lines +64 to +69
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The response object structure produces fields in order: originalCase, convertedText, originalText, targetCase. However, based on the requirements example, the expected order is originalCase, targetCase, originalText, convertedText. Consider restructuring the response to explicitly include fields in the specified order rather than relying on spread operator.

);
});

return server;
}

module.exports = {
createServer,
};
Loading