Gruntified and added more options

Ability to configure if you compensate for images as well as being able to
adjust the number of words per minute being used. Closes #1
This commit is contained in:
Josh Sherman 2014-09-03 22:04:59 -04:00
parent 8c99148381
commit ca545cfc33
7 changed files with 55 additions and 9 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
node_modules/*

30
Gruntfile.js Normal file
View file

@ -0,0 +1,30 @@
module.exports = function(grunt)
{
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
jshint: {
foo: {
src: ['src/<%= pkg.name %>.js']
}
},
uglify: {
options: {
banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
},
build: {
src: 'src/<%= pkg.name %>.js',
dest: 'build/<%= pkg.name %>.min.js'
}
},
watch: {
files: ['src/*.js'],
tasks: ['jshint', 'uglify']
}
});
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerTask('default', ['jshint', 'uglify']);
};

2
build/jquery.readtime.min.js vendored Normal file
View file

@ -0,0 +1,2 @@
/*! jquery.readtime 2014-09-03 */
!function(a){a.fn.readtime=function(b){var c={"class":"readtime",format:"# min read",images:12,wpm:275,wrapper:"time"};return b=a.extend(c,b),this.each(function(){var c=this.textContent||this.innerText||"",d=c.replace(/(^\s*)|(\s*$)/gi,"").replace(/[ ]{2,}/gi," ").replace(/\n /,"\n").split(" ").length,e=d/b.wpm*60;b.images&&(e+=a(this).find("img").length*b.images),e=Math.round(e/60),e=b.format.replace(/#/,e);var f=document.createElement(b.wrapper);a(f).addClass(b.class).html(e).prependTo(a(this))})}}(jQuery);

View file

@ -17,7 +17,7 @@
<p>Sed sodales arcu metus, sed convallis dolor semper sed. Curabitur consectetur aliquet dignissim. Phasellus eu magna enim. Aliquam varius posuere nibh quis ornare. Sed semper vestibulum nibh et tincidunt. Quisque aliquet lorem eu sapien vulputate, ac ultricies tellus elementum. Phasellus velit velit, mollis sit amet sagittis eu, iaculis a sapien. Praesent sed justo in erat viverra faucibus. Integer in porttitor dui, vel consectetur lorem. Donec dapibus sed sem non feugiat. Curabitur egestas augue vel volutpat rutrum. Quisque ut nisl ut lorem fermentum finibus. Donec ut augue mauris.</p> <p>Sed sodales arcu metus, sed convallis dolor semper sed. Curabitur consectetur aliquet dignissim. Phasellus eu magna enim. Aliquam varius posuere nibh quis ornare. Sed semper vestibulum nibh et tincidunt. Quisque aliquet lorem eu sapien vulputate, ac ultricies tellus elementum. Phasellus velit velit, mollis sit amet sagittis eu, iaculis a sapien. Praesent sed justo in erat viverra faucibus. Integer in porttitor dui, vel consectetur lorem. Donec dapibus sed sem non feugiat. Curabitur egestas augue vel volutpat rutrum. Quisque ut nisl ut lorem fermentum finibus. Donec ut augue mauris.</p>
</article> </article>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script> <script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="jquery.readtime.js"></script> <script src="../build/jquery.readtime.min.js"></script>
<script> <script>
$('article').readtime(); $('article').readtime();
</script> </script>

View file

@ -1,5 +0,0 @@
/*
The MIT License - http://opensource.org/licenses/MIT
@link https://github.com/joshtronic/jquery.readtime
*/
(function(b){b.e.h=function(c){c=b.extend({a:"readtime",format:"# min read",b:"time"},c);return this.d(function(){var a=(this.textContent||this.innerText||"").replace(/(^\s*)|(\s*$)/gi,"").replace(/[ ]{2,}/gi," ").replace(/\n /,"\n").split(" ").length/275*60,a=a+12*b(this).find("img").length,a=Math.round(a/60),a=c.format.replace(/#/,a),d=document.createElement(c.b);b(d).c(c.a).f(a).g(b(this))})}})(jQuery);

14
package.json Normal file
View file

@ -0,0 +1,14 @@
{
"name": "jquery.readtime",
"version": "0.2.0",
"repository": {
"type": "git",
"url": "git://github.com/joshtronic/jquery.readme.git"
},
"devDependencies": {
"grunt": "^0.4.5",
"grunt-contrib-jshint": "^0.10.0",
"grunt-contrib-uglify": "^0.5.1",
"grunt-contrib-watch": "^0.6.1"
}
}

View file

@ -9,7 +9,6 @@
* @license The MIT License - http://opensource.org/licenses/MIT * @license The MIT License - http://opensource.org/licenses/MIT
* @link https://github.com/joshtronic/jquery.readtime * @link https://github.com/joshtronic/jquery.readtime
*/ */
(function($) (function($)
{ {
$.fn.readtime = function(options) $.fn.readtime = function(options)
@ -18,6 +17,8 @@
var defaults = { var defaults = {
class: 'readtime', // Class name of the output element class: 'readtime', // Class name of the output element
format: '# min read', // # == the number of minutes format: '# min read', // # == the number of minutes
images: 12, // Seconds per image, false to disable
wpm: 275, // Words per minute, defaults to average
wrapper: 'time' // What the output will be wrapped in wrapper: 'time' // What the output will be wrapped in
}; };
@ -34,10 +35,13 @@
.split(' ').length; .split(' ').length;
// Calculates the read time for the words (in seconds) // Calculates the read time for the words (in seconds)
var time = (words / 275) * 60; var time = (words / options.wpm) * 60;
// Counts any images in the element and adds more time // Counts any images in the element and adds more time
time += ($(this).find('img').length * 12); if (options.images)
{
time += ($(this).find('img').length * options.images);
}
// Gets us back to minutes and rounds to an integer // Gets us back to minutes and rounds to an integer
time = Math.round(time / 60); time = Math.round(time / 60);