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

feat: count the number of words in a user's comment #4

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
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,14 @@ Request body:
{ "text": "I felt happy because I saw the others were happy and because I knew I should feel happy"}
Response body:
[{"word": "happy", "count": 3}]
```
```

#### /comment-words
* `POST` : Returns the word count in the submitted comment

```
Request body:
{ "comment": "I find it ridiculous to limit me!"}
Response body:
{"count": 7}
```
13 changes: 13 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const server = http.createServer((req, res) => {
if (req.url === "/user-agent") return respondU(req, res);
if (req.url.match(/^\/b64\//)) return respondB(req, res);
if (req.url === '/repetitive-word') return respondRepetitiveWord(req, res)
if (req.url === '/comment-words') return respondCommentWord(req, res)

res.end();
});
Expand Down Expand Up @@ -37,6 +38,18 @@ function respondRepetitiveWord (req, res) {
})
}

function respondCommentWord (req, res) {
let body = ''
req.on('data', chunk => {
body += chunk.toString()
})
req.on('end', () => {
const comment = JSON.parse(body).comment
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify({ count: comment.split(' ').length }))
})
}

server.listen(PORT);
console.log(`Server listening on port ${PORT}`);

Expand Down
12 changes: 11 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,23 @@ tape('should respond repetitive-word', (t) => {
}
jsonist.post(`${urlBase}/repetitive-word`, data, (err, body) => {
if (err) t.error(err)

Copy link
Contributor

Choose a reason for hiding this comment

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

This change doesn’t relate to this PR.

t.equal(body[0].count, 4)
t.equal(body[0].word, 'happy')
t.end()
})
})

tape('should respond comment-words', (t) => {
const data = {
comment: 'If <branch> is specified, git rebase will perform an automatic git switch <branch> before doing anything else.'
}
jsonist.post(`${urlBase}/comment-words`, data, (err, body) => {
if (err) t.error(err)
t.equal(body.count, 17)
t.end()
})
})

tape("cleanup", function(t) {
server.close();
t.end();
Expand Down