Skip to content

Commit

Permalink
Mejoramos TimeInterval para hacerlo un objeto independiente
Browse files Browse the repository at this point in the history
  • Loading branch information
emilioplatzer committed Jul 15, 2017
1 parent 31bbac4 commit 76c55e5
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 21 deletions.
81 changes: 64 additions & 17 deletions best-globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ function addDateMethods(dt) {
if(!objectOrTimeInterval.timeInterval){
objectOrTimeInterval=bestGlobals.timeInterval(objectOrTimeInterval);
}
dt.setTime(dt.getTime()+objectOrTimeInterval.timeInterval.ms);
return bestGlobals.date(new Date(dt.getTime()+objectOrTimeInterval.timeInterval.ms));
};
return dt;
}
Expand Down Expand Up @@ -244,11 +244,19 @@ bestGlobals.date.parseFormat = function parseFormat(dateStr) {
return { y:parseInt(match[2],10), m:parseInt(match[4],10), d:parseInt(match[7],10) };
};

bestGlobals.date.iso = function iso(dateStr) {
bestGlobals.date.iso = function iso(dateStr) {
var parsed=bestGlobals.date.parseFormat(dateStr);
return bestGlobals.date.ymd(parsed.y, parsed.m, parsed.d);
};

/*
bestGlobals.date.ms = function ms(ms){
console.log('xxxxxxxxxxxxxxxx ', ms);
console.log('xxxxxxxxxxxxxxxx ', new Date(ms));
return bestGlobals.date(new Date(ms));
};
*/

bestGlobals.date.array = function array(arr) {
if(arr.length !== 3) { throw new Error('invalid date array'); }
return bestGlobals.date.ymd(arr[0], arr[1], arr[2]);
Expand Down Expand Up @@ -309,43 +317,82 @@ bestGlobals.datetime.array = function array(arr) {
return bestGlobals.datetime.ymdHmsM(arr[0], arr[1], arr[2], arr[3]||0, arr[4]||0, arr[5]||0, arr[6]||0);
};

bestGlobals.timeInterval = function timeInterval(timePack) {
var d = new Date(0,0,0,0,0,0,0);
bestGlobals.TimeInterval = function TimeInterval(timePack){
if(typeof timePack === 'number'){
timePack={ms:timePack};
console.log('|-----------------------------|');
console.log('| DEPRECATED timeInterval(ms) |');
console.log('|-----------------------------|');
}
var timeValues={
ms :1,
sec :1000*60,
min :1000*60,
hours:1000*60*60,
days :1000*60*60*24,
ms :1,
seconds:1000,
minutes:1000*60,
hours :1000*60*60,
days :1000*60*60*24,
}
var time=0;
for(var attr in timePack){
time+=timePack[attr]*timeValues[attr];
}
d.setTime(time);
d.timeInterval={ms:time};
d.toHms = function toHms() {
this.timeInterval={ms:time};
this.toHms = function toHms(omitSeconds, withDays, omitLeftCeros) {
var leftCero = omitLeftCeros?'':'0';
var tm = this.timeInterval.ms;
var prefix = (tm<0?'-':'');
var tdiff = [];
var tm = this.getTime();
var x = Math.abs(tm);
x /= 1000;
var s = Math.floor(x % 60);
x /= 60;
var m = Math.floor(x % 60);
x /= 60;
var h = Math.floor(x);
tdiff.push((tm<0?'-':'')+(h<10?'0':'')+h);
if(withDays){
h = Math.floor(x % 24);
x /= 24;
var d = Math.floor(x);
if(d){
prefix+=(Math.abs(d)<10?leftCero:'')+d+'D';
if(!h && !m && !s){
return prefix;
}
prefix+=' ';
}
}
tdiff.push((h<10?leftCero:'')+h);
tdiff.push((m<10?'0':'')+m);
tdiff.push((s<10?'0':'')+s);
return tdiff.join(':');
if(!omitSeconds){
tdiff.push((s<10?'0':'')+s);
}
var hourPart=tdiff.join(':');
return prefix+hourPart;
};
return d;
this.toHm = function toHm() {
return this.toHms(true);
}
this.toPlainString = function toPlainString(){
return this.toHms(false,true,true);
}
this.add = function add(objectOrTimeInterval, factor){
if(!objectOrTimeInterval.timeInterval){
objectOrTimeInterval=bestGlobals.timeInterval(objectOrTimeInterval);
}
return new bestGlobals.TimeInterval({ms:this.timeInterval.ms+objectOrTimeInterval.timeInterval.ms*(factor||1)});
};
this.sub = function sub(objectOrTimeInterval){
return this.add(objectOrTimeInterval,-1);
}
this.getAllHours = function getAllHours(){
return Math.floor(this.timeInterval.ms/(1000*60*60));
}
this.sameValue = function sameValue(otherInterval){
return this.timeInterval.ms == otherInterval.timeInterval.ms;
}
}

bestGlobals.timeInterval = function timeInterval(timePack) {
return new bestGlobals.TimeInterval(timePack);
};

bestGlobals.functionName = function functionName(fun) {
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "best-globals",
"description": "common global function and constants - i.e. coalesce",
"version": "0.9.3",
"version": "0.9.4",
"author": "Codenautas <codenautas@googlegroups.com>",
"license": "MIT",
"repository": "codenautas/best-globals",
Expand All @@ -18,7 +18,8 @@
"mocha": "~3.4.2",
"sinon": "~2.3.8",

"audit-copy": "~0.0.5"
"audit-copy": "~0.0.5",
"discrepances": "~0.1.4"
},
"main": "best-globals.js",
"engines": {
Expand Down
39 changes: 37 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var sinon = require('sinon');
var assert = require('assert');
var bestGlobals = require('..');
var auditCopy = require('audit-copy');
var discrepances = require('discrepances');

describe('best-globals', function(){
describe('coalesce', function(){
Expand Down Expand Up @@ -248,6 +249,12 @@ describe("date", function(){
var d1 = date.iso("1916-07-09");
control(d1, indep);
});
/*
it("create date from ms", function(){
var d1 = date.ms(2*24*60*60*1000);
control(d1, date.iso("1970-01-03"));
});
*/
it("create date from string without trainling zeros", function(){
var d1 = date.iso("272-2-27");
control(d1, nateConstantino);
Expand Down Expand Up @@ -285,11 +292,39 @@ describe("date", function(){
it("create timeInterval from integer and format it", function(){
expect(timeInterval({ms:new Date(1916,7,9,10,32,0)-new Date(1916,7,09,10,32,11)}).toHms()).eql('-00:00:11');
expect(timeInterval({ms:new Date(1916,7,9,11,0,0)-new Date(1916,7,7,11,0,0) }).toHms()).eql('48:00:00');
expect(timeInterval({ms:new Date(1916,7,9,11,0,0)-new Date(1916,7,7,11,0,0) }).toPlainString()).eql('2D');
expect(timeInterval({ms:new Date(1916,7,9,12,0,0)-new Date(1916,7,7,11,0,0) }).toPlainString()).eql('2D 1:00:00');
expect(timeInterval({ms:new Date(1916,7,7,12,0,0)-new Date(1916,7,7,11,0,0) }).toPlainString()).eql('1:00:00');
expect(timeInterval({ms:new Date(1916,7,8,11,0,0)-new Date(1916,7,7,12,0,0) }).toHms()).eql('23:00:00');
expect(timeInterval({ms:new Date(1916,7,7,11,0,0)-new Date(1916,7,9,11,0,0) }).toHms()).eql('-48:00:00');
expect(timeInterval({ms:new Date(1916,7,7,11,0,0)-new Date(1916,7,9,10,32,11)}).toHms()).eql('-47:32:11');
expect(timeInterval({ms:new Date(1916,7,7,11,0,0)-new Date(1916,7,9,10,32,11)}).toHm()).eql('-47:32');
//expect(timeInterval(new Date(1916,7,7,11, 0,0)-new Date(1916,7,09,10,32,11)).toHms()).eql('48:27:49');
});
it("accept any interval", function(){
discrepances.showAndThrow(
timeInterval({ms:1, seconds:2, minutes:3, hours:4, days:5}),
timeInterval({ms:1+1000*(2+60*(3+60*(4+24*5)))})
);
/*
expect(timeInterval({ms:1, seconds:2, minutes:3, hours:4, days:5}))
.to.eql(timeInterval({ms:1+1000*(2+60*(3+60*(4+24*5)))}));
*/
});
it("get allhours from interval", function(){
expect(timeInterval({hours:44, minutes:3}).getAllHours()).to.eql(44);
expect(timeInterval({hours:-44, minutes:3}).getAllHours()).to.eql(-44);
});
it("substact intervals", function(){
discrepances.showAndThrow(
timeInterval({ms:65000}).sub(timeInterval({seconds:60})),
timeInterval({seconds:5})
);
discrepances.showAndThrow(
timeInterval({ms:65000}).sub({seconds:60}),
timeInterval({seconds:5})
);
});
[ ["1997-12"], [1997,12], [1997,0,1], [[1997,0,1]], [(new Date(1916,7-1,9)).getTime()]].forEach(function(invalidParams){
it("rejects invalid date: "+JSON.stringify(invalidParams), function(){
expect(function(){
Expand Down Expand Up @@ -481,8 +516,8 @@ describe("date", function(){
fixtures.forEach(function(fixture){
it("fixture "+JSON.stringify(fixture), function(){
var d = bestGlobals.date.iso(fixture.d);
d.add({days:fixture.days});
expect(d).to.eql(bestGlobals.date.iso(fixture.res));
var obtained = d.add({days:fixture.days});
expect(obtained).to.eql(bestGlobals.date.iso(fixture.res));
});
});
});
Expand Down

0 comments on commit 76c55e5

Please sign in to comment.