Skip to content

Instantly share code, notes, and snippets.

@borgateo
Last active January 16, 2021 11:40
Show Gist options
  • Save borgateo/3a39b02839d84f249311 to your computer and use it in GitHub Desktop.
Save borgateo/3a39b02839d84f249311 to your computer and use it in GitHub Desktop.
reading time
/*
** readingTime
** ===========
** Given a string, it returns the time a human needs to read it
** @param {String} text - the string to analyze
** @param {Number} wpm - words per minute
*/
function readingTime( text, wpm ) {
if ( !text ) text = '';
var words = text.trim().split(/\s+/g).length;
var wps = ( wpm || 210 ) / 60;
var rts = words / wps;
var m = Math.floor( rts / 60 );
var s = Math.round( rts - m * 60 );
return( {
minutes: m,
seconds: s
} );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment