diff --git a/index.js b/index.js index 1c7f9f2..2a7df55 100644 --- a/index.js +++ b/index.js @@ -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 } diff --git a/test/runout.js b/test/runout.js index 8f1456e..450262d 100644 --- a/test/runout.js +++ b/test/runout.js @@ -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(), @@ -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: [] }) }) @@ -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: [] }) }) @@ -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 +}