-
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 1 commit
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) { | ||
|
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. Incorrect usage of URLSearchParams: |
||
| errors.push({ | ||
| message: | ||
| '"toCase" query param is required. Correct request is: "/<TEXT_TO_CONVERT>?toCase=<CASE_NAME>"', | ||
|
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 error message for a missing |
||
| }); | ||
| } else { | ||
| if (!availableCases.includes(caseName)) { | ||
| errors.push({ | ||
| message: | ||
| 'This case is not supported. Available cases: SNAKE, KEBAB, CAMEL, PASCAL, UPPER', | ||
|
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 unsupported-case error message must exactly match the required message, including the final period. Add a trailing |
||
| }); | ||
| } | ||
| } | ||
|
|
||
| return errors; | ||
| } | ||
|
|
||
| module.exports = { | ||
| checkErrors, | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,69 @@ | ||
| // 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 params = new URLSearchParams(normalizedUrl); | ||
|
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. You are creating URLSearchParams incorrectly. |
||
| const caseName = params.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.
The error message for missing text must match the requirement exactly (including the final period). According to the spec it should end with:
"...toCase=<CASE_NAME>".— add the trailing period inside the string so it matches exactly.