Skip to content

Commit

Permalink
Sort and parse dates;
Browse files Browse the repository at this point in the history
  • Loading branch information
bredele committed Apr 6, 2018
1 parent 0f61754 commit fdb6157
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
17 changes: 17 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@


module.exports = (arr, limit) => {
const obj = { valid: [], expired: [] }
limit = parse(limit)
arr.map(time => {
time = parse(time)
if (time < limit) obj.valid.push(time)
else obj.expired.push(time)
})
return obj
}


function parse (time) {
return typeof time === 'number' ? time : Date.parse(time)
}
32 changes: 30 additions & 2 deletions test/runout.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,40 @@ const runout = require('..')
test('should sort dates with given expiration limit', assert => {
assert.plan(1)
const obj = runout([
new Date('01-01-2200').getTime(),
new Date('01-01-2000').getTime(),
new Date('01-01-2100').getTime(),
new Date('01-01-2300').getTime(),
], new Date('01-01-2101').getTime())
assert.deepEqual(obj, {
valid: [7258143600000, 4102470000000],
valid: [946710000000, 4102470000000],
expired: [10413817200000]
})
})


test('should convert dates in ms', assert => {
assert.plan(1)
const obj = runout([
new Date('01-01-2000'),
new Date('01-01-2100'),
new Date('01-01-2300'),
], new Date('01-01-2101'))
assert.deepEqual(obj, {
valid: [946710000000, 4102470000000],
expired: [10413817200000]
})
})


test('should convert date strings into ms and sort them', assert => {
assert.plan(1)
const obj = runout([
new Date('01-01-2000'),
'01-01-2100',
new Date('01-01-2300'),
], '01-01-2101')
assert.deepEqual(obj, {
valid: [946710000000, 4102470000000],
expired: [10413817200000]
})
})

0 comments on commit fdb6157

Please sign in to comment.