forked from nightscout/minimed-connect-to-nightscout
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose and test makeRecencyFilter so cgm-remote-monitor can use it
- Loading branch information
Showing
4 changed files
with
58 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* jshint node: true */ | ||
"use strict"; | ||
|
||
// Returns a stateful filter which remembers the time of the last-seen entry to | ||
// prevent uploading duplicates. | ||
function makeRecencyFilter(timeFn) { | ||
var lastTime = 0; | ||
|
||
return function(items) { | ||
var out = []; | ||
items.forEach(function(item) { | ||
if (timeFn(item) > lastTime) { | ||
out.push(item); | ||
} | ||
}); | ||
out.forEach(function(item) { | ||
lastTime = Math.max(lastTime, timeFn(item)); | ||
}); | ||
|
||
return out; | ||
}; | ||
} | ||
|
||
module.exports.makeRecencyFilter = makeRecencyFilter; |
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
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* jshint node: true */ | ||
/* globals describe, it */ | ||
"use strict"; | ||
|
||
var _ = require('lodash'), | ||
expect = require('expect.js'); | ||
|
||
var makeRecencyFilter = require('../filter.js').makeRecencyFilter; | ||
|
||
describe('makeRecencyFilter()', function() { | ||
it('should return a stateful filter which discards items older than the most recent one seen', function() { | ||
function sgv(date) { | ||
return {type: 'sgv', someDateKey: date}; | ||
} | ||
|
||
var filter = makeRecencyFilter(function(item) { | ||
return item['someDateKey']; | ||
}); | ||
|
||
expect(filter([2, 3, 4].map(sgv))).to.have.length(3); | ||
|
||
expect(filter([2, 3, 4].map(sgv))).to.have.length(0); | ||
|
||
var filtered = filter([2, 3, 4, 8, 6, 7, 5].map(sgv)); | ||
expect(filtered).to.have.length(4); | ||
[5, 6, 7, 8].forEach(function(val) { | ||
expect(_.pluck(filtered, 'someDateKey')).to.contain(val); | ||
}); | ||
}); | ||
}); |