update build steps

This commit is contained in:
Michael Barrett 2015-04-30 17:03:33 +01:00
parent 0a205f5e8a
commit f7dd41b765
5 changed files with 151 additions and 15 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
/node_modules/
/build/

View File

@ -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']);

View File

@ -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"
]
}

112
dist/timestring.js vendored Normal file
View File

@ -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);

View File

@ -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",