grandfatherson
Advanced tools
Comparing version 1.1.0 to 1.1.1
## [1.1.1] - 2016-10-24 | ||
### Bug Fixes | ||
* toKeep array could have contained duplicate entries if dates matched multiple filters. | ||
## [1.1.0] - 2016-10-18 | ||
@@ -3,0 +9,0 @@ |
70
index.js
@@ -11,16 +11,16 @@ | ||
_.defaults(options, { | ||
years : 0, | ||
months : 0, | ||
weeks : 0, | ||
days : 0, | ||
minutes : 0, | ||
hours : 0, | ||
seconds : 0, | ||
firstweekday : SATURDAY, | ||
now : undefined, // Default is defined in the filter function. No need to duplicate it here. | ||
}); | ||
_.defaults(options, { | ||
years : 0, | ||
months : 0, | ||
weeks : 0, | ||
days : 0, | ||
minutes : 0, | ||
hours : 0, | ||
seconds : 0, | ||
firstweekday : SATURDAY, | ||
now : undefined, // Default is defined in the filter function. No need to duplicate it here. | ||
}); | ||
var mightHaveDupes = _.flatten([ | ||
var mightHaveDupes = _.flatten([ | ||
filters.Years.filter(datetimes, {number:options.years, now: options.now}), | ||
@@ -35,23 +35,29 @@ filters.Months.filter(datetimes, {number:options.months, now: options.now}), | ||
var toKeep = []; | ||
var toKeep = []; | ||
// You can't have dupes without at least 2 | ||
if (mightHaveDupes.length < 2) { | ||
toKeep = mightHaveDupes; | ||
} | ||
// Remove duplicate dates, if any. | ||
else { | ||
var toKeep = [mightHaveDupes[0]]; | ||
// You can't have dupes without at least 2 | ||
if (mightHaveDupes.length < 2) { | ||
toKeep = mightHaveDupes; | ||
} | ||
// Remove duplicate dates, if any. | ||
else { | ||
mightHaveDupes = mightHaveDupes.sort(function (a ,b) { | ||
if (a.isBefore(b)) | ||
return -1; | ||
if (a.isAfter(b)) | ||
return 1; | ||
// Starting with the 2nd result discard results if they are the same as the previous one. | ||
// (Array should already be sorted from oldest->newest | ||
for (var i = 1; i < mightHaveDupes.length; i++) { | ||
if (! mightHaveDupes[i].isSame(mightHaveDupes[i-1])) { | ||
toKeep.push(mightHaveDupes[i]) | ||
} | ||
} | ||
} | ||
return toKeep; | ||
return 0; | ||
}); | ||
// Now remove dupes from sorted array | ||
for (var i = 0; i < mightHaveDupes.length; i++) { | ||
if (! mightHaveDupes[i].isSame(mightHaveDupes[i-1])) { | ||
toKeep.push(mightHaveDupes[i]) | ||
} | ||
} | ||
} | ||
return toKeep; | ||
} | ||
@@ -61,3 +67,3 @@ | ||
function toDelete(datetimes, options) { | ||
// We can't just a function like _.difference, because moment objects can't be compared that simply | ||
@@ -64,0 +70,0 @@ // and the incoming values might not already be moment objects |
{ | ||
"name": "grandfatherson", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"keywords": ["backup", "rotation", "algorithm", "snapshots" ], | ||
@@ -5,0 +5,0 @@ "description": "Grandfather Father Son algorithm, useful for backup rotation", |
@@ -41,3 +41,3 @@ "use strict" | ||
// Simulate it being the last day of the year | ||
var survivors = toKeep(allDaysin1999, {days:7, weeks:4, months:3, firstweekday:SATURDAY, now:lastDayof1999}) | ||
var survivors = toKeep(allDaysin1999, {days:8, weeks:4, months:3, firstweekday:SATURDAY, now:lastDayof1999}) | ||
@@ -51,2 +51,3 @@ var expectedDates = [ | ||
moment.utc('1999-12-18'), | ||
moment.utc('1999-12-24'), | ||
moment.utc('1999-12-25'), | ||
@@ -61,5 +62,5 @@ moment.utc('1999-12-26'), | ||
it("should correctly handle {days:7, weeks:4, months:3, firstweekday:SATURDAY, now:lastDayof1999}", function () { | ||
expect(survivors.map(_toString)).to.eql(expectedDates.map(_toString)); | ||
}); | ||
it("should correctly handle {days:8, weeks:4, months:3, firstweekday:SATURDAY, now:lastDayof1999}", function () { | ||
expect(survivors.map(_toString)).to.eql(expectedDates.map(_toString)); | ||
}); | ||
}); | ||
@@ -69,5 +70,5 @@ | ||
var toDelete = gfs.toDelete; | ||
// Simulate it being the last day of the year | ||
var condemned = toDelete(allDaysin1999, {days:7, weeks:4, months:3, firstweekday:SATURDAY, now:lastDayof1999}) | ||
var condemned = toDelete(allDaysin1999, {days:8, weeks:4, months:3, firstweekday:SATURDAY, now:lastDayof1999}) | ||
@@ -77,4 +78,5 @@ | ||
expect(allDaysin1999).to.have.length(365); | ||
// We see above that 13 records were kept and we know there are 365 days in the year | ||
expect(condemned.length).to.equal(365-13); | ||
// We see above that 14 records were kept and we know there are 365 days in the year | ||
expect(condemned.length).to.equal(365-14); | ||
}); | ||
@@ -81,0 +83,0 @@ }); |
45191
839