From 8c0926f5883b5a17c25ed155bb6e8823049fc88a Mon Sep 17 00:00:00 2001 From: Michael Barrett Date: Fri, 15 Jan 2016 10:00:12 +0000 Subject: [PATCH] back to basics --- .codeclimate.yml | 4 +- .gitignore | 3 +- .jscsrc | 4 +- .jshintrc | 4 +- .npmignore | 2 +- .travis.yml | 4 +- CHANGELOG.md | 2 + CONTRIBUTING.md | 32 ++------- Gulpfile.js | 91 ------------------------ README.md | 14 ---- bower.json | 15 ---- dist/lib/timestring.js | 122 -------------------------------- dist/timestring.js | 126 ---------------------------------- dist/timestring.min.js | 2 - index.js | 101 +++++++++++++++++++++++++++ package.json | 27 +++----- src/String.parseTime.js | 3 - src/Timestring.js | 91 ------------------------ test/timestring.js => test.js | 2 +- 19 files changed, 127 insertions(+), 522 deletions(-) delete mode 100644 Gulpfile.js delete mode 100644 bower.json delete mode 100644 dist/lib/timestring.js delete mode 100644 dist/timestring.js delete mode 100644 dist/timestring.min.js create mode 100644 index.js delete mode 100644 src/String.parseTime.js delete mode 100644 src/Timestring.js rename test/timestring.js => test.js (97%) diff --git a/.codeclimate.yml b/.codeclimate.yml index 14837a3..840ab4f 100644 --- a/.codeclimate.yml +++ b/.codeclimate.yml @@ -2,6 +2,4 @@ languages: JavaScript: true exclude_paths: - - Gulpfile.js - - test/* - - dist/* + - test.js diff --git a/.gitignore b/.gitignore index d49756d..3c3629e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -/node_modules/ -/build/ +node_modules diff --git a/.jscsrc b/.jscsrc index 47ecdd0..cc51063 100644 --- a/.jscsrc +++ b/.jscsrc @@ -1,4 +1,4 @@ { - "preset": "jquery", - "esnext": true + "preset": "node-style-guide", + "requireCapitalizedComments": false } diff --git a/.jshintrc b/.jshintrc index a5aaaed..0967ef4 100644 --- a/.jshintrc +++ b/.jshintrc @@ -1,3 +1 @@ -{ - "esnext": true -} +{} diff --git a/.npmignore b/.npmignore index 871d477..7a55b65 100644 --- a/.npmignore +++ b/.npmignore @@ -1,2 +1,2 @@ /* -!/dist/ +!/index.js diff --git a/.travis.yml b/.travis.yml index 09a3bb6..ffa8469 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,7 +17,9 @@ env: global: - secure: dILRjgEPjnDy8wcqSkKS4KoxSykeffpReeiRXcXNK9iJvoxNQpfEycVuC7eG4KmdHdLj8RmrPIHE3S2x+su7LD5Nly/StaxfYTWIi1++3wSLP+SlDdv9h+uy2uA6bMsP4hys2ArQ+i/P5V4r5zq1iLIf3mavIchNwTT3ThhOIso= -script: gulp ci +script: + - npm test + - npm run sa after_script: - istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec diff --git a/CHANGELOG.md b/CHANGELOG.md index d0014c2..457cfe6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ #Changelog +## 2.0.0 + ##1.1.1 - src now uses ES6 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f0e413e..b42e4e2 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,12 +1,12 @@ -#Contributing +# Contributing Contributions are **welcome** and will be fully **credited**. Contributions can be made via a Pull Request on [Github](https://github.com/mike182uk/timestring). -##Pull Requests +## Pull Requests -- **[jQuery coding style](https://contribute.jquery.org/style-guide/js/)** - [JSCS](http://jscs.info/). Make sure you run `gulp sa` before committing your code. +- **[Node coding style](https://github.com/felixge/node-style-guide)** - [JSCS](http://jscs.info/). Make sure you run `npm run sa` before committing your code. - **Add tests where appropriate** - [Mocha](http://mochajs.org/), [Chai](http://chaijs.com/) @@ -18,30 +18,8 @@ Contributions can be made via a Pull Request on [Github](https://github.com/mike - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please squash them before submitting. -##Building The Project - -[Gulp](http://gulpjs.com/) is used to build the project. The project uses ES6 so it needs to be transpiled. Once transpiled the code is then [browserified](http://browserify.org/). You can build the project by running: +## Running Tests ```bash -gulp build +npm test ``` - -This will perform all build steps, including running the tests and static analysis tools. - -##Running Tests - -Once the project has been built, the tests can be run with: - -```bash -gulp test -``` - -Alternatively, to build and test at the same time you can run: - -```bash -gulp ci -``` - -This will transpile, browserify and run the tests and static analysis tools. - - diff --git a/Gulpfile.js b/Gulpfile.js deleted file mode 100644 index a6a9889..0000000 --- a/Gulpfile.js +++ /dev/null @@ -1,91 +0,0 @@ -var gulp = require('gulp'); -var $ = require('gulp-load-plugins')({ - scope: 'devDependencies' -}); - -var browserify = require('browserify'); -var del = require('del'); -var runSequence = require('run-sequence'); -var source = require('vinyl-source-stream'); - -gulp.task('static-analysis:lint', function () { - gulp.src('./src/**/*.js') - .pipe($.jshint('.jshintrc')) - .pipe($.jshint.reporter('jshint-stylish')); -}); - -gulp.task('static-analysis:cs', function () { - return gulp.src('./src/**/*.js') - .pipe($.jscs()); -}); - -gulp.task('test', function () { - return gulp.src('./test/timestring.js', { read: false }) - .pipe($.mocha()); -}); - -gulp.task('build:clean', function (callback) { - del(['./build/**/*'], callback); -}); - -gulp.task('build:transpile', function () { - return gulp.src(['./src/Timestring.js', './src/String.parseTime.js']) - .pipe($.sourcemaps.init()) - .pipe($.concat('timestring.js')) - .pipe($.babel()) - .pipe($.sourcemaps.write()) - .pipe(gulp.dest('./build')); -}); - -gulp.task('build:lib', function () { - return gulp.src('./build/timestring.js') - .pipe(gulp.dest('./dist/lib')); -}); - -gulp.task('build:browserify', function() { - return browserify('./build/timestring.js', { standalone: 'Timestring', debug: true }) - .bundle() - .on('error', function(e){ - console.log(e.message); - - this.emit('end'); - }) - .pipe(source('timestring.js')) - .pipe(gulp.dest('dist')); -}); - -gulp.task('build:minify', function () { - return gulp.src('./dist/timestring.js') - .pipe($.sourcemaps.init({ loadMaps: true })) - .pipe($.uglify()) - .pipe($.rename({ - extname: '.min.js' - })) - .pipe($.sourcemaps.write()) - .pipe(gulp.dest('dist')); -}); - -gulp.task('build', function (callback) { - runSequence( - ['static-analysis:lint', 'static-analysis:cs'], - 'build:clean', - 'build:transpile', - 'test', - ['build:lib', 'build:browserify'], - 'build:minify', - callback - ); -}); - -gulp.task('sa', ['static-analysis:lint', 'static-analysis:cs']); - -gulp.task('ci', function (callback) { - runSequence( - ['static-analysis:lint', 'static-analysis:cs'], - 'build:clean', - 'build:transpile', - 'test' - ); -}); - -gulp.task('default', ['build']); diff --git a/README.md b/README.md index 93e00b2..349afaf 100644 --- a/README.md +++ b/README.md @@ -11,24 +11,10 @@ Parse a human readable time string into a time based value. ## Installation -### Node - ```bash npm install --save timestring ``` -### Browser - -```bash -bower install --save timestring -``` - -Then add a reference to the script in your HTML: - -```html - -``` - ## Usage ### Overview diff --git a/bower.json b/bower.json deleted file mode 100644 index 5d8693b..0000000 --- a/bower.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "name": "timestring", - "main": "dist/timestring.js", - "version": "1.1.1", - "description": "Parse a human readable time string into a time based value", - "homepage": "https://github.com/mike182uk/timestring", - "authors": [ - "Michael David Barrett " - ], - "license": "MIT", - "ignore": [ - "/*", - "!/dist/" - ] -} diff --git a/dist/lib/timestring.js b/dist/lib/timestring.js deleted file mode 100644 index d81f64d..0000000 --- a/dist/lib/timestring.js +++ /dev/null @@ -1,122 +0,0 @@ -"use strict"; - -Object.defineProperty(exports, "__esModule", { - value: true -}); - -var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); - -function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } - -var Timestring = (function () { - function Timestring() { - var settings = arguments[0] === undefined ? {} : arguments[0]; - - _classCallCheck(this, Timestring); - - var DEFAULT_SETTINGS = { - hoursPerDay: 24, - daysPerWeek: 7, - weeksPerMonth: 4, - monthsPerYear: 12 - }; - - // merge default settings with user settings - this.settings = DEFAULT_SETTINGS; - 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; - } - - _createClass(Timestring, [{ - key: "parse", - value: function parse(string) { - var _this = this; - - var returnUnit = arguments[1] === undefined ? "s" : arguments[1]; - - // get unit key helper - var getUnitKey = function getUnitKey(unit) { - for (var k in _this.units) { - for (var u in _this.units[k]) { - if (unit === _this.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 - var convert = function convert(value, unit) { - var baseValue = _this.unitValues[getUnitKey(unit)]; - - return value / baseValue; - }; - - // get a value in seconds based on a specific unit - var getSeconds = function getSeconds(value, unit) { - var baseValue = _this.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 group in groups) { - var g = groups[group]; - var value = g.match(/[0-9]+/g)[0]; - var unit = g.match(/[a-z]+/g)[0]; - - totalSeconds += getSeconds(value, unit); - } - } - - return convert(totalSeconds, returnUnit); - } - }]); - - return Timestring; -})(); - -exports["default"] = Timestring; - -String.prototype.parseTime = function (unit, settings) { - return new Timestring(settings).parse(this, unit); -}; -module.exports = exports["default"]; -//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIlRpbWVzdHJpbmcuanMiLCJTdHJpbmcucGFyc2VUaW1lLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7SUFBQSxVQUFBO0FBQ0EsV0FEQSxVQUFBLEdBQ0E7UUFBQSxRQUFBLGdDQUFBLEVBQUE7OzBCQURBLFVBQUE7O0FBRUEsUUFBQSxnQkFBQSxHQUFBO0FBQ0EsaUJBQUEsRUFBQSxFQUFBO0FBQ0EsaUJBQUEsRUFBQSxDQUFBO0FBQ0EsbUJBQUEsRUFBQSxDQUFBO0FBQ0EsbUJBQUEsRUFBQSxFQUFBO0tBQ0EsQ0FBQTs7O0FBR0EsUUFBQSxDQUFBLFFBQUEsR0FBQSxnQkFBQSxDQUFBO0FBQ0EsU0FBQSxJQUFBLENBQUEsSUFBQSxRQUFBLEVBQUE7QUFBQSxVQUFBLENBQUEsUUFBQSxDQUFBLENBQUEsQ0FBQSxHQUFBLFFBQUEsQ0FBQSxDQUFBLENBQUEsQ0FBQTtLQUFBOzs7QUFHQSxRQUFBLENBQUEsS0FBQSxHQUFBO0FBQ0EsT0FBQSxFQUFBLENBQUEsR0FBQSxFQUFBLEtBQUEsRUFBQSxNQUFBLEVBQUEsUUFBQSxFQUFBLFNBQUEsQ0FBQTtBQUNBLE9BQUEsRUFBQSxDQUFBLEdBQUEsRUFBQSxLQUFBLEVBQUEsTUFBQSxFQUFBLFFBQUEsRUFBQSxTQUFBLENBQUE7QUFDQSxPQUFBLEVBQUEsQ0FBQSxHQUFBLEVBQUEsSUFBQSxFQUFBLEtBQUEsRUFBQSxNQUFBLEVBQUEsT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sZXQgZyA9IGdyb3Vwc1tncm91cF07XG4gICAgICAgIGxldCB2YWx1ZSA9IGcubWF0Y2goIC9bMC05XSsvZyApWzBdO1xuICAgICAgICBsZXQgdW5pdCA9IGcubWF0Y2goIC9bYS16XSsvZyApWzBdO1xuXG4gICAgICAgIHRvdGFsU2Vjb25kcyArPSBnZXRTZWNvbmRzKCB2YWx1ZSwgdW5pdCApO1xuICAgICAgfVxuICAgIH1cblxuICAgIHJldHVybiBjb252ZXJ0KCB0b3RhbFNlY29uZHMsIHJldHVyblVuaXQgKTtcbiAgfVxufVxuIiwiU3RyaW5nLnByb3RvdHlwZS5wYXJzZVRpbWUgPSBmdW5jdGlvbiggdW5pdCwgc2V0dGluZ3MgKSB7XG4gIHJldHVybiAoIG5ldyBUaW1lc3RyaW5nKCBzZXR0aW5ncyApICkucGFyc2UoIHRoaXMsIHVuaXQgKTtcbn07XG4iXSwic291cmNlUm9vdCI6Ii9zb3VyY2UvIn0= \ No newline at end of file diff --git a/dist/timestring.js b/dist/timestring.js deleted file mode 100644 index 45ddd5f..0000000 --- a/dist/timestring.js +++ /dev/null @@ -1,126 +0,0 @@ -(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Timestring = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o { - for ( var k in this.units ) { - for ( var u in this.units[k] ) { - if ( unit === this.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 - let convert = ( value, unit ) => { - let baseValue = this.unitValues[getUnitKey( unit )]; - - return value / baseValue; - }; - - // get a value in seconds based on a specific unit - let getSeconds = ( value, unit ) => { - let baseValue = this.unitValues[getUnitKey( unit )]; - - return value * baseValue; - }; - - // seconds counter - let totalSeconds = 0; - - // split string into groups and get total seconds for each group - let 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 ( let group in groups ) { - let g = groups[group]; - let value = g.match( /[0-9]+/g )[0]; - let unit = g.match( /[a-z]+/g )[0]; - - totalSeconds += getSeconds( value, unit ); - } - } - - return convert( totalSeconds, returnUnit ); - } -} diff --git a/test/timestring.js b/test.js similarity index 97% rename from test/timestring.js rename to test.js index fc29f48..d20311c 100644 --- a/test/timestring.js +++ b/test.js @@ -1,7 +1,7 @@ var chai = require('chai'); var expect = chai.expect; -var timestring = require('../build/timestring'); +var timestring = require('./index'); describe('timestring', function() { it('can parse a timestring', function() {