From f7dd41b765b011bc7b76a538eb33e25c6a5765ee Mon Sep 17 00:00:00 2001 From: Michael Barrett Date: Thu, 30 Apr 2015 17:03:33 +0100 Subject: [PATCH] update build steps --- .gitignore | 1 + Gulpfile.js | 37 +++++++++++---- bower.json | 11 +++-- dist/timestring.js | 112 +++++++++++++++++++++++++++++++++++++++++++++ package.json | 5 +- 5 files changed, 151 insertions(+), 15 deletions(-) create mode 100644 dist/timestring.js diff --git a/.gitignore b/.gitignore index 2ccbe46..d49756d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /node_modules/ +/build/ diff --git a/Gulpfile.js b/Gulpfile.js index af67ad4..601dbd6 100644 --- a/Gulpfile.js +++ b/Gulpfile.js @@ -2,15 +2,17 @@ var gulp = require('gulp'); var $ = require('gulp-load-plugins')({ scope: 'devDependencies' }); -var runSequence = require('run-sequence'); -gulp.task('lint', function () { +var runSequence = require('run-sequence'); +var del = require('del'); + +gulp.task('static-analysis:lint', function () { gulp.src('src/**/*.js') .pipe($.jshint('.jshintrc')) .pipe($.jshint.reporter('jshint-stylish')); }); -gulp.task('cs', function () { +gulp.task('static-analysis:cs', function () { return gulp.src('src/**/*.js') .pipe($.jscs()); }); @@ -20,8 +22,22 @@ gulp.task('test', function () { .pipe($.mocha()); }); -gulp.task('minify', function () { - return gulp.src('src/*.js') +gulp.task('build:clean', function (callback) { + del(['build/**/*'], callback); +}); + +gulp.task('build:copy-src-to-build', function () { + return gulp.src('src/**/*.js') + .pipe(gulp.dest('build')); +}); + +gulp.task('build:copy-build-to-dist', function () { + return gulp.src('build/**/*.js') + .pipe(gulp.dest('dist')); +}); + +gulp.task('build:minify', function () { + return gulp.src('build/**/*.js') .pipe($.sourcemaps.init()) .pipe($.uglify()) .pipe($.rename({ @@ -33,12 +49,17 @@ gulp.task('minify', function () { gulp.task('build', function (callback) { runSequence( - ['lint', 'cs', 'test'], - 'minify', + ['static-analysis:lint', 'static-analysis:cs', 'test'], + 'build:clean', + 'build:copy-src-to-build', + 'build:copy-build-to-dist', + 'build:minify', callback ); }); -gulp.task('ci', ['lint', 'cs', 'test']); +gulp.task('sa', ['static-analysis:lint', 'static-analysis:cs']); + +gulp.task('ci', ['static-analysis:lint', 'static-analysis:cs', 'test']); gulp.task('default', ['build']); diff --git a/bower.json b/bower.json index 63d2a82..dce973d 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "timestring", - "main": "timestring.js", - "version": "1.1.0", + "main": "dist/timestring.js", + "version": "2.0.0", "description": "Parse a human readable time string into a time based value", "homepage": "https://github.com/mike182uk/timestring", "authors": [ @@ -10,11 +10,12 @@ "license": "MIT", "ignore": [ "**/.*", + "node_modules", + "test", "CHANGELOG.md", - "Gulpfile.js", "LICENSE", "README.md", - "node_modules", - "test" + "Gulpfile.js", + "package.json" ] } diff --git a/dist/timestring.js b/dist/timestring.js new file mode 100644 index 0000000..b0249b2 --- /dev/null +++ b/dist/timestring.js @@ -0,0 +1,112 @@ +(function(){ + "use strict"; + + var Timestring = function(settings) { + // default settings + var defaults = { + hoursPerDay: 24, + daysPerWeek: 7, + weeksPerMonth: 4, + monthsPerYear: 12 + }; + + // merge default settings with user settings + settings = settings || {}; + this.settings = defaults; + for (var s in settings) { this.settings[s] = settings[s]; } + + // time units + this.units = { + s: ['s', 'sec', 'secs', 'second', 'seconds'], + m: ['m', 'min', 'mins', 'minute', 'minutes'], + h: ['h', 'hr', 'hrs', 'hour', 'hours'], + d: ['d', 'day', 'days'], + w: ['w', 'week', 'weeks'], + mth: ['mth', 'mths','month', 'months'], + y: ['y', 'yr', 'yrs', 'year', 'years'] + }; + + // time unit seconds mappings + this.unitValues = { + s: 1, + m: 60, + h: 3600 + }; + + // dynamic time unit seconds mappings + // these are dynamic based on the settings + this.unitValues.d = this.settings.hoursPerDay * this.unitValues.h; + this.unitValues.w = this.settings.daysPerWeek * this.unitValues.d; + this.unitValues.mth = this.settings.weeksPerMonth * this.unitValues.w; + this.unitValues.y = this.settings.monthsPerYear * this.unitValues.mth; + }; + + Timestring.prototype.parse = function(string, returnUnit) { + // reference to this + var that = this; + + // get unit key helper + function getUnitKey(unit) { + for (var k in that.units) { + for (var u in that.units[k]) { + if (unit === that.units[k][u]) { + return k; + } + } + } + + // throw error if invalid unit was passed + throw new Error('The unit [' + unit + '] is not supported by timestring'); + } + + // convert a value to a specific unit + function convert(value, unit) { + var baseValue = that.unitValues[getUnitKey(unit)]; + + return value / baseValue; + } + + // get a value in seconds based on a specific unit + function getSeconds(value, unit) { + var baseValue = that.unitValues[getUnitKey(unit)]; + + return value * baseValue; + } + + // seconds counter + var totalSeconds = 0; + + // split string into groups and get total seconds for each group + var groups = string + .toLowerCase() // convert words to lower case + .replace(/[^\.\w+-]+/g, '') // remove white space + .match(/[-+]?[0-9]+[a-z]+/g); // match time groups (digit followed by time unit - i.e 5d 15m = 2 time groups) + + if (groups !== null) { + for(var i = 0; i < groups.length; i++) { + var g = groups[i]; + var value = g.match(/[0-9]+/g)[0]; + var unit = g.match(/[a-z]+/g)[0]; + + totalSeconds += getSeconds(value, unit); + } + } + + // return total, convert if needed + return (returnUnit) ? convert(totalSeconds, returnUnit) : totalSeconds; + }; + + // add convenience method to string prototype + String.prototype.parseTime = function (unit, settings) { + return (new Timestring(settings)).parse(this, unit); + }; + + // export Timestring object + if (typeof module !== 'undefined' && module.exports) { + module.exports = Timestring; + } + else { + this.Timestring = Timestring; + } + +}).call(this); diff --git a/package.json b/package.json index 97ebaec..bc2fd8a 100644 --- a/package.json +++ b/package.json @@ -7,10 +7,11 @@ "type": "git", "url": "git://github.com/mike182uk/timestring.git" }, - "main": "timestring.js", - "version": "1.1.0", + "main": "dist/timestring.js", + "version": "2.0.0", "devDependencies": { "chai": "^2.3.0", + "del": "^1.1.1", "gulp": "^3.8.11", "gulp-jscs": "^1.6.0", "gulp-jshint": "^1.10.0",