diff --git a/bower.json b/bower.json index fe86230..47d1c0f 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "kairos", - "version": "1.0.0", + "version": "1.1.0", "description": "A non date-based time calculator", "homepage": "https://github.com/kairos", "repository": { diff --git a/build/kairos-debug.js b/build/kairos-debug.js index a5d9c3b..2a32edc 100644 --- a/build/kairos-debug.js +++ b/build/kairos-debug.js @@ -1,7 +1,7 @@ /** * Kairos.js - A non date-based time calculator * @author Rodrigo Gomes da Silva - * @version v1.0.0 + * @version v1.1.0 * @link https://github.com/kairos * @license BSD-2-Clause */ @@ -51,7 +51,17 @@ var regex = /^[+-]?\d+(?::?\d{1,2}(?::\d{1,2}(?::\d{1,3})?)?)?$/; return regex.test(expression); }; - + + /** + * Return a Kairos.Gnomon instance. + * + * @param {String|Number} expression Time expression + * @returns {Kairos.Gnomon} + */ + Kairos.with = function (expression) { + return new Kairos.Gnomon(expression); + }; + /** * Sums augend time with addend time * @@ -62,10 +72,7 @@ * @returns {String} */ Kairos.plus = function (augend, addend) { - var a = new Kairos.Gnomon(augend); - var b = new Kairos.Gnomon(addend); - a.plus(b); - return a.toExpression(); + return Kairos.with(augend).plus(addend).toExpression(); }; /** @@ -78,10 +85,7 @@ * @returns {String} */ Kairos.minus = function (minuend, subtrahend) { - var a = new Kairos.Gnomon(minuend); - var b = new Kairos.Gnomon(subtrahend); - a.minus(b); - return a.toExpression(); + return Kairos.with(minuend).minus(subtrahend).toExpression(); }; /** @@ -94,9 +98,7 @@ * @returns {String} */ Kairos.multiply = function (multiplier, multiplicand) { - var m = new Kairos.Gnomon(multiplier); - m.multiply(multiplicand); - return m.toExpression(); + return Kairos.with(multiplier).multiply(multiplicand).toExpression(); }; /** @@ -109,11 +111,9 @@ * @returns {String} */ Kairos.divide = function (dividend, divisor) { - var d = new Kairos.Gnomon(dividend); - d.divide(divisor); - return d.toExpression(); + return Kairos.with(dividend).divide(divisor).toExpression(); }; - + /** * Returns a fraction of the current time * @@ -128,13 +128,9 @@ if (numerator > denominator) { throw new Error('Improper fraction'); } - - var gnomon = new Kairos.Gnomon(time); - gnomon.multiply(numerator); - gnomon.divide(denominator); - return gnomon.toExpression(); + return Kairos.with(time).multiply(numerator).divide(denominator).toExpression(); }; - + /** * Returns a time expression representing the time between starting time and ending time * @@ -150,9 +146,7 @@ if (st.compareTo(en) > 0) { throw new Error('Starting time must be bigger than ending time'); } - - en.minus(st); - return en.toExpression(); + return en.minus(st).toExpression(); }; /** @@ -164,8 +158,7 @@ * @returns {Number} */ Kairos.toMilliseconds = function (expression) { - var gnomon = new Kairos.Gnomon(expression); - return gnomon.toMilliseconds(); + return Kairos.with(expression).toMilliseconds(); }; /** @@ -177,8 +170,7 @@ * @returns {Number} */ Kairos.toSeconds = function (expression) { - var gnomon = new Kairos.Gnomon(expression); - return gnomon.toSeconds(); + return Kairos.with(expression).toSeconds(); }; /** @@ -190,8 +182,7 @@ * @returns {Number} */ Kairos.toMinutes = function (expression) { - var gnomon = new Kairos.Gnomon(expression); - return gnomon.toMinutes(); + return Kairos.with(expression).toMinutes(); }; /** @@ -203,10 +194,9 @@ * @returns {Number} */ Kairos.toHours = function (expression) { - var gnomon = new Kairos.Gnomon(expression); - return gnomon.toHours(); + return Kairos.with(expression).toHours(); }; - + /** * Compares first time with second time and returns -1, 0 or 1 if first value * is smaller, equals or bigger than second value @@ -218,11 +208,9 @@ * @returns {Number} */ Kairos.compare = function (time1, time2) { - var a = new Kairos.Gnomon(time1); - var b = new Kairos.Gnomon(time2); - return a.compareTo(b); + return Kairos.with(time1).compareTo(time2); }; - + /** * Returns the minimum value from the given values * @@ -235,7 +223,7 @@ if (!(values instanceof Array)) { values = Array.prototype.slice.call(arguments); } - + var min = values.reduce(function (previous, current) { if (!(previous instanceof Kairos.Gnomon)) { previous = new Kairos.Gnomon(previous ? previous : 0); @@ -248,7 +236,7 @@ return (min instanceof Kairos.Gnomon) ? min.toExpression() : new Kairos.Gnomon(min).toExpression(); }; - + /** * Returns the maximum value from the given values * @@ -261,7 +249,7 @@ if (!(values instanceof Array)) { values = Array.prototype.slice.call(arguments); } - + var max = values.reduce(function (previous, current) { if (!(previous instanceof Kairos.Gnomon)) { previous = new Kairos.Gnomon(previous ? previous : 0); @@ -271,10 +259,10 @@ } return ( previous.toMilliseconds() > current.toMilliseconds() ? previous : current ); }); - + return (max instanceof Kairos.Gnomon) ? max.toExpression() : new Kairos.Gnomon(max).toExpression(); }; - + // Node.js if (typeof module === 'object' && module.exports) { //=include /engine/Gnomon.js @@ -290,7 +278,7 @@ else { root.Kairos = Kairos; } - + // Polyfill Math.trunc = Math.trunc || function (x) { return x < 0 ? Math.ceil(x) : Math.floor(x); @@ -602,9 +590,7 @@ * @returns {Kairos.Gnomon} self */ Kairos.Gnomon.prototype.plus = function (addend) { - if (!(addend instanceof Kairos.Gnomon)) { - addend = new Kairos.Gnomon(addend); - } + addend = (addend instanceof Kairos.Gnomon) ? addend : new Kairos.Gnomon(addend); this.milliseconds += addend.toMilliseconds(); return this; }; @@ -615,9 +601,7 @@ * @returns {Kairos.Gnomon} self */ Kairos.Gnomon.prototype.minus = function (subtrahend) { - if (!(subtrahend instanceof Kairos.Gnomon)) { - subtrahend = new Kairos.Gnomon(subtrahend); - } + subtrahend = (subtrahend instanceof Kairos.Gnomon) ? subtrahend : new Kairos.Gnomon(subtrahend); this.milliseconds -= subtrahend.toMilliseconds(); return this; }; @@ -648,10 +632,12 @@ * Equals 0 * Bigger 1 * - * @param {Kairos.Gnomon} another + * @param {String|Number|Kairos.Gnomon} another Expression to compare with * @returns {Number} */ Kairos.Gnomon.prototype.compareTo = function (another) { + another = (another instanceof Kairos.Gnomon) ? another : new Kairos.Gnomon(another); + if (this.milliseconds < another.toMilliseconds()) { return -1; } diff --git a/build/kairos-min.js b/build/kairos-min.js index 6a429ba..9cd7c00 100644 --- a/build/kairos-min.js +++ b/build/kairos-min.js @@ -1,9 +1,9 @@ /** * Kairos.js - A non date-based time calculator * @author Rodrigo Gomes da Silva - * @version v1.0.0 + * @version v1.1.0 * @link https://github.com/kairos * @license BSD-2-Clause */ -!function(){"use strict";var n,o={},e="object"==typeof self&&self.self===self&&self||"object"==typeof global&&global.global===global&&global||this;null!==e&&(n=e.Kairos),o.noConflict=function(){return e.Kairos=n,o},o.validateExpression=function(n){var o=/^[+-]?\d+(?::?\d{1,2}(?::\d{1,2}(?::\d{1,3})?)?)?$/;return o.test(n)},o.plus=function(n,e){var t=new o.Gnomon(n),r=new o.Gnomon(e);return t.plus(r),t.toExpression()},o.minus=function(n,e){var t=new o.Gnomon(n),r=new o.Gnomon(e);return t.minus(r),t.toExpression()},o.multiply=function(n,e){var t=new o.Gnomon(n);return t.multiply(e),t.toExpression()},o.divide=function(n,e){var t=new o.Gnomon(n);return t.divide(e),t.toExpression()},o.getFraction=function(n,e,t){if(e>t)throw new Error("Improper fraction");var r=new o.Gnomon(n);return r.multiply(e),r.divide(t),r.toExpression()},o.getInterval=function(n,e){var t=new o.Gnomon(n),r=new o.Gnomon(e);if(t.compareTo(r)>0)throw new Error("Starting time must be bigger than ending time");return r.minus(t),r.toExpression()},o.toMilliseconds=function(n){var e=new o.Gnomon(n);return e.toMilliseconds()},o.toSeconds=function(n){var e=new o.Gnomon(n);return e.toSeconds()},o.toMinutes=function(n){var e=new o.Gnomon(n);return e.toMinutes()},o.toHours=function(n){var e=new o.Gnomon(n);return e.toHours()},o.compare=function(n,e){var t=new o.Gnomon(n),r=new o.Gnomon(e);return t.compareTo(r)},o.min=function(n){n instanceof Array||(n=Array.prototype.slice.call(arguments));var e=n.reduce(function(n,e){return n instanceof o.Gnomon||(n=new o.Gnomon(n?n:0)),e instanceof o.Gnomon||(e=new o.Gnomon(e?e:0)),n.toMilliseconds()e.toMilliseconds()?n:e});return e instanceof o.Gnomon?e.toExpression():new o.Gnomon(e).toExpression()},"object"==typeof module&&module.exports?module.exports=o:"function"==typeof define&&define.amd?define([],function(){return o}):e.Kairos=o,Math.trunc=Math.trunc||function(n){return 0>n?Math.ceil(n):Math.floor(n)}}(); -!function(){"use strict";var o={SECOND:1e3,MINUTE:6e4,HOUR:36e5};Kairos.Gnomon=function(i){if("number"==typeof i)this.milliseconds=i;else if("string"==typeof i&&i.length>0){if(!Kairos.validateExpression(i))throw new Error("Invalid time expression");for(var t=i.split(":"),n="-"!==i.slice(0,1)[0],e=0,r=t.length;r>e;e++){var c=t[e];switch(c=Math.abs(c),e){case 0:this.milliseconds=s(this,o.HOUR,c);break;case 1:this.milliseconds=s(this,o.MINUTE,c);break;case 2:this.milliseconds=s(this,o.SECOND,c);break;case 3:this.milliseconds=s(this,1,c)}}n||(this.milliseconds=-Math.abs(this.milliseconds))}};var s=function(s,i,t){switch(i){case 1:s.removeMilliseconds(s.getMilliseconds());break;case o.SECOND:s.removeSeconds(s.getSeconds());break;case o.MINUTE:s.removeMinutes(s.getMinutes());break;case o.HOUR:s.removeHours(s.getHours())}return s.milliseconds+t*i};Kairos.Gnomon.prototype.milliseconds=0,Kairos.Gnomon.prototype.setHours=function(i){return this.milliseconds=s(this,o.HOUR,i),this},Kairos.Gnomon.prototype.getHours=function(){return Math.trunc(this.milliseconds/o.HOUR)},Kairos.Gnomon.prototype.setMinutes=function(i){return this.milliseconds=s(this,o.MINUTE,i),this},Kairos.Gnomon.prototype.getMinutes=function(){return Math.trunc(Math.trunc(this.milliseconds-Math.trunc(this.toHours())*o.HOUR)/o.MINUTE)},Kairos.Gnomon.prototype.setSeconds=function(i){return this.milliseconds=s(this,o.SECOND,i),this},Kairos.Gnomon.prototype.getSeconds=function(){return Math.trunc(Math.trunc(this.milliseconds-Math.trunc(this.toMinutes())*o.MINUTE)/o.SECOND)},Kairos.Gnomon.prototype.setMilliseconds=function(o){return this.milliseconds=s(this,1,o),this},Kairos.Gnomon.prototype.getMilliseconds=function(){return Math.trunc(this.milliseconds-Math.trunc(this.toSeconds())*o.SECOND)},Kairos.Gnomon.prototype.addHours=function(s){return this.milliseconds+=o.HOUR*s,this},Kairos.Gnomon.prototype.addMinutes=function(s){return this.milliseconds+=o.MINUTE*s,this},Kairos.Gnomon.prototype.addSeconds=function(s){return this.milliseconds+=o.SECOND*s,this},Kairos.Gnomon.prototype.addMilliseconds=function(o){return this.milliseconds+=o,this},Kairos.Gnomon.prototype.removeHours=function(s){return this.milliseconds-=o.HOUR*s,this},Kairos.Gnomon.prototype.removeMinutes=function(s){return this.milliseconds-=o.MINUTE*s,this},Kairos.Gnomon.prototype.removeSeconds=function(s){return this.milliseconds-=o.SECOND*s,this},Kairos.Gnomon.prototype.removeMilliseconds=function(o){return this.milliseconds-=o,this},Kairos.Gnomon.prototype.toHours=function(){return this.milliseconds/o.HOUR},Kairos.Gnomon.prototype.toMinutes=function(){return this.milliseconds/o.MINUTE},Kairos.Gnomon.prototype.toSeconds=function(){return this.milliseconds/o.SECOND},Kairos.Gnomon.prototype.toMilliseconds=function(){return this.milliseconds},Kairos.Gnomon.prototype.toExpression=function(){var o="",s=Math.trunc(Math.abs(this.getHours()));return o+=(String(s).length>1?"":"0")+s+":",o+=("00"+Math.trunc(Math.abs(this.getMinutes()))).slice(-2),(0!==this.getSeconds()||0!==this.getMilliseconds())&&(o+=":"+("00"+Math.trunc(Math.abs(this.getSeconds()))).slice(-2)),0!==this.getMilliseconds()&&(o+=":"+("000"+Math.trunc(Math.abs(this.getMilliseconds()))).slice(-3)),this.milliseconds<0&&(o="-"+o),o},Kairos.Gnomon.prototype.plus=function(o){return o instanceof Kairos.Gnomon||(o=new Kairos.Gnomon(o)),this.milliseconds+=o.toMilliseconds(),this},Kairos.Gnomon.prototype.minus=function(o){return o instanceof Kairos.Gnomon||(o=new Kairos.Gnomon(o)),this.milliseconds-=o.toMilliseconds(),this},Kairos.Gnomon.prototype.multiply=function(o){return this.milliseconds*=o,this},Kairos.Gnomon.prototype.divide=function(o){return this.milliseconds/=o,this},Kairos.Gnomon.prototype.compareTo=function(o){return this.millisecondso.toMilliseconds()?1:void 0}}(); \ No newline at end of file +!function(){"use strict";var n,o={},t="object"==typeof self&&self.self===self&&self||"object"==typeof global&&global.global===global&&global||this;null!==t&&(n=t.Kairos),o.noConflict=function(){return t.Kairos=n,o},o.validateExpression=function(n){var o=/^[+-]?\d+(?::?\d{1,2}(?::\d{1,2}(?::\d{1,3})?)?)?$/;return o.test(n)},o["with"]=function(n){return new o.Gnomon(n)},o.plus=function(n,t){return o["with"](n).plus(t).toExpression()},o.minus=function(n,t){return o["with"](n).minus(t).toExpression()},o.multiply=function(n,t){return o["with"](n).multiply(t).toExpression()},o.divide=function(n,t){return o["with"](n).divide(t).toExpression()},o.getFraction=function(n,t,e){if(t>e)throw new Error("Improper fraction");return o["with"](n).multiply(t).divide(e).toExpression()},o.getInterval=function(n,t){var e=new o.Gnomon(n),i=new o.Gnomon(t);if(e.compareTo(i)>0)throw new Error("Starting time must be bigger than ending time");return i.minus(e).toExpression()},o.toMilliseconds=function(n){return o["with"](n).toMilliseconds()},o.toSeconds=function(n){return o["with"](n).toSeconds()},o.toMinutes=function(n){return o["with"](n).toMinutes()},o.toHours=function(n){return o["with"](n).toHours()},o.compare=function(n,t){return o["with"](n).compareTo(t)},o.min=function(n){n instanceof Array||(n=Array.prototype.slice.call(arguments));var t=n.reduce(function(n,t){return n instanceof o.Gnomon||(n=new o.Gnomon(n?n:0)),t instanceof o.Gnomon||(t=new o.Gnomon(t?t:0)),n.toMilliseconds()t.toMilliseconds()?n:t});return t instanceof o.Gnomon?t.toExpression():new o.Gnomon(t).toExpression()},"object"==typeof module&&module.exports?module.exports=o:"function"==typeof define&&define.amd?define([],function(){return o}):t.Kairos=o,Math.trunc=Math.trunc||function(n){return 0>n?Math.ceil(n):Math.floor(n)}}(); +!function(){"use strict";var o={SECOND:1e3,MINUTE:6e4,HOUR:36e5};Kairos.Gnomon=function(i){if("number"==typeof i)this.milliseconds=i;else if("string"==typeof i&&i.length>0){if(!Kairos.validateExpression(i))throw new Error("Invalid time expression");for(var t=i.split(":"),n="-"!==i.slice(0,1)[0],e=0,r=t.length;r>e;e++){var c=t[e];switch(c=Math.abs(c),e){case 0:this.milliseconds=s(this,o.HOUR,c);break;case 1:this.milliseconds=s(this,o.MINUTE,c);break;case 2:this.milliseconds=s(this,o.SECOND,c);break;case 3:this.milliseconds=s(this,1,c)}}n||(this.milliseconds=-Math.abs(this.milliseconds))}};var s=function(s,i,t){switch(i){case 1:s.removeMilliseconds(s.getMilliseconds());break;case o.SECOND:s.removeSeconds(s.getSeconds());break;case o.MINUTE:s.removeMinutes(s.getMinutes());break;case o.HOUR:s.removeHours(s.getHours())}return s.milliseconds+t*i};Kairos.Gnomon.prototype.milliseconds=0,Kairos.Gnomon.prototype.setHours=function(i){return this.milliseconds=s(this,o.HOUR,i),this},Kairos.Gnomon.prototype.getHours=function(){return Math.trunc(this.milliseconds/o.HOUR)},Kairos.Gnomon.prototype.setMinutes=function(i){return this.milliseconds=s(this,o.MINUTE,i),this},Kairos.Gnomon.prototype.getMinutes=function(){return Math.trunc(Math.trunc(this.milliseconds-Math.trunc(this.toHours())*o.HOUR)/o.MINUTE)},Kairos.Gnomon.prototype.setSeconds=function(i){return this.milliseconds=s(this,o.SECOND,i),this},Kairos.Gnomon.prototype.getSeconds=function(){return Math.trunc(Math.trunc(this.milliseconds-Math.trunc(this.toMinutes())*o.MINUTE)/o.SECOND)},Kairos.Gnomon.prototype.setMilliseconds=function(o){return this.milliseconds=s(this,1,o),this},Kairos.Gnomon.prototype.getMilliseconds=function(){return Math.trunc(this.milliseconds-Math.trunc(this.toSeconds())*o.SECOND)},Kairos.Gnomon.prototype.addHours=function(s){return this.milliseconds+=o.HOUR*s,this},Kairos.Gnomon.prototype.addMinutes=function(s){return this.milliseconds+=o.MINUTE*s,this},Kairos.Gnomon.prototype.addSeconds=function(s){return this.milliseconds+=o.SECOND*s,this},Kairos.Gnomon.prototype.addMilliseconds=function(o){return this.milliseconds+=o,this},Kairos.Gnomon.prototype.removeHours=function(s){return this.milliseconds-=o.HOUR*s,this},Kairos.Gnomon.prototype.removeMinutes=function(s){return this.milliseconds-=o.MINUTE*s,this},Kairos.Gnomon.prototype.removeSeconds=function(s){return this.milliseconds-=o.SECOND*s,this},Kairos.Gnomon.prototype.removeMilliseconds=function(o){return this.milliseconds-=o,this},Kairos.Gnomon.prototype.toHours=function(){return this.milliseconds/o.HOUR},Kairos.Gnomon.prototype.toMinutes=function(){return this.milliseconds/o.MINUTE},Kairos.Gnomon.prototype.toSeconds=function(){return this.milliseconds/o.SECOND},Kairos.Gnomon.prototype.toMilliseconds=function(){return this.milliseconds},Kairos.Gnomon.prototype.toExpression=function(){var o="",s=Math.trunc(Math.abs(this.getHours()));return o+=(String(s).length>1?"":"0")+s+":",o+=("00"+Math.trunc(Math.abs(this.getMinutes()))).slice(-2),(0!==this.getSeconds()||0!==this.getMilliseconds())&&(o+=":"+("00"+Math.trunc(Math.abs(this.getSeconds()))).slice(-2)),0!==this.getMilliseconds()&&(o+=":"+("000"+Math.trunc(Math.abs(this.getMilliseconds()))).slice(-3)),this.milliseconds<0&&(o="-"+o),o},Kairos.Gnomon.prototype.plus=function(o){return o=o instanceof Kairos.Gnomon?o:new Kairos.Gnomon(o),this.milliseconds+=o.toMilliseconds(),this},Kairos.Gnomon.prototype.minus=function(o){return o=o instanceof Kairos.Gnomon?o:new Kairos.Gnomon(o),this.milliseconds-=o.toMilliseconds(),this},Kairos.Gnomon.prototype.multiply=function(o){return this.milliseconds*=o,this},Kairos.Gnomon.prototype.divide=function(o){return this.milliseconds/=o,this},Kairos.Gnomon.prototype.compareTo=function(o){return o=o instanceof Kairos.Gnomon?o:new Kairos.Gnomon(o),this.millisecondso.toMilliseconds()?1:void 0}}(); \ No newline at end of file diff --git a/build/kairos-node.js b/build/kairos-node.js index c1786b0..003ed22 100644 --- a/build/kairos-node.js +++ b/build/kairos-node.js @@ -1,7 +1,7 @@ /** * Kairos.js - A non date-based time calculator * @author Rodrigo Gomes da Silva - * @version v1.0.0 + * @version v1.1.0 * @link https://github.com/kairos * @license BSD-2-Clause */ @@ -51,7 +51,17 @@ var regex = /^[+-]?\d+(?::?\d{1,2}(?::\d{1,2}(?::\d{1,3})?)?)?$/; return regex.test(expression); }; - + + /** + * Return a Kairos.Gnomon instance. + * + * @param {String|Number} expression Time expression + * @returns {Kairos.Gnomon} + */ + Kairos.with = function (expression) { + return new Kairos.Gnomon(expression); + }; + /** * Sums augend time with addend time * @@ -62,10 +72,7 @@ * @returns {String} */ Kairos.plus = function (augend, addend) { - var a = new Kairos.Gnomon(augend); - var b = new Kairos.Gnomon(addend); - a.plus(b); - return a.toExpression(); + return Kairos.with(augend).plus(addend).toExpression(); }; /** @@ -78,10 +85,7 @@ * @returns {String} */ Kairos.minus = function (minuend, subtrahend) { - var a = new Kairos.Gnomon(minuend); - var b = new Kairos.Gnomon(subtrahend); - a.minus(b); - return a.toExpression(); + return Kairos.with(minuend).minus(subtrahend).toExpression(); }; /** @@ -94,9 +98,7 @@ * @returns {String} */ Kairos.multiply = function (multiplier, multiplicand) { - var m = new Kairos.Gnomon(multiplier); - m.multiply(multiplicand); - return m.toExpression(); + return Kairos.with(multiplier).multiply(multiplicand).toExpression(); }; /** @@ -109,11 +111,9 @@ * @returns {String} */ Kairos.divide = function (dividend, divisor) { - var d = new Kairos.Gnomon(dividend); - d.divide(divisor); - return d.toExpression(); + return Kairos.with(dividend).divide(divisor).toExpression(); }; - + /** * Returns a fraction of the current time * @@ -128,13 +128,9 @@ if (numerator > denominator) { throw new Error('Improper fraction'); } - - var gnomon = new Kairos.Gnomon(time); - gnomon.multiply(numerator); - gnomon.divide(denominator); - return gnomon.toExpression(); + return Kairos.with(time).multiply(numerator).divide(denominator).toExpression(); }; - + /** * Returns a time expression representing the time between starting time and ending time * @@ -150,9 +146,7 @@ if (st.compareTo(en) > 0) { throw new Error('Starting time must be bigger than ending time'); } - - en.minus(st); - return en.toExpression(); + return en.minus(st).toExpression(); }; /** @@ -164,8 +158,7 @@ * @returns {Number} */ Kairos.toMilliseconds = function (expression) { - var gnomon = new Kairos.Gnomon(expression); - return gnomon.toMilliseconds(); + return Kairos.with(expression).toMilliseconds(); }; /** @@ -177,8 +170,7 @@ * @returns {Number} */ Kairos.toSeconds = function (expression) { - var gnomon = new Kairos.Gnomon(expression); - return gnomon.toSeconds(); + return Kairos.with(expression).toSeconds(); }; /** @@ -190,8 +182,7 @@ * @returns {Number} */ Kairos.toMinutes = function (expression) { - var gnomon = new Kairos.Gnomon(expression); - return gnomon.toMinutes(); + return Kairos.with(expression).toMinutes(); }; /** @@ -203,10 +194,9 @@ * @returns {Number} */ Kairos.toHours = function (expression) { - var gnomon = new Kairos.Gnomon(expression); - return gnomon.toHours(); + return Kairos.with(expression).toHours(); }; - + /** * Compares first time with second time and returns -1, 0 or 1 if first value * is smaller, equals or bigger than second value @@ -218,11 +208,9 @@ * @returns {Number} */ Kairos.compare = function (time1, time2) { - var a = new Kairos.Gnomon(time1); - var b = new Kairos.Gnomon(time2); - return a.compareTo(b); + return Kairos.with(time1).compareTo(time2); }; - + /** * Returns the minimum value from the given values * @@ -235,7 +223,7 @@ if (!(values instanceof Array)) { values = Array.prototype.slice.call(arguments); } - + var min = values.reduce(function (previous, current) { if (!(previous instanceof Kairos.Gnomon)) { previous = new Kairos.Gnomon(previous ? previous : 0); @@ -248,7 +236,7 @@ return (min instanceof Kairos.Gnomon) ? min.toExpression() : new Kairos.Gnomon(min).toExpression(); }; - + /** * Returns the maximum value from the given values * @@ -261,7 +249,7 @@ if (!(values instanceof Array)) { values = Array.prototype.slice.call(arguments); } - + var max = values.reduce(function (previous, current) { if (!(previous instanceof Kairos.Gnomon)) { previous = new Kairos.Gnomon(previous ? previous : 0); @@ -271,10 +259,10 @@ } return ( previous.toMilliseconds() > current.toMilliseconds() ? previous : current ); }); - + return (max instanceof Kairos.Gnomon) ? max.toExpression() : new Kairos.Gnomon(max).toExpression(); }; - + // Node.js if (typeof module === 'object' && module.exports) { (function () { @@ -583,9 +571,7 @@ * @returns {Kairos.Gnomon} self */ Kairos.Gnomon.prototype.plus = function (addend) { - if (!(addend instanceof Kairos.Gnomon)) { - addend = new Kairos.Gnomon(addend); - } + addend = (addend instanceof Kairos.Gnomon) ? addend : new Kairos.Gnomon(addend); this.milliseconds += addend.toMilliseconds(); return this; }; @@ -596,9 +582,7 @@ * @returns {Kairos.Gnomon} self */ Kairos.Gnomon.prototype.minus = function (subtrahend) { - if (!(subtrahend instanceof Kairos.Gnomon)) { - subtrahend = new Kairos.Gnomon(subtrahend); - } + subtrahend = (subtrahend instanceof Kairos.Gnomon) ? subtrahend : new Kairos.Gnomon(subtrahend); this.milliseconds -= subtrahend.toMilliseconds(); return this; }; @@ -629,10 +613,12 @@ * Equals 0 * Bigger 1 * - * @param {Kairos.Gnomon} another + * @param {String|Number|Kairos.Gnomon} another Expression to compare with * @returns {Number} */ Kairos.Gnomon.prototype.compareTo = function (another) { + another = (another instanceof Kairos.Gnomon) ? another : new Kairos.Gnomon(another); + if (this.milliseconds < another.toMilliseconds()) { return -1; } @@ -656,7 +642,7 @@ else { root.Kairos = Kairos; } - + // Polyfill Math.trunc = Math.trunc || function (x) { return x < 0 ? Math.ceil(x) : Math.floor(x); diff --git a/build/kairos.js b/build/kairos.js index a5d9c3b..2a32edc 100644 --- a/build/kairos.js +++ b/build/kairos.js @@ -1,7 +1,7 @@ /** * Kairos.js - A non date-based time calculator * @author Rodrigo Gomes da Silva - * @version v1.0.0 + * @version v1.1.0 * @link https://github.com/kairos * @license BSD-2-Clause */ @@ -51,7 +51,17 @@ var regex = /^[+-]?\d+(?::?\d{1,2}(?::\d{1,2}(?::\d{1,3})?)?)?$/; return regex.test(expression); }; - + + /** + * Return a Kairos.Gnomon instance. + * + * @param {String|Number} expression Time expression + * @returns {Kairos.Gnomon} + */ + Kairos.with = function (expression) { + return new Kairos.Gnomon(expression); + }; + /** * Sums augend time with addend time * @@ -62,10 +72,7 @@ * @returns {String} */ Kairos.plus = function (augend, addend) { - var a = new Kairos.Gnomon(augend); - var b = new Kairos.Gnomon(addend); - a.plus(b); - return a.toExpression(); + return Kairos.with(augend).plus(addend).toExpression(); }; /** @@ -78,10 +85,7 @@ * @returns {String} */ Kairos.minus = function (minuend, subtrahend) { - var a = new Kairos.Gnomon(minuend); - var b = new Kairos.Gnomon(subtrahend); - a.minus(b); - return a.toExpression(); + return Kairos.with(minuend).minus(subtrahend).toExpression(); }; /** @@ -94,9 +98,7 @@ * @returns {String} */ Kairos.multiply = function (multiplier, multiplicand) { - var m = new Kairos.Gnomon(multiplier); - m.multiply(multiplicand); - return m.toExpression(); + return Kairos.with(multiplier).multiply(multiplicand).toExpression(); }; /** @@ -109,11 +111,9 @@ * @returns {String} */ Kairos.divide = function (dividend, divisor) { - var d = new Kairos.Gnomon(dividend); - d.divide(divisor); - return d.toExpression(); + return Kairos.with(dividend).divide(divisor).toExpression(); }; - + /** * Returns a fraction of the current time * @@ -128,13 +128,9 @@ if (numerator > denominator) { throw new Error('Improper fraction'); } - - var gnomon = new Kairos.Gnomon(time); - gnomon.multiply(numerator); - gnomon.divide(denominator); - return gnomon.toExpression(); + return Kairos.with(time).multiply(numerator).divide(denominator).toExpression(); }; - + /** * Returns a time expression representing the time between starting time and ending time * @@ -150,9 +146,7 @@ if (st.compareTo(en) > 0) { throw new Error('Starting time must be bigger than ending time'); } - - en.minus(st); - return en.toExpression(); + return en.minus(st).toExpression(); }; /** @@ -164,8 +158,7 @@ * @returns {Number} */ Kairos.toMilliseconds = function (expression) { - var gnomon = new Kairos.Gnomon(expression); - return gnomon.toMilliseconds(); + return Kairos.with(expression).toMilliseconds(); }; /** @@ -177,8 +170,7 @@ * @returns {Number} */ Kairos.toSeconds = function (expression) { - var gnomon = new Kairos.Gnomon(expression); - return gnomon.toSeconds(); + return Kairos.with(expression).toSeconds(); }; /** @@ -190,8 +182,7 @@ * @returns {Number} */ Kairos.toMinutes = function (expression) { - var gnomon = new Kairos.Gnomon(expression); - return gnomon.toMinutes(); + return Kairos.with(expression).toMinutes(); }; /** @@ -203,10 +194,9 @@ * @returns {Number} */ Kairos.toHours = function (expression) { - var gnomon = new Kairos.Gnomon(expression); - return gnomon.toHours(); + return Kairos.with(expression).toHours(); }; - + /** * Compares first time with second time and returns -1, 0 or 1 if first value * is smaller, equals or bigger than second value @@ -218,11 +208,9 @@ * @returns {Number} */ Kairos.compare = function (time1, time2) { - var a = new Kairos.Gnomon(time1); - var b = new Kairos.Gnomon(time2); - return a.compareTo(b); + return Kairos.with(time1).compareTo(time2); }; - + /** * Returns the minimum value from the given values * @@ -235,7 +223,7 @@ if (!(values instanceof Array)) { values = Array.prototype.slice.call(arguments); } - + var min = values.reduce(function (previous, current) { if (!(previous instanceof Kairos.Gnomon)) { previous = new Kairos.Gnomon(previous ? previous : 0); @@ -248,7 +236,7 @@ return (min instanceof Kairos.Gnomon) ? min.toExpression() : new Kairos.Gnomon(min).toExpression(); }; - + /** * Returns the maximum value from the given values * @@ -261,7 +249,7 @@ if (!(values instanceof Array)) { values = Array.prototype.slice.call(arguments); } - + var max = values.reduce(function (previous, current) { if (!(previous instanceof Kairos.Gnomon)) { previous = new Kairos.Gnomon(previous ? previous : 0); @@ -271,10 +259,10 @@ } return ( previous.toMilliseconds() > current.toMilliseconds() ? previous : current ); }); - + return (max instanceof Kairos.Gnomon) ? max.toExpression() : new Kairos.Gnomon(max).toExpression(); }; - + // Node.js if (typeof module === 'object' && module.exports) { //=include /engine/Gnomon.js @@ -290,7 +278,7 @@ else { root.Kairos = Kairos; } - + // Polyfill Math.trunc = Math.trunc || function (x) { return x < 0 ? Math.ceil(x) : Math.floor(x); @@ -602,9 +590,7 @@ * @returns {Kairos.Gnomon} self */ Kairos.Gnomon.prototype.plus = function (addend) { - if (!(addend instanceof Kairos.Gnomon)) { - addend = new Kairos.Gnomon(addend); - } + addend = (addend instanceof Kairos.Gnomon) ? addend : new Kairos.Gnomon(addend); this.milliseconds += addend.toMilliseconds(); return this; }; @@ -615,9 +601,7 @@ * @returns {Kairos.Gnomon} self */ Kairos.Gnomon.prototype.minus = function (subtrahend) { - if (!(subtrahend instanceof Kairos.Gnomon)) { - subtrahend = new Kairos.Gnomon(subtrahend); - } + subtrahend = (subtrahend instanceof Kairos.Gnomon) ? subtrahend : new Kairos.Gnomon(subtrahend); this.milliseconds -= subtrahend.toMilliseconds(); return this; }; @@ -648,10 +632,12 @@ * Equals 0 * Bigger 1 * - * @param {Kairos.Gnomon} another + * @param {String|Number|Kairos.Gnomon} another Expression to compare with * @returns {Number} */ Kairos.Gnomon.prototype.compareTo = function (another) { + another = (another instanceof Kairos.Gnomon) ? another : new Kairos.Gnomon(another); + if (this.milliseconds < another.toMilliseconds()) { return -1; } diff --git a/package.json b/package.json index d9e60db..9656e75 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kairos", - "version": "1.0.0", + "version": "1.1.0", "description": "A non date-based time calculator", "license": "BSD-2-Clause", "repository": "kairos", diff --git a/src/engine/Gnomon.js b/src/engine/Gnomon.js index 6507797..0d1fab8 100644 --- a/src/engine/Gnomon.js +++ b/src/engine/Gnomon.js @@ -304,9 +304,7 @@ * @returns {Kairos.Gnomon} self */ Kairos.Gnomon.prototype.plus = function (addend) { - if (!(addend instanceof Kairos.Gnomon)) { - addend = new Kairos.Gnomon(addend); - } + addend = (addend instanceof Kairos.Gnomon) ? addend : new Kairos.Gnomon(addend); this.milliseconds += addend.toMilliseconds(); return this; }; @@ -317,9 +315,7 @@ * @returns {Kairos.Gnomon} self */ Kairos.Gnomon.prototype.minus = function (subtrahend) { - if (!(subtrahend instanceof Kairos.Gnomon)) { - subtrahend = new Kairos.Gnomon(subtrahend); - } + subtrahend = (subtrahend instanceof Kairos.Gnomon) ? subtrahend : new Kairos.Gnomon(subtrahend); this.milliseconds -= subtrahend.toMilliseconds(); return this; }; @@ -350,10 +346,12 @@ * Equals 0 * Bigger 1 * - * @param {Kairos.Gnomon} another + * @param {String|Number|Kairos.Gnomon} another Expression to compare with * @returns {Number} */ Kairos.Gnomon.prototype.compareTo = function (another) { + another = (another instanceof Kairos.Gnomon) ? another : new Kairos.Gnomon(another); + if (this.milliseconds < another.toMilliseconds()) { return -1; } diff --git a/src/kairos.js b/src/kairos.js index 6fccb2d..c22001b 100644 --- a/src/kairos.js +++ b/src/kairos.js @@ -44,7 +44,17 @@ var regex = /^[+-]?\d+(?::?\d{1,2}(?::\d{1,2}(?::\d{1,3})?)?)?$/; return regex.test(expression); }; - + + /** + * Return a Kairos.Gnomon instance. + * + * @param {String|Number} expression Time expression + * @returns {Kairos.Gnomon} + */ + Kairos.with = function (expression) { + return new Kairos.Gnomon(expression); + }; + /** * Sums augend time with addend time * @@ -55,10 +65,7 @@ * @returns {String} */ Kairos.plus = function (augend, addend) { - var a = new Kairos.Gnomon(augend); - var b = new Kairos.Gnomon(addend); - a.plus(b); - return a.toExpression(); + return Kairos.with(augend).plus(addend).toExpression(); }; /** @@ -71,10 +78,7 @@ * @returns {String} */ Kairos.minus = function (minuend, subtrahend) { - var a = new Kairos.Gnomon(minuend); - var b = new Kairos.Gnomon(subtrahend); - a.minus(b); - return a.toExpression(); + return Kairos.with(minuend).minus(subtrahend).toExpression(); }; /** @@ -87,9 +91,7 @@ * @returns {String} */ Kairos.multiply = function (multiplier, multiplicand) { - var m = new Kairos.Gnomon(multiplier); - m.multiply(multiplicand); - return m.toExpression(); + return Kairos.with(multiplier).multiply(multiplicand).toExpression(); }; /** @@ -102,11 +104,9 @@ * @returns {String} */ Kairos.divide = function (dividend, divisor) { - var d = new Kairos.Gnomon(dividend); - d.divide(divisor); - return d.toExpression(); + return Kairos.with(dividend).divide(divisor).toExpression(); }; - + /** * Returns a fraction of the current time * @@ -121,13 +121,9 @@ if (numerator > denominator) { throw new Error('Improper fraction'); } - - var gnomon = new Kairos.Gnomon(time); - gnomon.multiply(numerator); - gnomon.divide(denominator); - return gnomon.toExpression(); + return Kairos.with(time).multiply(numerator).divide(denominator).toExpression(); }; - + /** * Returns a time expression representing the time between starting time and ending time * @@ -143,9 +139,7 @@ if (st.compareTo(en) > 0) { throw new Error('Starting time must be bigger than ending time'); } - - en.minus(st); - return en.toExpression(); + return en.minus(st).toExpression(); }; /** @@ -157,8 +151,7 @@ * @returns {Number} */ Kairos.toMilliseconds = function (expression) { - var gnomon = new Kairos.Gnomon(expression); - return gnomon.toMilliseconds(); + return Kairos.with(expression).toMilliseconds(); }; /** @@ -170,8 +163,7 @@ * @returns {Number} */ Kairos.toSeconds = function (expression) { - var gnomon = new Kairos.Gnomon(expression); - return gnomon.toSeconds(); + return Kairos.with(expression).toSeconds(); }; /** @@ -183,8 +175,7 @@ * @returns {Number} */ Kairos.toMinutes = function (expression) { - var gnomon = new Kairos.Gnomon(expression); - return gnomon.toMinutes(); + return Kairos.with(expression).toMinutes(); }; /** @@ -196,10 +187,9 @@ * @returns {Number} */ Kairos.toHours = function (expression) { - var gnomon = new Kairos.Gnomon(expression); - return gnomon.toHours(); + return Kairos.with(expression).toHours(); }; - + /** * Compares first time with second time and returns -1, 0 or 1 if first value * is smaller, equals or bigger than second value @@ -211,11 +201,9 @@ * @returns {Number} */ Kairos.compare = function (time1, time2) { - var a = new Kairos.Gnomon(time1); - var b = new Kairos.Gnomon(time2); - return a.compareTo(b); + return Kairos.with(time1).compareTo(time2); }; - + /** * Returns the minimum value from the given values * @@ -228,7 +216,7 @@ if (!(values instanceof Array)) { values = Array.prototype.slice.call(arguments); } - + var min = values.reduce(function (previous, current) { if (!(previous instanceof Kairos.Gnomon)) { previous = new Kairos.Gnomon(previous ? previous : 0); @@ -241,7 +229,7 @@ return (min instanceof Kairos.Gnomon) ? min.toExpression() : new Kairos.Gnomon(min).toExpression(); }; - + /** * Returns the maximum value from the given values * @@ -254,7 +242,7 @@ if (!(values instanceof Array)) { values = Array.prototype.slice.call(arguments); } - + var max = values.reduce(function (previous, current) { if (!(previous instanceof Kairos.Gnomon)) { previous = new Kairos.Gnomon(previous ? previous : 0); @@ -264,10 +252,10 @@ } return ( previous.toMilliseconds() > current.toMilliseconds() ? previous : current ); }); - + return (max instanceof Kairos.Gnomon) ? max.toExpression() : new Kairos.Gnomon(max).toExpression(); }; - + // Node.js if (typeof module === 'object' && module.exports) { //=include /engine/Gnomon.js @@ -283,7 +271,7 @@ else { root.Kairos = Kairos; } - + // Polyfill Math.trunc = Math.trunc || function (x) { return x < 0 ? Math.ceil(x) : Math.floor(x); diff --git a/test/browser/kairos.js b/test/browser/kairos.js index 0f548e1..adc12fa 100644 --- a/test/browser/kairos.js +++ b/test/browser/kairos.js @@ -160,4 +160,15 @@ describe('Kairos', function () { assert.equal(Kairos.validateExpression('10:01:00:000'), true); done(); }); + + it('should create an instance of Kairos.Gnomon', function (done) { + var a = Kairos.with(); + var b = Kairos.with('01:00'); + var c = Kairos.with(123); + + assert.ok(a instanceof Kairos.Gnomon); + assert.ok(b instanceof Kairos.Gnomon); + assert.ok(c instanceof Kairos.Gnomon); + done(); + }); });