-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created function Karios.with / removed code smells
- Loading branch information
Showing
9 changed files
with
167 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ | ||
|
@@ -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; | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.