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
23 changes: 23 additions & 0 deletions .github/workflows/test.yml-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test

on:
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
35 changes: 19 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
"test:only": "mate-scripts test",
"update": "mate-scripts update",
"postinstall": "npm run update",
"test": "npm run lint && npm run test:only"
"test": "npm run lint && npm run test:only",
"test:watch": "jest --watch"
},
"author": "Mate academy",
"license": "GPL-3.0",
"devDependencies": {
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.8.6",
"@mate-academy/scripts": "^2.1.3",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-node": "^11.1.0",
Expand Down
33 changes: 33 additions & 0 deletions src/checkErrors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* eslint-disable max-len */

function checkErrors(textToConvert, caseName) {
const errors = [];
const availableCases = ['SNAKE', 'KEBAB', 'CAMEL', 'PASCAL', 'UPPER'];

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

if (!caseName) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Incorrect usage of URLSearchParams: new URLSearchParams(normalizedUrl) will not extract the query parameters and params.get('toCase') will be null. Use the URL object's searchParams property (normalizedUrl.searchParams.get('toCase')) or pass normalizedUrl.search to new URLSearchParams(...).

errors.push({
message:
'"toCase" query param is required. Correct request is: "/<TEXT_TO_CONVERT>?toCase=<CASE_NAME>".',
});
} else {
if (!availableCases.includes(caseName)) {
errors.push({
message:
'This case is not supported. Available cases: SNAKE, KEBAB, CAMEL, PASCAL, UPPER.',
});
}
}

return errors;
}

module.exports = {
checkErrors,
};
71 changes: 68 additions & 3 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,68 @@
// Write code here
// Also, you can create additional files in the src folder
// and import (require) them here
/* eslint-disable max-len */

const http = require('http');

const { convertToCase } = require('./convertToCase/convertToCase.js');
const { checkErrors } = require('./checkErrors.js');

function createServer() {
const server = http.createServer((req, res) => {
res.setHeader('Content-Type', 'application/json');
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 string is missing the final period required by the spec. Error messages must match exactly. Update this message to end with a dot: ...toCase=<CASE_NAME>. (note the trailing period after the closing quote).


const normalizedUrl = new URL(req.url, `http://${req.headers.host}`);
const textToConvert = normalizedUrl.pathname.slice(1);
const caseName = normalizedUrl.searchParams.get('toCase');

const isErrors = checkErrors(textToConvert, caseName);
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 string is missing the final period required by the spec. Update this message to exactly: "toCase" query param is required. Correct request is: "/<TEXT_TO_CONVERT>?toCase=<CASE_NAME>". (ensure the trailing period).


if (isErrors.length > 0) {
res.statusCode(400);
res.statusMessage = 'Bad request';

res.end(
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This message is missing the final period. Per requirements it should end with a dot: This case is not supported. Available cases: SNAKE, KEBAB, CAMEL, PASCAL, UPPER.

JSON.stringify({
errors: isErrors,
}),
);

return;
}

const convertedObj = convertToCase(textToConvert, caseName);

/*
{
originalCase: 'CASE_NAME',
convertedText: 'CONVERTED_TEXT',
}
*/

const { originalCase, convertedText } = convertedObj;

const result = {
originalCase,
targetCase: caseName,
originalText: textToConvert,
convertedText,
};

/*
{
"originalCase": "KEBAB",
"targetCase": "PASCAL",
"originalText": "hello-world",
"convertedText": "HelloWorld"
}
*/

res.statusCode(200);
res.statusMessage = 'OK';
res.end(JSON.stringify(result));
});

return server;
}

module.exports = {
createServer,
};
Loading