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: 0 additions & 2 deletions src/convertToCase/detectCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,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('_')) {
Expand All @@ -21,7 +20,6 @@ function detectCase(text) {
return 'KEBAB';
}
}
}

if (text[0].toUpperCase() === text[0]) {
return 'PASCAL';
Expand Down
61 changes: 58 additions & 3 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,58 @@
// 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 CASES = ['SNAKE', 'KEBAB', 'CAMEL', 'PASCAL', 'UPPER'];

function validateParams(text, toCase) {
const errors = [];

if (!text) {
errors.push({
message:
'Text to convert is required. Correct request is: "/<TEXT_TO_CONVERT>?toCase=<CASE_NAME>".',

Check failure on line 11 in src/createServer.js

View workflow job for this annotation

GitHub Actions / build (20.x)

This line has a length of 100. Maximum allowed is 80
});
}

if (!toCase) {
errors.push({
message:
'"toCase" query param is required. Correct request is: "/<TEXT_TO_CONVERT>?toCase=<CASE_NAME>".'

Check failure on line 18 in src/createServer.js

View workflow job for this annotation

GitHub Actions / build (20.x)

This line has a length of 105. Maximum allowed is 80
});
}

if (toCase && !CASES.includes(toCase.toUpperCase())) {
errors.push({
message:
'This case is not supported. Available cases: SNAKE, KEBAB, CAMEL, PASCAL, UPPER.'

Check failure on line 25 in src/createServer.js

View workflow job for this annotation

GitHub Actions / build (20.x)

This line has a length of 91. Maximum allowed is 80
});
}

return errors;
}

function createServer() {
return http.createServer(function (req, res) {
const [path, queryString] = req.url.split('?');

const params = new URLSearchParams(queryString);
const toCase = params.get('toCase');
const errors = validateParams(path.slice(1), toCase);

if (errors.length) {
res.setHeader('Content-Type', 'application/json');
res.statusCode = 400;
res.end(JSON.stringify({ errors }));

return;
}

const target = { originalText: path.slice(1), targetCase: toCase };
const result = convertToCase(path.slice(1), toCase);

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

res.end(JSON.stringify({ ...result, ...target }));
});
}

module.exports = { createServer };
Loading