Skip to content

Commit

Permalink
Add getBroadcastMonth
Browse files Browse the repository at this point in the history
  • Loading branch information
velocityzen committed Mar 6, 2024
1 parent 117ba7c commit 948fe52
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 48 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ returns broadcast year for a given date

returns broadcast quarter for a given date

- **getBroadcastMonth(date: DateTime): null | number**

return broadcast month (1-12) for a given date

- **getBroadcastQuarterWeek(date: DateTime): null | [number, number]**

returns `[quarter, week]` numbers for a given date
Expand Down
3 changes: 2 additions & 1 deletion src/calendar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { DateTime } from "luxon";
import { getBroadcastYear, getBroadcastQuarter } from "./yearQuarter";
import {
getBroadcastYearInterval,
getBroadcastQuarterInterval,
Expand All @@ -8,6 +7,8 @@ import {
} from "./interval";
import { getBroadcastWeek } from "./week";
import { getBroadcastWeekKey } from "./weekKey";
import { getBroadcastYear } from "./year";
import { getBroadcastQuarter } from "./quarter";

export function toCalendarDateTime<IsValid extends boolean>(
broadcast: DateTime<IsValid>,
Expand Down
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
export { DateTime, Interval } from "luxon";
export * from "./types";
export * from "./calendar";
export * from "./format";
export * from "./parse";
export * from "./interval";
export * from "./month";
export * from "./parse";
export * from "./quarter";
export * from "./types";
export * from "./week";
export * from "./weekKey";
export * from "./year";
export * from "./yearQuarter";
export * from "./calendar";
16 changes: 16 additions & 0 deletions src/month.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { DateTime } from "luxon";

import { IfValid, isValid } from "./helpers";

/**
return broadcast month (1-12) for a given date
**/
export function getBroadcastMonth<IsValid extends boolean>(
date: DateTime<IsValid>,
): IfValid<IsValid, number> {
if (!isValid(date)) {
return null as IfValid<IsValid, number>;
}

return date.endOf("week").month as unknown as IfValid<IsValid, number>;
}
38 changes: 38 additions & 0 deletions src/quarter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { DateTime } from "luxon";

import { IfValid, isValid } from "./helpers";
import { getBroadcastWeek } from "./week";

/**
* returns broadcast quarter for a given date
**/
export function getBroadcastQuarter<IsValid extends boolean>(
date: DateTime<IsValid>,
): IfValid<IsValid, number> {
const week = getBroadcastWeek(date);
if (!isValid(date) || week === null) {
return null as IfValid<IsValid, number>;
}

const quarterLength = 13;
return Math.min(Math.ceil(week / quarterLength), 4) as IfValid<
IsValid,
number
>;
}

/**
* returns broadcast [quarter, week] for a given date
**/
export function getBroadcastQuarterWeek<IsValid extends boolean>(
date: DateTime<IsValid>,
): IfValid<IsValid, [number, number]> {
const week = getBroadcastWeek(date);
if (!isValid(date) || week === null) {
return null as IfValid<IsValid, [number, number]>;
}

const quarterLength = 13;
const quarter = Math.min(Math.ceil(week / quarterLength), 4);
return [quarter, week] as IfValid<IsValid, [number, number]>;
}
3 changes: 3 additions & 0 deletions src/week.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ function isLastWeekOverflown(date: DateTime): boolean {
return yearEnd.toMillis() - date.toMillis() <= yearEndWeekDay * DAY;
}

/**
* returns broadcast week number (1-54) for a given date
**/
export function getBroadcastWeek<IsValid extends boolean>(
date: DateTime<IsValid>,
): IfValid<IsValid, number> {
Expand Down
5 changes: 4 additions & 1 deletion src/weekKey.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type { DateTime } from "luxon";
import { getBroadcastYear } from "./yearQuarter";
import { getBroadcastWeek } from "./week";
import { IfValid } from "./helpers";
import { getBroadcastYear } from "./year";

/**
* returns broadcast week key for a given date. Examples: `202103`, `202232`
**/
export function getBroadcastWeekKey<IsValid extends boolean>(
date: DateTime<IsValid>,
): IfValid<IsValid, number> {
Expand Down
19 changes: 19 additions & 0 deletions src/year.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { DateTime } from "luxon";

import { getBroadcastYearInterval } from "./interval";
import { IfValid, isValid } from "./helpers";

/**
* returns broadcast year for a given date
**/
export function getBroadcastYear<IsValid extends boolean>(
date: DateTime<IsValid>,
): IfValid<IsValid, number> {
const yearInterval = getBroadcastYearInterval(date);

if (!(yearInterval && isValid(yearInterval.end))) {
return null as IfValid<IsValid, number>;
}

return yearInterval.end.get("year") as IfValid<IsValid, number>;
}
44 changes: 2 additions & 42 deletions src/yearQuarter.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,9 @@
import { DateTime, Interval } from "luxon";

import { YearQuarter, YearQuarters } from "./types";
import { getBroadcastYearInterval } from "./interval";
import { getBroadcastWeek } from "./week";
import { IfValid, isValid } from "./helpers";

export function getBroadcastYear<IsValid extends boolean>(
date: DateTime<IsValid>,
): IfValid<IsValid, number> {
const yearInterval = getBroadcastYearInterval(date);

if (!(yearInterval && isValid(yearInterval.end))) {
return null as IfValid<IsValid, number>;
}

return yearInterval.end.get("year") as IfValid<IsValid, number>;
}

export function getBroadcastQuarter<IsValid extends boolean>(
date: DateTime<IsValid>,
): IfValid<IsValid, number> {
const week = getBroadcastWeek(date);
if (!isValid(date) || week === null) {
return null as IfValid<IsValid, number>;
}

const quarterLength = 13;
return Math.min(Math.ceil(week / quarterLength), 4) as IfValid<
IsValid,
number
>;
}

export function getBroadcastQuarterWeek<IsValid extends boolean>(
date: DateTime<IsValid>,
): IfValid<IsValid, [number, number]> {
const week = getBroadcastWeek(date);
if (!isValid(date) || week === null) {
return null as IfValid<IsValid, [number, number]>;
}

const quarterLength = 13;
const quarter = Math.min(Math.ceil(week / quarterLength), 4);
return [quarter, week] as IfValid<IsValid, [number, number]>;
}
import { getBroadcastYear } from "./year";
import { getBroadcastQuarter } from "./quarter";

export function getBroadcastYearQuarter<IsValid extends boolean>(
date: DateTime<IsValid>,
Expand Down
46 changes: 45 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
formatToISOWithoutTZ,
formatToSQLWithoutTZ,
getBroadcastMonthInterval,
getBroadcastMonth,
getBroadcastQuarter,
getBroadcastQuarterInterval,
getBroadcastQuarterIntervalFromYearQuarter,
Expand Down Expand Up @@ -58,6 +59,7 @@ const broadcastTestData = [
{
broadcastYear: 2017,
broadcastQuarter: 4,
broadcastMonth: 12,
week: ["2017-12-25 Mon", "2017-12-31 Sun"],
month: ["2017-11-27 Mon", "2017-12-31 Sun"],
quarter: ["2017-09-25 Mon", "2017-12-31 Sun"],
Expand All @@ -69,6 +71,7 @@ const broadcastTestData = [
{
broadcastYear: 2019,
broadcastQuarter: 1,
broadcastMonth: 1,
week: ["2018-12-31 Mon", "2019-01-06 Sun"],
month: ["2018-12-31 Mon", "2019-01-27 Sun"],
quarter: ["2018-12-31 Mon", "2019-03-31 Sun"],
Expand All @@ -80,6 +83,7 @@ const broadcastTestData = [
{
broadcastYear: 2019,
broadcastQuarter: 1,
broadcastMonth: 3,
week: ["2019-03-25 Mon", "2019-03-31 Sun"],
month: ["2019-02-25 Mon", "2019-03-31 Sun"],
quarter: ["2018-12-31 Mon", "2019-03-31 Sun"],
Expand All @@ -91,6 +95,7 @@ const broadcastTestData = [
{
broadcastYear: 2019,
broadcastQuarter: 2,
broadcastMonth: 4,
week: ["2019-04-01 Mon", "2019-04-07 Sun"],
month: ["2019-04-01 Mon", "2019-04-28 Sun"],
quarter: ["2019-04-01 Mon", "2019-06-30 Sun"],
Expand All @@ -102,6 +107,7 @@ const broadcastTestData = [
{
broadcastYear: 2019,
broadcastQuarter: 2,
broadcastMonth: 6,
week: ["2019-06-24 Mon", "2019-06-30 Sun"],
month: ["2019-05-27 Mon", "2019-06-30 Sun"],
quarter: ["2019-04-01 Mon", "2019-06-30 Sun"],
Expand All @@ -113,6 +119,7 @@ const broadcastTestData = [
{
broadcastYear: 2019,
broadcastQuarter: 3,
broadcastMonth: 7,
week: ["2019-07-01 Mon", "2019-07-07 Sun"],
month: ["2019-07-01 Mon", "2019-07-28 Sun"],
quarter: ["2019-07-01 Mon", "2019-09-29 Sun"],
Expand All @@ -124,6 +131,7 @@ const broadcastTestData = [
{
broadcastYear: 2019,
broadcastQuarter: 3,
broadcastMonth: 9,
week: ["2019-09-23 Mon", "2019-09-29 Sun"],
month: ["2019-08-26 Mon", "2019-09-29 Sun"],
quarter: ["2019-07-01 Mon", "2019-09-29 Sun"],
Expand All @@ -135,6 +143,7 @@ const broadcastTestData = [
{
broadcastYear: 2019,
broadcastQuarter: 4,
broadcastMonth: 10,
week: ["2019-09-30 Mon", "2019-10-06 Sun"],
month: ["2019-09-30 Mon", "2019-10-27 Sun"],
quarter: ["2019-09-30 Mon", "2019-12-29 Sun"],
Expand All @@ -146,6 +155,7 @@ const broadcastTestData = [
{
broadcastYear: 2019,
broadcastQuarter: 4,
broadcastMonth: 12,
week: ["2019-12-23 Mon", "2019-12-29 Sun"],
month: ["2019-11-25 Mon", "2019-12-29 Sun"],
quarter: ["2019-09-30 Mon", "2019-12-29 Sun"],
Expand All @@ -157,6 +167,7 @@ const broadcastTestData = [
{
broadcastYear: 2020,
broadcastQuarter: 1,
broadcastMonth: 1,
week: ["2019-12-30 Mon", "2020-01-05 Sun"],
month: ["2019-12-30 Mon", "2020-01-26 Sun"],
quarter: ["2019-12-30 Mon", "2020-03-29 Sun"],
Expand All @@ -168,6 +179,7 @@ const broadcastTestData = [
{
broadcastYear: 2020,
broadcastQuarter: 2,
broadcastMonth: 5,
week: ["2020-04-27 Mon", "2020-05-03 Sun"],
month: ["2020-04-27 Mon", "2020-05-31 Sun"],
quarter: ["2020-03-30 Mon", "2020-06-28 Sun"],
Expand All @@ -179,6 +191,7 @@ const broadcastTestData = [
{
broadcastYear: 2022,
broadcastQuarter: 1,
broadcastMonth: 1,
week: ["2021-12-27 Mon", "2022-01-02 Sun"],
month: ["2021-12-27 Mon", "2022-01-30 Sun"],
quarter: ["2021-12-27 Mon", "2022-03-27 Sun"],
Expand All @@ -187,6 +200,31 @@ const broadcastTestData = [
],
];

const broadcastMonthTestDates = [
["2024-01-01", 1],
["2024-01-30", 2],
["2024-01-31", 2],
["2024-03-31", 3],
["2024-04-01", 4],
["2024-04-28", 4],
["2024-04-29", 5],
["2024-04-30", 5],
["2024-11-25", 12],
["2024-11-30", 12],
["2024-12-01", 12],
["2024-12-31", 1],
];

test("getBroadcastMonth", (t) => {
broadcastMonthTestDates.forEach(([weekStart, monthNumber]) => {
t.is(
getBroadcastMonth(parseDateFromISO(weekStart)),
monthNumber,
`getBroadcastMonth ${weekStart}`,
);
});
});

const broadcastWeekTestDates = [
["2016-12-31", 1],
["2017-12-31", 53],
Expand Down Expand Up @@ -281,7 +319,7 @@ test("parseDateFromBroadcastWeekKey", (t) => {
});
});

test("broadcast calendar interval", (t) => {
test("broadcast calendar intervals", (t) => {
broadcastTestData.forEach(([weekStr, expected]) => {
const week = parseDateFromISO(weekStr);

Expand Down Expand Up @@ -324,6 +362,12 @@ test("broadcast calendar interval", (t) => {
`getBroadcastQuarter expects ${expected.broadcastQuarter}`,
);

t.deepEqual(
getBroadcastMonth(week),
expected.broadcastMonth,
`getBroadcastMonth expects ${expected.broadcastMonth}`,
);

t.deepEqual(
getBroadcastYearQuarter(week),
{
Expand Down

0 comments on commit 948fe52

Please sign in to comment.