Skip to content

Commit

Permalink
Improvement performance and error control. Add more values for rounded
Browse files Browse the repository at this point in the history
  • Loading branch information
lmfresneda committed Mar 17, 2016
1 parent 968ed51 commit f302ac0
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 73 deletions.
95 changes: 59 additions & 36 deletions dist/moment-round.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,78 @@
(function() {

var moment = (typeof require !== "undefined" && require !== null) && !require.amd ? require("moment") : this.moment;
var moment = ( typeof require !== "undefined" && require !== null ) && !require.amd ? require( "moment" ) : this.moment;

moment.fn.round = function(precision, key, direction) {
if(typeof direction === 'undefined') {
direction = 'round';
}

var keys = ['Hours', 'Minutes', 'Seconds', 'Milliseconds'];
var maxValues = [24, 60, 60, 1000];

// Capitalize first letter
key = key.charAt(0).toUpperCase() + key.slice(1).toLowerCase();

// make sure key is plural
if (key.indexOf('s', key.length - 1) === -1) {
key += 's';
}
moment.fn.round = function( precision, key, direction ) {
direction = direction || 'round';
var _this = this; //cache of this
var methods = {
hours: { 'name': 'Hours', 'maxValue': 24},
minutes: { 'name': 'Minutes', 'maxValue': 60 },
seconds: { 'name': 'Seconds', 'maxValue': 60 },
milliseconds: { 'name': 'Milliseconds', 'maxValue': 1000 }
};
var keys = {
'mm': methods.milliseconds.name,
'milliseconds': methods.milliseconds.name,
'Milliseconds': methods.milliseconds.name,
's': methods.seconds.name,
'seconds': methods.seconds.name,
'Seconds': methods.seconds.name,
'm': methods.minutes.name,
'minutes': methods.minutes.name,
'Minutes': methods.minutes.name,
'H': methods.hours.name,
'h': methods.hours.name,
'hours': methods.hours.name,
'Hours': methods.hours.name
};
var value = 0;
var rounded = false;
var subRatio = 1;
var maxValue ;
for (var i in keys) {
var k = keys[i];
if (k === key) {
value = this._d['get' + key]();
maxValue = maxValues[i];

// make sure key is plural
if ( key.length > 1 && key !== 'mm' && key.slice( -1 ) !== 's' ) {
key += 's';
}
key = keys[ key ].toLowerCase();

//control
if( !methods[ key ] ){
throw new Error( 'The value to round is not valid. Possibles ["hours", "minutes", "seconds", "milliseconds"]' );
}

var get = 'get' + methods[ key ].name;
var set = 'set' + methods[ key ].name;

for( var k in methods ){
if ( k === key ) {
value = _this._d[ get ]();
maxValue = methods[ k ].maxValue;
rounded = true;
} else if(rounded) {
subRatio *= maxValues[i];
value += this._d['get' + k]() / subRatio;
this._d['set' + k](0);
} else if( rounded ) {
subRatio *= methods[ k ].maxValue;
value += _this._d[ 'get' + methods[ k ].name ]() / subRatio;
_this._d[ 'set' + methods[ k ].name ](0);
}
};
}

value = Math[direction](value / precision) * precision;
value = Math.min(value, maxValue);
this._d['set' + key](value);
value = Math[ direction ]( value / precision ) * precision;
value = Math.min( value, maxValue );
_this._d[ set ]( value );

return this;
return _this;
}

moment.fn.ceil = function(precision, key) {
return this.round(precision, key, 'ceil');
moment.fn.ceil = function( precision, key ) {
return this.round( precision, key, 'ceil' );
}

moment.fn.floor = function(precision, key) {
return this.round(precision, key, 'floor');
moment.fn.floor = function( precision, key ) {
return this.round( precision, key, 'floor' );
}

if ((typeof module !== "undefined" && module !== null ? module.exports : void 0) != null) {
if ( ( typeof module !== "undefined" && module !== null ? module.exports : void 0 ) != null ) {
module.exports = moment;
}
}).call(this);
}).call( this );
2 changes: 1 addition & 1 deletion dist/moment-round.min.js

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

95 changes: 59 additions & 36 deletions src/moment-round.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,78 @@
(function() {

var moment = (typeof require !== "undefined" && require !== null) && !require.amd ? require("moment") : this.moment;
var moment = ( typeof require !== "undefined" && require !== null ) && !require.amd ? require( "moment" ) : this.moment;

moment.fn.round = function(precision, key, direction) {
if(typeof direction === 'undefined') {
direction = 'round';
}

var keys = ['Hours', 'Minutes', 'Seconds', 'Milliseconds'];
var maxValues = [24, 60, 60, 1000];

// Capitalize first letter
key = key.charAt(0).toUpperCase() + key.slice(1).toLowerCase();

// make sure key is plural
if (key.indexOf('s', key.length - 1) === -1) {
key += 's';
}
moment.fn.round = function( precision, key, direction ) {
direction = direction || 'round';
var _this = this; //cache of this
var methods = {
hours: { 'name': 'Hours', 'maxValue': 24},
minutes: { 'name': 'Minutes', 'maxValue': 60 },
seconds: { 'name': 'Seconds', 'maxValue': 60 },
milliseconds: { 'name': 'Milliseconds', 'maxValue': 1000 }
};
var keys = {
'mm': methods.milliseconds.name,
'milliseconds': methods.milliseconds.name,
'Milliseconds': methods.milliseconds.name,
's': methods.seconds.name,
'seconds': methods.seconds.name,
'Seconds': methods.seconds.name,
'm': methods.minutes.name,
'minutes': methods.minutes.name,
'Minutes': methods.minutes.name,
'H': methods.hours.name,
'h': methods.hours.name,
'hours': methods.hours.name,
'Hours': methods.hours.name
};
var value = 0;
var rounded = false;
var subRatio = 1;
var maxValue ;
for (var i in keys) {
var k = keys[i];
if (k === key && typeof this._d['get' + key] === "function") {
value = this._d['get' + key]();
maxValue = maxValues[i];

// make sure key is plural
if ( key.length > 1 && key !== 'mm' && key.slice( -1 ) !== 's' ) {
key += 's';
}
key = keys[ key ].toLowerCase();

//control
if( !methods[ key ] ){
throw new Error( 'The value to round is not valid. Possibles ["hours", "minutes", "seconds", "milliseconds"]' );
}

var get = 'get' + methods[ key ].name;
var set = 'set' + methods[ key ].name;

for( var k in methods ){
if ( k === key ) {
value = _this._d[ get ]();
maxValue = methods[ k ].maxValue;
rounded = true;
} else if(rounded && typeof this._d['get' + k] === "function") {
subRatio *= maxValues[i];
value += this._d['get' + k]() / subRatio;
this._d['set' + k](0);
} else if( rounded ) {
subRatio *= methods[ k ].maxValue;
value += _this._d[ 'get' + methods[ k ].name ]() / subRatio;
_this._d[ 'set' + methods[ k ].name ](0);
}
};
}

value = Math[direction](value / precision) * precision;
value = Math.min(value, maxValue);
this._d['set' + key](value);
value = Math[ direction ]( value / precision ) * precision;
value = Math.min( value, maxValue );
_this._d[ set ]( value );

return this;
return _this;
}

moment.fn.ceil = function(precision, key) {
return this.round(precision, key, 'ceil');
moment.fn.ceil = function( precision, key ) {
return this.round( precision, key, 'ceil' );
}

moment.fn.floor = function(precision, key) {
return this.round(precision, key, 'floor');
moment.fn.floor = function( precision, key ) {
return this.round( precision, key, 'floor' );
}

if ((typeof module !== "undefined" && module !== null ? module.exports : void 0) != null) {
if ( ( typeof module !== "undefined" && module !== null ? module.exports : void 0 ) != null ) {
module.exports = moment;
}
}).call(this);
}).call( this );

0 comments on commit f302ac0

Please sign in to comment.