Skip to content

Commit

Permalink
Filter dates that expire soon
Browse files Browse the repository at this point in the history
  • Loading branch information
bredele committed Apr 6, 2018
1 parent fdb6157 commit 8c9ab68
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
13 changes: 9 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@


module.exports = (arr, limit) => {
const obj = { valid: [], expired: [] }
module.exports = (arr, limit, soon = 0) => {
const obj = { valid: [], expired: [], soon: []}
limit = parse(limit)
arr.map(time => {
time = parse(time)
if (time < limit) obj.valid.push(time)
else obj.expired.push(time)
if (time < limit) {
if ((limit - soon) <= time && time < limit) {
obj.soon.push(time)
} else {
obj.valid.push(time)
}
} else obj.expired.push(time)
})
return obj
}
Expand Down
32 changes: 28 additions & 4 deletions test/runout.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const test = require('tape')
const runout = require('..')


test('should sort dates with given expiration limit', assert => {
test('should extract valid and expired dates from a given expiration limit', assert => {
assert.plan(1)
const obj = runout([
new Date('01-01-2000').getTime(),
Expand All @@ -15,7 +15,8 @@ test('should sort dates with given expiration limit', assert => {
], new Date('01-01-2101').getTime())
assert.deepEqual(obj, {
valid: [946710000000, 4102470000000],
expired: [10413817200000]
expired: [10413817200000],
soon: []
})
})

Expand All @@ -29,7 +30,8 @@ test('should convert dates in ms', assert => {
], new Date('01-01-2101'))
assert.deepEqual(obj, {
valid: [946710000000, 4102470000000],
expired: [10413817200000]
expired: [10413817200000],
soon: []
})
})

Expand All @@ -43,6 +45,28 @@ test('should convert date strings into ms and sort them', assert => {
], '01-01-2101')
assert.deepEqual(obj, {
valid: [946710000000, 4102470000000],
expired: [10413817200000]
expired: [10413817200000],
soon: []
})
})


test('should extract dates that expire soon', assert => {
assert.plan(1)
const obj = runout([
new Date('01-01-2000'),
new Date('01-01-2100'),
new Date('12-06-2100'),
new Date('01-01-2300'),
], new Date('01-01-2101'), oneyear())
assert.deepEqual(obj, {
valid: [946710000000],
expired: [10413817200000],
soon: [4102470000000, 4131759600000]
})
})


function oneyear () {
return 1000 * 60 * 60 * 24 * 365
}

0 comments on commit 8c9ab68

Please sign in to comment.