Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution #806

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
39 changes: 39 additions & 0 deletions src/Error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class ErrorCase {
caseTypes = ['SNAKE', 'KEBAB', 'CAMEL', 'PASCAL', 'UPPER'];

convertErrorMessage = '/<TEXT_TO_CONVERT>?toCase=<CASE_NAME>';

Choose a reason for hiding this comment

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

The convertErrorMessage string uses a placeholder format that might not be correctly interpreted if used directly in a URL. Consider using a more explicit format or providing an example of how the URL should be structured.


originalTextError = `Text to convert is required. Correct request is: "${this.convertErrorMessage}".`;

originalCaseError = `"toCase" query param is required. Correct request is: "${this.convertErrorMessage}".`;

caseTypesError = `This case is not supported. Available cases: ${this.caseTypes.join(', ')}.`;

hasError(originalText, originalCase) {
return (
!originalText || !originalCase || !this.caseTypes.includes(originalCase)
);
}

getError(originalText, originalCase) {
const errors = [];

if (!originalText) {
errors.push(this.originalTextError);
}

if (!originalCase) {
errors.push(this.originalCaseError);
} else if (!this.caseTypes.includes(originalCase)) {
errors.push(this.caseTypesError);
}

return errors.map((error) => ({ message: error }));
}
}

const errorCase = new ErrorCase();

module.exports = {
errorCase,
};
1 change: 1 addition & 0 deletions src/convertToCase/convertToCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const { wordsToCase } = require('./wordsToCase');
*
* @returns {Result}
*/

function convertToCase(text, caseName) {
const originalCase = detectCase(text);
const words = toWords(text, originalCase);
Expand Down
2 changes: 0 additions & 2 deletions src/convertToCase/detectCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ function detectCase(text) {

if (text.toLowerCase() === text) {
if (text.includes('_') || text.includes('-')) {
// There are no uppercase in the text, so it's one of the lower cases
// See if they're snake or kebab
if (text.includes('_')) {
return 'SNAKE';
}
Expand Down
50 changes: 47 additions & 3 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
// Write code here
// Also, you can create additional files in the src folder
// and import (require) them here
const http = require('http');

const { convertToCase } = require('./convertToCase/convertToCase');
const { errorCase } = require('./Error');
const { sendJSONResponse } = require('./senResponse');

Choose a reason for hiding this comment

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

There is a typo in the import path for sendJSONResponse. It should be ./sendResponse instead of ./senResponse to match the file name.

const createServer = () => {
const server = http.createServer((req, res) => {
const url = new URL(req.url || '', `http://${req.headers.host}`);

const originalText = url.pathname.slice(1);
const targetCase = url.searchParams.get('toCase');

const hasError = errorCase.hasError(originalText, targetCase);

if (hasError) {
const errors = errorCase.getError(originalText, targetCase);

return sendJSONResponse(res, 400, { errors }, 'Bad request');
}

try {
const { originalCase, convertedText } = convertToCase(
originalText,
targetCase,
);

return sendJSONResponse(res, 200, {
originalCase,
targetCase,
originalText,
convertedText,
});
} catch (error) {
return sendJSONResponse(
res,
500,
{ errors: { message: error.message } },
'Internal Server Error',
);
}
});

return server;
};

module.exports = { createServer };
4 changes: 3 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { createServer } = require('./createServer');

createServer().listen(5700, () => {
const PORT = process.env.PORT || 5701;

createServer().listen(PORT, () => {
// eslint-disable-next-line no-console
console.log('Server started! 🚀');
});
13 changes: 13 additions & 0 deletions src/senResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const sendJSONResponse = (res, code, content, message) => {
res.statusCode = code;

if (message) {
res.statusMessage = message;
}

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

res.end(JSON.stringify(content));
};

module.exports = { sendJSONResponse };
Loading