@opencode-ai/util 0.0.0-dev-18819 → 0.0.0-dev-18822

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ export declare function activityCalendar(input: {
2
+ activity: readonly {
3
+ date: string;
4
+ steps: number;
5
+ }[];
6
+ from: number;
7
+ to: number;
8
+ maxWeeks: number;
9
+ }): {
10
+ weeks: {
11
+ date: string;
12
+ steps: number;
13
+ level: number;
14
+ }[][];
15
+ months: {
16
+ span: number;
17
+ week: number;
18
+ label: string;
19
+ }[];
20
+ clipped: boolean;
21
+ };
@@ -0,0 +1,42 @@
1
+ export function activityCalendar(input) {
2
+ // Extract local dates first, then use UTC ordinals so DST cannot add or drop cells.
3
+ const ordinal = (time) => {
4
+ const date = new Date(time);
5
+ return Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()) / 86_400_000;
6
+ };
7
+ const from = ordinal(input.from);
8
+ const to = ordinal(input.to - 1);
9
+ const monday = from - ((new Date(from * 86_400_000).getUTCDay() + 6) % 7);
10
+ const total = input.to <= input.from ? 0 : Math.floor((to - monday) / 7) + 1;
11
+ const count = Math.min(total, 53, Math.max(1, Math.floor(input.maxWeeks)));
12
+ const start = monday + Math.max(0, total - count) * 7;
13
+ const values = new Map(input.activity.map((day) => [day.date, day.steps]));
14
+ const levels = [...new Set([...values.values()].filter((steps) => steps > 0))].sort((a, b) => a - b);
15
+ const weeks = Array.from({ length: count }, (_, week) => Array.from({ length: 7 }, (_, day) => {
16
+ const date = start + week * 7 + day;
17
+ const key = new Date(date * 86_400_000).toISOString().slice(0, 10);
18
+ const steps = values.get(key) ?? 0;
19
+ return {
20
+ date: key,
21
+ steps,
22
+ level: date < from || date > to
23
+ ? -1
24
+ : steps === 0
25
+ ? 0
26
+ : Math.max(1, Math.ceil(((levels.indexOf(steps) + 1) / levels.length) * 4)),
27
+ };
28
+ }));
29
+ const formatter = new Intl.DateTimeFormat("en-US", { month: "short", timeZone: "UTC" });
30
+ // Thursday identifies the month owning most of a week; partial edge weeks stay inside the range.
31
+ const months = weeks
32
+ .map((_, week) => ({
33
+ week,
34
+ label: formatter.format(Math.min(to, Math.max(from, start + week * 7 + 3)) * 86_400_000),
35
+ }))
36
+ .filter((month, index, months) => index === 0 || month.label !== months[index - 1].label);
37
+ return {
38
+ weeks,
39
+ months: months.map((month, index) => ({ ...month, span: (months[index + 1]?.week ?? count) - month.week })),
40
+ clipped: count < total,
41
+ };
42
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,119 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { activityCalendar } from "./activity-calendar.js";
3
+ const input = {
4
+ from: new Date(2026, 0, 1).getTime(),
5
+ to: new Date(2026, 0, 8).getTime(),
6
+ maxWeeks: 53,
7
+ activity: [
8
+ { date: "2026-01-01", steps: 1 },
9
+ { date: "2026-01-02", steps: 50 },
10
+ ],
11
+ };
12
+ describe("activity calendar", () => {
13
+ test("aligns Monday-first weeks and excludes cells outside the end-exclusive range", () => {
14
+ const calendar = activityCalendar(input);
15
+ expect(calendar.clipped).toBe(false);
16
+ expect(calendar.weeks).toHaveLength(2);
17
+ expect(calendar.weeks[0][0]).toEqual({ date: "2025-12-29", steps: 0, level: -1 });
18
+ expect(calendar.weeks[0][3]).toEqual({ date: "2026-01-01", steps: 1, level: 2 });
19
+ expect(calendar.weeks[0][4]).toEqual({ date: "2026-01-02", steps: 50, level: 4 });
20
+ expect(calendar.weeks[1][2]).toEqual({ date: "2026-01-07", steps: 0, level: 0 });
21
+ expect(calendar.weeks[1][3].level).toBe(-1);
22
+ expect(calendar.months).toEqual([{ week: 0, label: "Jan", span: 2 }]);
23
+ });
24
+ test("clips to the latest weeks and keeps empty days distinct from out-of-range cells", () => {
25
+ const calendar = activityCalendar({ ...input, activity: [], maxWeeks: 1 });
26
+ expect(calendar.clipped).toBe(true);
27
+ expect(calendar.weeks).toHaveLength(1);
28
+ expect(calendar.weeks[0][0]).toEqual({ date: "2026-01-05", steps: 0, level: 0 });
29
+ expect(calendar.weeks[0][3].level).toBe(-1);
30
+ });
31
+ test("caps long histories at 53 weeks", () => {
32
+ const calendar = activityCalendar({ ...input, from: new Date(2020, 0, 1).getTime(), maxWeeks: 200 });
33
+ expect(calendar.clipped).toBe(true);
34
+ expect(calendar.weeks).toHaveLength(53);
35
+ expect(calendar.weeks.at(-1)?.[2].date).toBe("2026-01-07");
36
+ expect(calendar.months.reduce((sum, month) => sum + month.span, 0)).toBe(53);
37
+ });
38
+ test("returns month positions and spans for partial first and last months", () => {
39
+ const calendar = activityCalendar({
40
+ ...input,
41
+ from: new Date(2026, 3, 29).getTime(),
42
+ to: new Date(2026, 4, 20).getTime(),
43
+ });
44
+ expect(calendar.months).toEqual([
45
+ { week: 0, label: "Apr", span: 1 },
46
+ { week: 1, label: "May", span: 3 },
47
+ ]);
48
+ const partial = activityCalendar({
49
+ ...input,
50
+ from: new Date(2026, 3, 1).getTime(),
51
+ to: new Date(2026, 3, 2).getTime(),
52
+ });
53
+ expect(partial.months).toEqual([{ week: 0, label: "Apr", span: 1 }]);
54
+ });
55
+ test("places month labels across a year boundary", () => {
56
+ const calendar = activityCalendar({
57
+ ...input,
58
+ from: new Date(2025, 11, 22).getTime(),
59
+ to: new Date(2026, 0, 12).getTime(),
60
+ });
61
+ expect(calendar.months).toEqual([
62
+ { week: 0, label: "Dec", span: 1 },
63
+ { week: 1, label: "Jan", span: 2 },
64
+ ]);
65
+ });
66
+ test("keeps exactly seven distinct cells per week across DST and leap days", () => {
67
+ const calendar = activityCalendar({
68
+ from: new Date(2024, 1, 26).getTime(),
69
+ to: new Date(2024, 2, 12).getTime(),
70
+ maxWeeks: 53,
71
+ activity: [
72
+ { date: "2024-02-29", steps: 1 },
73
+ { date: "2024-03-10", steps: 1 },
74
+ ],
75
+ });
76
+ expect(calendar.weeks).toHaveLength(3);
77
+ expect(calendar.weeks
78
+ .flat()
79
+ .filter((day) => day.steps > 0)
80
+ .map((day) => day.date)).toEqual(["2024-02-29", "2024-03-10"]);
81
+ expect(new Set(calendar.weeks.flat().map((day) => day.date)).size).toBe(21);
82
+ });
83
+ test("uses the highest intensity for a single distinct positive count", () => {
84
+ const calendar = activityCalendar({ ...input, activity: input.activity.map((day) => ({ ...day, steps: 10 })) });
85
+ expect(calendar.weeks
86
+ .flat()
87
+ .filter((day) => day.steps > 0)
88
+ .map((day) => day.level)).toEqual([4, 4]);
89
+ });
90
+ test("ranks distinct activity counts in four levels", () => {
91
+ const calendar = activityCalendar({
92
+ ...input,
93
+ to: new Date(2026, 0, 10).getTime(),
94
+ activity: Array.from({ length: 8 }, (_, index) => ({ date: `2026-01-0${index + 1}`, steps: index + 1 })),
95
+ });
96
+ expect(calendar.weeks
97
+ .flat()
98
+ .filter((day) => day.steps > 0)
99
+ .map((day) => day.level)).toEqual([1, 1, 2, 2, 3, 3, 4, 4]);
100
+ });
101
+ test("keeps intensity relative to the requested range when older weeks are clipped", () => {
102
+ const calendar = activityCalendar({
103
+ ...input,
104
+ maxWeeks: 1,
105
+ activity: [
106
+ { date: "2026-01-01", steps: 50 },
107
+ { date: "2026-01-05", steps: 1 },
108
+ ],
109
+ });
110
+ expect(calendar.weeks[0][0].level).toBe(2);
111
+ });
112
+ test("returns an empty calendar for an empty range", () => {
113
+ expect(activityCalendar({ ...input, to: input.from, activity: [] })).toEqual({
114
+ weeks: [],
115
+ months: [],
116
+ clipped: false,
117
+ });
118
+ });
119
+ });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@opencode-ai/util",
4
- "version": "0.0.0-dev-18819",
4
+ "version": "0.0.0-dev-18822",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "repository": {