-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(article): implement article read time
Extended the article model to include timeToRead column. Calculate time to read article then save article. - Add calculateTimeToRead helper method - Add pre save hook - include relevant tests
- Loading branch information
Showing
5 changed files
with
60 additions
and
2 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
server/migrations/20180911074956-article-add-time-to-read-column.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.addColumn( | ||
'Articles', | ||
'timeToRead', | ||
{ | ||
type: Sequelize.INTEGER, | ||
}, | ||
); | ||
}, | ||
|
||
down: (queryInterface) => { | ||
return queryInterface.removeColumn( | ||
'Articles', | ||
'timeToRead', | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { expect } from 'chai'; | ||
import calculateTimeToRead from '../../utils/calculateTimeToRead'; | ||
|
||
describe('calculateTimeToRead', () => { | ||
it('should calculate time to read an article', () => { | ||
const article = { | ||
body: 'Some short article', | ||
}; | ||
|
||
const timeToRead = calculateTimeToRead(article); | ||
expect(timeToRead).to.be.a('number'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* calculates time to read an article | ||
* | ||
* @param {string} article | ||
* @returns {number} timeToRead - time it takes to read an article | ||
*/ | ||
const calculateTimeToRead = (article) => { | ||
const averageReadSpeed = 265; // words per minute | ||
|
||
// get article word count | ||
const articleWordCount = article.body.split(' ').length; | ||
const timeToRead = articleWordCount / averageReadSpeed; | ||
|
||
return Math.ceil(timeToRead); | ||
}; | ||
|
||
export default calculateTimeToRead; |