Skip to content

Commit

Permalink
created function Karios.with / removed code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigogs committed Jan 11, 2016
1 parent 8c50db6 commit 0a5fc1f
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 212 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
90 changes: 38 additions & 52 deletions build/kairos-debug.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Kairos.js - A non date-based time calculator
* @author Rodrigo Gomes da Silva <[email protected]>
* @version v1.0.0
* @version v1.1.0
* @link https://github.com/kairos
* @license BSD-2-Clause
*/
Expand Down Expand Up @@ -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
*
Expand All @@ -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();
};

/**
Expand All @@ -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();
};

/**
Expand All @@ -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();
};

/**
Expand All @@ -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
*
Expand All @@ -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
*
Expand All @@ -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();
};

/**
Expand All @@ -164,8 +158,7 @@
* @returns {Number}
*/
Kairos.toMilliseconds = function (expression) {
var gnomon = new Kairos.Gnomon(expression);
return gnomon.toMilliseconds();
return Kairos.with(expression).toMilliseconds();
};

/**
Expand All @@ -177,8 +170,7 @@
* @returns {Number}
*/
Kairos.toSeconds = function (expression) {
var gnomon = new Kairos.Gnomon(expression);
return gnomon.toSeconds();
return Kairos.with(expression).toSeconds();
};

/**
Expand All @@ -190,8 +182,7 @@
* @returns {Number}
*/
Kairos.toMinutes = function (expression) {
var gnomon = new Kairos.Gnomon(expression);
return gnomon.toMinutes();
return Kairos.with(expression).toMinutes();
};

/**
Expand All @@ -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
Expand All @@ -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
*
Expand All @@ -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);
Expand All @@ -248,7 +236,7 @@

return (min instanceof Kairos.Gnomon) ? min.toExpression() : new Kairos.Gnomon(min).toExpression();
};

/**
* Returns the maximum value from the given values
*
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -290,7 +278,7 @@
else {
root.Kairos = Kairos;
}

// Polyfill
Math.trunc = Math.trunc || function (x) {
return x < 0 ? Math.ceil(x) : Math.floor(x);
Expand Down Expand Up @@ -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;
};
Expand All @@ -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;
};
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions build/kairos-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0a5fc1f

Please sign in to comment.