Skip to content

Commit

Permalink
docs: add README
Browse files Browse the repository at this point in the history
  • Loading branch information
patchamama committed Sep 9, 2023
1 parent ea1d773 commit e402cbc
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 8 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ Answers to [openfullstack.com](https://fullstackopen.com) course exercises from
- [Exercises 4.3.-4.7.](https://fullstackopen.com/en/part4/structure_of_backend_application_introduction_to_testing#exercises-4-3-4-7)
_Details solutions: [4.3](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/0f1a759e78310de711531ed481917496e2f50ec8) | [4.4](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/2585dc72841e8382123fa481aa149d19dd611482) | [4.5](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/4fd94b8779e33ea32b3703a6e6a823291d247e0c) | [4.6](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/c9d554cf41d5cbe5992fd2e2005564fab816d8ad) | [4.7](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/27b41a689bf77cb20a9853fb4f7eaaa998262022)_

### Test can be execute:

```
npm test
```

### b. Testing the backend

### c. User administration
Expand Down
79 changes: 71 additions & 8 deletions tests/blog.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('favorites', () => {
expect(result).toEqual({})
})

test('Most favorite with one value', () => {
test('--- with one value', () => {
const blogs = [
{
_id: '23234323242dssfsf',
Expand All @@ -107,21 +107,22 @@ describe('favorites', () => {
expect(result).toEqual(blogs[0])
})

test('Most favorite with a big list', () => {
test('--- with a big list', () => {
const result = listHelper.favoriteBlog(bigbloglist)
expect(result).toEqual(bigbloglist[0])
expect(result.likes).toEqual(12)
})
})

describe('Authors with more blogs', () => {
test('More blogs with empty list', () => {
test('lodash - with empty list', () => {
const blogs = [{}]
const result = listHelper.mostBlogs(blogs)
console.log(result)
expect(result).toEqual({ author: undefined, entries: 1 })
})
test('More blogs with one element in the list', () => {

test('lodash - with one element in the list', () => {
const blogs = [
{
_id: '5a422bc61b54a676234d17fc',
Expand All @@ -136,22 +137,53 @@ describe('Authors with more blogs', () => {
console.log(result)
expect(result).toEqual({ author: 'Robert C. Martin', entries: 1 })
})
test('More blogs with big list', () => {

test('lodash - with big list', () => {
const result = listHelper.mostBlogs(bigbloglist)
console.log(result)
expect(result).toEqual({ author: 'Robert C. Martin', entries: 3 })
})

// Vanilla JS
test('Vanilla JS - with empty list', () => {
const blogs = [{}]
const result = listHelper.mostBlogsVanilla(blogs)
console.log(result)
expect(result).toEqual({ author: undefined, entries: 1 })
})

test('Vanilla JS - with one element in the list', () => {
const blogs = [
{
_id: '5a422bc61b54a676234d17fc',
title: 'Type wars',
author: 'Robert C. Martin',
url: 'http://blog.cleancoder.com/uncle-bob/2016/05/01/TypeWars.html',
likes: 2,
__v: 0,
},
]
const result = listHelper.mostBlogsVanilla(blogs)
console.log(result)
expect(result).toEqual({ author: 'Robert C. Martin', entries: 1 })
})

test('Vanilla JS - with big list', () => {
const result = listHelper.mostBlogsVanilla(bigbloglist)
console.log(result)
expect(result).toEqual({ author: 'Robert C. Martin', entries: 3 })
})
})

describe('Authors with more likes', () => {
test('--- with empty list', () => {
test('lodash - with empty list', () => {
const blogs = [{}]
const result = listHelper.mostLikes(blogs)
console.log(result)
expect(result).toEqual({ author: undefined, likes: undefined })
})

test('--- with one blog list', () => {
test('lodash - with one blog list', () => {
const blogs = [
{
_id: '5a422bc61b54a676234d17fc',
Expand All @@ -166,9 +198,40 @@ describe('Authors with more likes', () => {
console.log(result)
expect(result).toEqual({ author: 'Robert C. Martin', likes: 2 })
})
test('--- with big blog list', () => {

test('lodash - with big blog list', () => {
const result = listHelper.mostLikes(bigbloglist)
console.log(result)
expect(result).toEqual({ author: 'Edsger W. Dijkstra', likes: 17 })
})

// VanillaJS
test('VanillaJS - with empty list', () => {
const blogs = [{}]
const result = listHelper.mostLikesVanilla(blogs)
console.log(result)
expect(result).toEqual({ author: undefined, likes: undefined })
})

test('VanillaJS - with one blog list', () => {
const blogs = [
{
_id: '5a422bc61b54a676234d17fc',
title: 'Type wars',
author: 'Robert C. Martin',
url: 'http://blog.cleancoder.com/uncle-bob/2016/05/01/TypeWars.html',
likes: 2,
__v: 0,
},
]
const result = listHelper.mostLikesVanilla(blogs)
console.log(result)
expect(result).toEqual({ author: 'Robert C. Martin', likes: 2 })
})

test('VanillaJS -with big blog list', () => {
const result = listHelper.mostLikesVanilla(bigbloglist)
console.log(result)
expect(result).toEqual({ author: 'Edsger W. Dijkstra', likes: 17 })
})
})
66 changes: 66 additions & 0 deletions utils/list_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,38 @@ const mostBlogs = (blogs) => {
}
}

const mostBlogsVanilla = (blogs) => {
if (blogs.length === 0) return { author: undefined, entries: 0 }
else if (blogs.length === 1) return { author: blogs[0].author, entries: 1 }
else {
const groupByAuthor = blogs.reduce((result, item) => {
if (result[item.author]) {
result[item.author] += 1
} else {
result[item.author] = 1
}
return result
}, {})

console.log(groupByAuthor)

let authorMoreBlogs = ''

for (const author in groupByAuthor) {
authorMoreBlogs = authorMoreBlogs === '' ? author : authorMoreBlogs
if (groupByAuthor[author] > groupByAuthor[authorMoreBlogs]) {
authorMoreBlogs = author
}
}

console.log(authorMoreBlogs, '-', groupByAuthor[authorMoreBlogs])
return {
author: authorMoreBlogs,
entries: groupByAuthor[authorMoreBlogs],
}
}
}

const mostLikes = (blogs) => {
if (blogs.length === 0) return { author: undefined, likes: undefined }
else if (blogs.length === 1)
Expand All @@ -56,10 +88,44 @@ const mostLikes = (blogs) => {
}
}

const mostLikesVanilla = (blogs) => {
if (blogs.length === 0) return { author: undefined, likes: undefined }
else if (blogs.length === 1)
return { author: blogs[0].author, likes: blogs[0].likes }
else {
const groupByAuthor = blogs.reduce((result, item) => {
if (result[item.author]) {
result[item.author] += item.likes
} else {
result[item.author] = item.likes
}
return result
}, {})

console.log(groupByAuthor)

let authorMoreLikes = ''

for (const author in groupByAuthor) {
authorMoreLikes = authorMoreLikes === '' ? author : authorMoreLikes
if (groupByAuthor[author] > groupByAuthor[authorMoreLikes]) {
authorMoreLikes = author
}
}

return {
author: authorMoreLikes,
likes: groupByAuthor[authorMoreLikes],
}
}
}

module.exports = {
dummy,
totalLikes,
favoriteBlog,
mostBlogs,
mostLikes,
mostBlogsVanilla,
mostLikesVanilla,
}

0 comments on commit e402cbc

Please sign in to comment.