Take into account year to isThisMonth

This commit is contained in:
Chocobozzz
2026-07-13 14:01:17 +02:00
parent 0f76b76292
commit d47fefe376
2 changed files with 114 additions and 5 deletions
+3 -3
View File
@@ -13,7 +13,7 @@ export function isYesterday (d: Date) {
export function isThisWeek (d: Date) {
const minDateOfThisWeek = new Date()
minDateOfThisWeek.setHours(0, 0, 0)
minDateOfThisWeek.setHours(0, 0, 0, 0)
// getDay() -> Sunday - Saturday : 0 - 6
// We want to start our week on Monday
@@ -26,9 +26,9 @@ export function isThisWeek (d: Date) {
}
export function isThisMonth (d: Date) {
const thisMonth = new Date().getMonth()
const now = new Date()
return d.getMonth() === thisMonth
return d.getMonth() === now.getMonth() && d.getFullYear() === now.getFullYear()
}
export function isLastMonth (d: Date) {
+111 -2
View File
@@ -1,6 +1,116 @@
import { millisecondsToTime, millisecondsToVttTime, secondsToTime } from '@peertube/peertube-core-utils'
/* oxlint-disable @typescript-eslint/no-unused-expressions */
import {
isLastMonth,
isLastWeek,
isThisMonth,
isThisWeek,
isToday,
isYesterday,
millisecondsToTime,
millisecondsToVttTime,
secondsToTime
} from '@peertube/peertube-core-utils'
import { expect } from 'chai'
describe('Is today', function () {
it('Returns true for the current date', function () {
expect(isToday(new Date())).to.be.true
})
it('Returns false for yesterday', function () {
const d = new Date()
d.setDate(d.getDate() - 1)
expect(isToday(d)).to.be.false
})
})
describe('Is yesterday', function () {
it('Returns true for yesterday', function () {
const d = new Date()
d.setDate(d.getDate() - 1)
expect(isYesterday(d)).to.be.true
})
it('Returns false for today', function () {
expect(isYesterday(new Date())).to.be.false
})
})
describe('Is this week', function () {
it('Returns true for today', function () {
expect(isThisWeek(new Date())).to.be.true
})
it('Returns false for 8 days ago', function () {
const d = new Date()
d.setDate(d.getDate() - 8)
expect(isThisWeek(d)).to.be.false
})
it('Returns true for the very start of today, ignoring milliseconds', function () {
const d = new Date()
d.setHours(0, 0, 0, 0)
expect(isThisWeek(d)).to.be.true
})
})
describe('Is this month', function () {
it('Returns true for the current date', function () {
expect(isThisMonth(new Date())).to.be.true
})
it('Returns false for the same month last year', function () {
const d = new Date()
d.setFullYear(d.getFullYear() - 1)
expect(isThisMonth(d)).to.be.false
})
it('Returns false for last month', function () {
const d = new Date()
d.setMonth(d.getMonth() - 1)
expect(isThisMonth(d)).to.be.false
})
})
describe('Is last month', function () {
it('Returns true for 15 days ago', function () {
const d = new Date()
d.setDate(d.getDate() - 15)
expect(isLastMonth(d)).to.be.true
})
it('Returns false for 40 days ago', function () {
const d = new Date()
d.setDate(d.getDate() - 40)
expect(isLastMonth(d)).to.be.false
})
})
describe('Is last week', function () {
it('Returns true for 3 days ago', function () {
const d = new Date()
d.setDate(d.getDate() - 3)
expect(isLastWeek(d)).to.be.true
})
it('Returns false for 10 days ago', function () {
const d = new Date()
d.setDate(d.getDate() - 10)
expect(isLastWeek(d)).to.be.false
})
})
describe('Seconds to time', function () {
it('Outputs a human readable time', function () {
expect(secondsToTime(61.1335)).to.equals('1m1s')
@@ -29,7 +139,6 @@ describe('Milliseconds to time', function () {
})
describe('Milliseconds to WebVTT time', function () {
it('Should have a valid WebVTT time', function () {
expect(millisecondsToVttTime(1000)).to.equal('00:00:01.000')
expect(millisecondsToVttTime(1001)).to.equal('00:00:01.001')