plmercereau/platyplus

View on GitHub
src/utils/index.js

Summary

Maintainability
A
2 hrs
Test Coverage
Function `timeDifference` has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
function timeDifference (current, previous) {
const milliSecondsPerMinute = 60 * 1000
const milliSecondsPerHour = milliSecondsPerMinute * 60
const milliSecondsPerDay = milliSecondsPerHour * 24
const milliSecondsPerMonth = milliSecondsPerDay * 30
const milliSecondsPerYear = milliSecondsPerDay * 365
 
const elapsed = current - previous
 
if (elapsed < milliSecondsPerMinute / 3) {
return 'just now'
}
 
if (elapsed < milliSecondsPerMinute) {
return 'less than 1 min ago'
} else if (elapsed < milliSecondsPerHour) {
return Math.round(elapsed / milliSecondsPerMinute) + ' min ago'
} else if (elapsed < milliSecondsPerDay) {
return Math.round(elapsed / milliSecondsPerHour) + ' h ago'
} else if (elapsed < milliSecondsPerMonth) {
Avoid too many `return` statements within this function.
return Math.round(elapsed / milliSecondsPerDay) + ' days ago'
} else if (elapsed < milliSecondsPerYear) {
Avoid too many `return` statements within this function.
return Math.round(elapsed / milliSecondsPerMonth) + ' mo ago'
} else {
Avoid too many `return` statements within this function.
return Math.round(elapsed / milliSecondsPerYear) + ' years ago'
}
}
 
export function timeDifferenceForDate (date) {
const now = new Date().getTime()
const updated = new Date(date).getTime()
return timeDifference(now, updated)
}
 
// returns the first attribute of an object. rank allows to pick the first attribute of the first sub-object recursively
export function firstAttribute (obj, rank = 1) {
if (rank > 0) {
return firstAttribute(obj[Object.keys(obj)[0]], rank - 1)
}
return obj
}