-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Solution #1107
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
base: master
Are you sure you want to change the base?
Solution #1107
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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) { | ||
| 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, | ||
| }; | ||
| 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'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: |
||
|
|
||
| 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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: |
||
|
|
||
| if (isErrors.length > 0) { | ||
| res.statusCode(400); | ||
| res.statusMessage = 'Bad request'; | ||
|
|
||
| res.end( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: |
||
| 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, | ||
| }; | ||
There was a problem hiding this comment.
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 andparams.get('toCase')will be null. Use the URL object'ssearchParamsproperty (normalizedUrl.searchParams.get('toCase')) or passnormalizedUrl.searchtonew URLSearchParams(...).