@thebriangao/totem 1.4.4
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.
- package/.dockerignore +23 -0
- package/.env.example +66 -0
- package/CHANGELOG.md +283 -0
- package/CONTRIBUTING.md +149 -0
- package/Dockerfile +53 -0
- package/LICENSE +37 -0
- package/README.md +587 -0
- package/SECURITY.md +79 -0
- package/TOOLS.md +429 -0
- package/WHOOP.md +2673 -0
- package/assets/banner.svg +38 -0
- package/dist/cli/index.js +589 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/setup.js +1127 -0
- package/dist/cli/setup.js.map +1 -0
- package/dist/cli/ui.js +512 -0
- package/dist/cli/ui.js.map +1 -0
- package/dist/cli/update.js +465 -0
- package/dist/cli/update.js.map +1 -0
- package/dist/data/behaviors.js +2777 -0
- package/dist/data/behaviors.js.map +1 -0
- package/dist/data/endpoints.js +320 -0
- package/dist/data/endpoints.js.map +1 -0
- package/dist/data/exercises.js +4863 -0
- package/dist/data/exercises.js.map +1 -0
- package/dist/data/sports.js +212 -0
- package/dist/data/sports.js.map +1 -0
- package/dist/lib/concurrency.js +27 -0
- package/dist/lib/concurrency.js.map +1 -0
- package/dist/lib/dates.js +29 -0
- package/dist/lib/dates.js.map +1 -0
- package/dist/lib/format.js +22 -0
- package/dist/lib/format.js.map +1 -0
- package/dist/lib/stats.js +32 -0
- package/dist/lib/stats.js.map +1 -0
- package/dist/lib/timezone.js +249 -0
- package/dist/lib/timezone.js.map +1 -0
- package/dist/lib/walk.js +396 -0
- package/dist/lib/walk.js.map +1 -0
- package/dist/projections/behavior_impact.js +96 -0
- package/dist/projections/behavior_impact.js.map +1 -0
- package/dist/projections/calendar.js +33 -0
- package/dist/projections/calendar.js.map +1 -0
- package/dist/projections/communities.js +105 -0
- package/dist/projections/communities.js.map +1 -0
- package/dist/projections/cycle.js +81 -0
- package/dist/projections/cycle.js.map +1 -0
- package/dist/projections/hr_zones.js +35 -0
- package/dist/projections/hr_zones.js.map +1 -0
- package/dist/projections/journal.js +56 -0
- package/dist/projections/journal.js.map +1 -0
- package/dist/projections/leaderboard.js +37 -0
- package/dist/projections/leaderboard.js.map +1 -0
- package/dist/projections/lift_exercise.js +22 -0
- package/dist/projections/lift_exercise.js.map +1 -0
- package/dist/projections/lift_history.js +133 -0
- package/dist/projections/lift_history.js.map +1 -0
- package/dist/projections/lift_library.js +71 -0
- package/dist/projections/lift_library.js.map +1 -0
- package/dist/projections/lift_progression.js +138 -0
- package/dist/projections/lift_progression.js.map +1 -0
- package/dist/projections/lift_prs.js +20 -0
- package/dist/projections/lift_prs.js.map +1 -0
- package/dist/projections/live_hr.js +19 -0
- package/dist/projections/live_hr.js.map +1 -0
- package/dist/projections/live_state.js +23 -0
- package/dist/projections/live_state.js.map +1 -0
- package/dist/projections/live_stress.js +11 -0
- package/dist/projections/live_stress.js.map +1 -0
- package/dist/projections/performance_assessment.js +15 -0
- package/dist/projections/performance_assessment.js.map +1 -0
- package/dist/projections/profile.js +59 -0
- package/dist/projections/profile.js.map +1 -0
- package/dist/projections/recovery.js +131 -0
- package/dist/projections/recovery.js.map +1 -0
- package/dist/projections/sleep.js +256 -0
- package/dist/projections/sleep.js.map +1 -0
- package/dist/projections/sleep_need.js +32 -0
- package/dist/projections/sleep_need.js.map +1 -0
- package/dist/projections/smart_alarm.js +45 -0
- package/dist/projections/smart_alarm.js.map +1 -0
- package/dist/projections/strain.js +128 -0
- package/dist/projections/strain.js.map +1 -0
- package/dist/projections/stress.js +86 -0
- package/dist/projections/stress.js.map +1 -0
- package/dist/projections/today.js +129 -0
- package/dist/projections/today.js.map +1 -0
- package/dist/projections/trend.js +149 -0
- package/dist/projections/trend.js.map +1 -0
- package/dist/projections/workout.js +161 -0
- package/dist/projections/workout.js.map +1 -0
- package/dist/projections/workouts.js +37 -0
- package/dist/projections/workouts.js.map +1 -0
- package/dist/schemas/calendar.js +12 -0
- package/dist/schemas/calendar.js.map +1 -0
- package/dist/schemas/coach.js +11 -0
- package/dist/schemas/coach.js.map +1 -0
- package/dist/schemas/communities.js +51 -0
- package/dist/schemas/communities.js.map +1 -0
- package/dist/schemas/compare.js +15 -0
- package/dist/schemas/compare.js.map +1 -0
- package/dist/schemas/escape.js +18 -0
- package/dist/schemas/escape.js.map +1 -0
- package/dist/schemas/journal.js +69 -0
- package/dist/schemas/journal.js.map +1 -0
- package/dist/schemas/leaderboard.js +25 -0
- package/dist/schemas/leaderboard.js.map +1 -0
- package/dist/schemas/live.js +26 -0
- package/dist/schemas/live.js.map +1 -0
- package/dist/schemas/performance.js +12 -0
- package/dist/schemas/performance.js.map +1 -0
- package/dist/schemas/primitives.js +29 -0
- package/dist/schemas/primitives.js.map +1 -0
- package/dist/schemas/profile.js +39 -0
- package/dist/schemas/profile.js.map +1 -0
- package/dist/schemas/recovery.js +27 -0
- package/dist/schemas/recovery.js.map +1 -0
- package/dist/schemas/settings.js +26 -0
- package/dist/schemas/settings.js.map +1 -0
- package/dist/schemas/sleep.js +38 -0
- package/dist/schemas/sleep.js.map +1 -0
- package/dist/schemas/sleep_need.js +15 -0
- package/dist/schemas/sleep_need.js.map +1 -0
- package/dist/schemas/smart_alarm.js +36 -0
- package/dist/schemas/smart_alarm.js.map +1 -0
- package/dist/schemas/strain.js +24 -0
- package/dist/schemas/strain.js.map +1 -0
- package/dist/schemas/strength.js +170 -0
- package/dist/schemas/strength.js.map +1 -0
- package/dist/schemas/stress.js +16 -0
- package/dist/schemas/stress.js.map +1 -0
- package/dist/schemas/today.js +38 -0
- package/dist/schemas/today.js.map +1 -0
- package/dist/schemas/trend.js +50 -0
- package/dist/schemas/trend.js.map +1 -0
- package/dist/schemas/womens_health.js +23 -0
- package/dist/schemas/womens_health.js.map +1 -0
- package/dist/schemas/workouts.js +50 -0
- package/dist/schemas/workouts.js.map +1 -0
- package/dist/scripts/cognito_bootstrap.js +278 -0
- package/dist/scripts/cognito_bootstrap.js.map +1 -0
- package/dist/server-http.js +221 -0
- package/dist/server-http.js.map +1 -0
- package/dist/server.js +74 -0
- package/dist/server.js.map +1 -0
- package/dist/tools/register.js +117 -0
- package/dist/tools/register.js.map +1 -0
- package/dist/tools/v2/activity_create.js +64 -0
- package/dist/tools/v2/activity_create.js.map +1 -0
- package/dist/tools/v2/activity_delete.js +26 -0
- package/dist/tools/v2/activity_delete.js.map +1 -0
- package/dist/tools/v2/behavior_impact.js +36 -0
- package/dist/tools/v2/behavior_impact.js.map +1 -0
- package/dist/tools/v2/calendar.js +28 -0
- package/dist/tools/v2/calendar.js.map +1 -0
- package/dist/tools/v2/coach_ask.js +117 -0
- package/dist/tools/v2/coach_ask.js.map +1 -0
- package/dist/tools/v2/communities.js +60 -0
- package/dist/tools/v2/communities.js.map +1 -0
- package/dist/tools/v2/compare.js +67 -0
- package/dist/tools/v2/compare.js.map +1 -0
- package/dist/tools/v2/cycle.js +23 -0
- package/dist/tools/v2/cycle.js.map +1 -0
- package/dist/tools/v2/cycle_log.js +43 -0
- package/dist/tools/v2/cycle_log.js.map +1 -0
- package/dist/tools/v2/day.js +32 -0
- package/dist/tools/v2/day.js.map +1 -0
- package/dist/tools/v2/endpoints.js +28 -0
- package/dist/tools/v2/endpoints.js.map +1 -0
- package/dist/tools/v2/hidden_metric.js +28 -0
- package/dist/tools/v2/hidden_metric.js.map +1 -0
- package/dist/tools/v2/hr_zones.js +24 -0
- package/dist/tools/v2/hr_zones.js.map +1 -0
- package/dist/tools/v2/hr_zones_set.js +51 -0
- package/dist/tools/v2/hr_zones_set.js.map +1 -0
- package/dist/tools/v2/journal.js +26 -0
- package/dist/tools/v2/journal.js.map +1 -0
- package/dist/tools/v2/journal_autopop.js +19 -0
- package/dist/tools/v2/journal_autopop.js.map +1 -0
- package/dist/tools/v2/journal_catalog.js +44 -0
- package/dist/tools/v2/journal_catalog.js.map +1 -0
- package/dist/tools/v2/journal_log.js +78 -0
- package/dist/tools/v2/journal_log.js.map +1 -0
- package/dist/tools/v2/leaderboard.js +79 -0
- package/dist/tools/v2/leaderboard.js.map +1 -0
- package/dist/tools/v2/lift_catalog.js +47 -0
- package/dist/tools/v2/lift_catalog.js.map +1 -0
- package/dist/tools/v2/lift_custom_exercise.js +108 -0
- package/dist/tools/v2/lift_custom_exercise.js.map +1 -0
- package/dist/tools/v2/lift_exercise.js +31 -0
- package/dist/tools/v2/lift_exercise.js.map +1 -0
- package/dist/tools/v2/lift_history.js +39 -0
- package/dist/tools/v2/lift_history.js.map +1 -0
- package/dist/tools/v2/lift_library.js +22 -0
- package/dist/tools/v2/lift_library.js.map +1 -0
- package/dist/tools/v2/lift_log.js +95 -0
- package/dist/tools/v2/lift_log.js.map +1 -0
- package/dist/tools/v2/lift_progression.js +30 -0
- package/dist/tools/v2/lift_progression.js.map +1 -0
- package/dist/tools/v2/lift_prs.js +21 -0
- package/dist/tools/v2/lift_prs.js.map +1 -0
- package/dist/tools/v2/lift_template_save.js +83 -0
- package/dist/tools/v2/lift_template_save.js.map +1 -0
- package/dist/tools/v2/live_hr.js +21 -0
- package/dist/tools/v2/live_hr.js.map +1 -0
- package/dist/tools/v2/live_state.js +21 -0
- package/dist/tools/v2/live_state.js.map +1 -0
- package/dist/tools/v2/live_stress.js +22 -0
- package/dist/tools/v2/live_stress.js.map +1 -0
- package/dist/tools/v2/performance_assessment.js +38 -0
- package/dist/tools/v2/performance_assessment.js.map +1 -0
- package/dist/tools/v2/profile.js +28 -0
- package/dist/tools/v2/profile.js.map +1 -0
- package/dist/tools/v2/profile_update.js +66 -0
- package/dist/tools/v2/profile_update.js.map +1 -0
- package/dist/tools/v2/raw.js +43 -0
- package/dist/tools/v2/raw.js.map +1 -0
- package/dist/tools/v2/recovery.js +31 -0
- package/dist/tools/v2/recovery.js.map +1 -0
- package/dist/tools/v2/sleep.js +23 -0
- package/dist/tools/v2/sleep.js.map +1 -0
- package/dist/tools/v2/sleep_need.js +21 -0
- package/dist/tools/v2/sleep_need.js.map +1 -0
- package/dist/tools/v2/smart_alarm.js +24 -0
- package/dist/tools/v2/smart_alarm.js.map +1 -0
- package/dist/tools/v2/smart_alarm_set.js +94 -0
- package/dist/tools/v2/smart_alarm_set.js.map +1 -0
- package/dist/tools/v2/sports_catalog.js +26 -0
- package/dist/tools/v2/sports_catalog.js.map +1 -0
- package/dist/tools/v2/strain.js +23 -0
- package/dist/tools/v2/strain.js.map +1 -0
- package/dist/tools/v2/stress.js +23 -0
- package/dist/tools/v2/stress.js.map +1 -0
- package/dist/tools/v2/symptom_log.js +72 -0
- package/dist/tools/v2/symptom_log.js.map +1 -0
- package/dist/tools/v2/today.js +30 -0
- package/dist/tools/v2/today.js.map +1 -0
- package/dist/tools/v2/trend.js +26 -0
- package/dist/tools/v2/trend.js.map +1 -0
- package/dist/tools/v2/workout.js +23 -0
- package/dist/tools/v2/workout.js.map +1 -0
- package/dist/tools/v2/workouts.js +32 -0
- package/dist/tools/v2/workouts.js.map +1 -0
- package/dist/whoop/build_lift_body.js +67 -0
- package/dist/whoop/build_lift_body.js.map +1 -0
- package/dist/whoop/client.js +84 -0
- package/dist/whoop/client.js.map +1 -0
- package/dist/whoop/cognito.js +110 -0
- package/dist/whoop/cognito.js.map +1 -0
- package/dist/whoop/constants.js +4 -0
- package/dist/whoop/constants.js.map +1 -0
- package/dist/whoop/device.js +103 -0
- package/dist/whoop/device.js.map +1 -0
- package/dist/whoop/errors.js +42 -0
- package/dist/whoop/errors.js.map +1 -0
- package/dist/whoop/init_timezone.js +62 -0
- package/dist/whoop/init_timezone.js.map +1 -0
- package/dist/whoop/installation.js +62 -0
- package/dist/whoop/installation.js.map +1 -0
- package/dist/whoop/json_out.js +9 -0
- package/dist/whoop/json_out.js.map +1 -0
- package/dist/whoop/oauth_provider.js +297 -0
- package/dist/whoop/oauth_provider.js.map +1 -0
- package/dist/whoop/session_state.js +34 -0
- package/dist/whoop/session_state.js.map +1 -0
- package/dist/whoop/token_manager.js +60 -0
- package/dist/whoop/token_manager.js.map +1 -0
- package/dist/whoop/token_store.js +47 -0
- package/dist/whoop/token_store.js.map +1 -0
- package/dist/whoop/types.js +82 -0
- package/dist/whoop/types.js.map +1 -0
- package/dist/whoop/write_safety.js +23 -0
- package/dist/whoop/write_safety.js.map +1 -0
- package/package.json +66 -0
- package/src/cli/index.ts +597 -0
- package/src/cli/setup.ts +1106 -0
- package/src/cli/ui.ts +505 -0
- package/src/cli/update.ts +442 -0
- package/src/data/behaviors.ts +2784 -0
- package/src/data/endpoints.ts +319 -0
- package/src/data/exercises.ts +4868 -0
- package/src/data/sports.ts +218 -0
- package/src/lib/concurrency.ts +28 -0
- package/src/lib/dates.ts +35 -0
- package/src/lib/format.ts +27 -0
- package/src/lib/stats.ts +35 -0
- package/src/lib/timezone.ts +269 -0
- package/src/lib/walk.ts +418 -0
- package/src/projections/behavior_impact.ts +98 -0
- package/src/projections/calendar.ts +46 -0
- package/src/projections/communities.ts +135 -0
- package/src/projections/cycle.ts +85 -0
- package/src/projections/hr_zones.ts +43 -0
- package/src/projections/journal.ts +55 -0
- package/src/projections/leaderboard.ts +49 -0
- package/src/projections/lift_exercise.ts +29 -0
- package/src/projections/lift_history.ts +140 -0
- package/src/projections/lift_library.ts +70 -0
- package/src/projections/lift_progression.ts +138 -0
- package/src/projections/lift_prs.ts +23 -0
- package/src/projections/live_hr.ts +21 -0
- package/src/projections/live_state.ts +28 -0
- package/src/projections/live_stress.ts +13 -0
- package/src/projections/performance_assessment.ts +19 -0
- package/src/projections/profile.ts +71 -0
- package/src/projections/recovery.ts +133 -0
- package/src/projections/sleep.ts +276 -0
- package/src/projections/sleep_need.ts +36 -0
- package/src/projections/smart_alarm.ts +70 -0
- package/src/projections/strain.ts +131 -0
- package/src/projections/stress.ts +95 -0
- package/src/projections/today.ts +166 -0
- package/src/projections/trend.ts +149 -0
- package/src/projections/workout.ts +168 -0
- package/src/projections/workouts.ts +36 -0
- package/src/schemas/calendar.ts +13 -0
- package/src/schemas/coach.ts +11 -0
- package/src/schemas/communities.ts +56 -0
- package/src/schemas/compare.ts +17 -0
- package/src/schemas/escape.ts +21 -0
- package/src/schemas/journal.ts +80 -0
- package/src/schemas/leaderboard.ts +26 -0
- package/src/schemas/live.ts +31 -0
- package/src/schemas/performance.ts +13 -0
- package/src/schemas/primitives.ts +35 -0
- package/src/schemas/profile.ts +40 -0
- package/src/schemas/recovery.ts +28 -0
- package/src/schemas/settings.ts +30 -0
- package/src/schemas/sleep.ts +40 -0
- package/src/schemas/sleep_need.ts +16 -0
- package/src/schemas/smart_alarm.ts +40 -0
- package/src/schemas/strain.ts +25 -0
- package/src/schemas/strength.ts +189 -0
- package/src/schemas/stress.ts +17 -0
- package/src/schemas/today.ts +39 -0
- package/src/schemas/trend.ts +53 -0
- package/src/schemas/womens_health.ts +26 -0
- package/src/schemas/workouts.ts +55 -0
- package/src/scripts/cognito_bootstrap.ts +249 -0
- package/src/server-http.ts +241 -0
- package/src/server.ts +85 -0
- package/src/tools/register.ts +120 -0
- package/src/tools/v2/activity_create.ts +74 -0
- package/src/tools/v2/activity_delete.ts +33 -0
- package/src/tools/v2/behavior_impact.ts +39 -0
- package/src/tools/v2/calendar.ts +33 -0
- package/src/tools/v2/coach_ask.ts +131 -0
- package/src/tools/v2/communities.ts +65 -0
- package/src/tools/v2/compare.ts +83 -0
- package/src/tools/v2/cycle.ts +28 -0
- package/src/tools/v2/cycle_log.ts +51 -0
- package/src/tools/v2/day.ts +37 -0
- package/src/tools/v2/endpoints.ts +32 -0
- package/src/tools/v2/hidden_metric.ts +33 -0
- package/src/tools/v2/hr_zones.ts +29 -0
- package/src/tools/v2/hr_zones_set.ts +57 -0
- package/src/tools/v2/journal.ts +31 -0
- package/src/tools/v2/journal_autopop.ts +26 -0
- package/src/tools/v2/journal_catalog.ts +48 -0
- package/src/tools/v2/journal_log.ts +84 -0
- package/src/tools/v2/leaderboard.ts +92 -0
- package/src/tools/v2/lift_catalog.ts +48 -0
- package/src/tools/v2/lift_custom_exercise.ts +117 -0
- package/src/tools/v2/lift_exercise.ts +35 -0
- package/src/tools/v2/lift_history.ts +52 -0
- package/src/tools/v2/lift_library.ts +28 -0
- package/src/tools/v2/lift_log.ts +102 -0
- package/src/tools/v2/lift_progression.ts +34 -0
- package/src/tools/v2/lift_prs.ts +26 -0
- package/src/tools/v2/lift_template_save.ts +89 -0
- package/src/tools/v2/live_hr.ts +26 -0
- package/src/tools/v2/live_state.ts +26 -0
- package/src/tools/v2/live_stress.ts +27 -0
- package/src/tools/v2/performance_assessment.ts +44 -0
- package/src/tools/v2/profile.ts +33 -0
- package/src/tools/v2/profile_update.ts +76 -0
- package/src/tools/v2/raw.ts +50 -0
- package/src/tools/v2/recovery.ts +36 -0
- package/src/tools/v2/sleep.ts +28 -0
- package/src/tools/v2/sleep_need.ts +26 -0
- package/src/tools/v2/smart_alarm.ts +29 -0
- package/src/tools/v2/smart_alarm_set.ts +103 -0
- package/src/tools/v2/sports_catalog.ts +32 -0
- package/src/tools/v2/strain.ts +28 -0
- package/src/tools/v2/stress.ts +28 -0
- package/src/tools/v2/symptom_log.ts +78 -0
- package/src/tools/v2/today.ts +35 -0
- package/src/tools/v2/trend.ts +31 -0
- package/src/tools/v2/workout.ts +28 -0
- package/src/tools/v2/workouts.ts +37 -0
- package/src/whoop/build_lift_body.ts +84 -0
- package/src/whoop/client.ts +118 -0
- package/src/whoop/cognito.ts +150 -0
- package/src/whoop/constants.ts +3 -0
- package/src/whoop/device.ts +106 -0
- package/src/whoop/errors.ts +41 -0
- package/src/whoop/init_timezone.ts +64 -0
- package/src/whoop/installation.ts +58 -0
- package/src/whoop/json_out.ts +9 -0
- package/src/whoop/oauth_provider.ts +377 -0
- package/src/whoop/session_state.ts +41 -0
- package/src/whoop/token_manager.ts +76 -0
- package/src/whoop/token_store.ts +45 -0
- package/src/whoop/types.ts +89 -0
- package/src/whoop/write_safety.ts +30 -0
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
// Convert Whoop's UTC timestamps (which end in "Z") to ISO 8601 strings with
|
|
2
|
+
// an explicit offset for the user's timezone (e.g., "2026-05-25T15:30:00-07:00").
|
|
3
|
+
//
|
|
4
|
+
// Why: Whoop returns every timestamp in UTC, so without conversion the AI sees
|
|
5
|
+
// "22:30:00Z" and (correctly) treats it as 10:30 PM UTC. For Brian in San Jose
|
|
6
|
+
// at PDT/UTC-7 that's actually 3:30 PM local, but the AI can't infer that
|
|
7
|
+
// without knowing the user's TZ.
|
|
8
|
+
//
|
|
9
|
+
// Approach: write the explicit offset into every timestamp so it's unambiguous.
|
|
10
|
+
// "2026-05-25T15:30:00-07:00" means "3:30 PM in a -07:00 zone" — same instant
|
|
11
|
+
// as the original UTC timestamp, but the AI now knows the local clock value
|
|
12
|
+
// without needing extra context.
|
|
13
|
+
//
|
|
14
|
+
// Config:
|
|
15
|
+
// WHOOP_TIMEZONE=America/Los_Angeles (IANA name; default = system TZ)
|
|
16
|
+
//
|
|
17
|
+
// Date-only strings ("2026-05-25") and timestamps that already carry an offset
|
|
18
|
+
// pass through unchanged — only "*Z" UTC timestamps are converted.
|
|
19
|
+
// Matches a UTC timestamp in any of the forms Whoop emits: "...Z", "...+0000",
|
|
20
|
+
// or "...+00:00" (the journal / pg-range endpoints use the +0000 form, the
|
|
21
|
+
// deep-dive endpoints use Z). All three mean UTC and should be localized.
|
|
22
|
+
const UTC_ISO_RE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|\+00:?00)$/;
|
|
23
|
+
// Trailing UTC marker (Z / +0000 / +00:00) — used to strip before re-suffixing.
|
|
24
|
+
const UTC_SUFFIX_RE = /(?:Z|\+00:?00)$/;
|
|
25
|
+
// Matches both Whoop's "+0000"/"-0700" form and standard "+00:00"/"-07:00" form.
|
|
26
|
+
const FIXED_OFFSET_RE = /^([+-])(\d{2}):?(\d{2})$/;
|
|
27
|
+
// Module-level cache populated by `setProfileTimezone()` at server boot. This
|
|
28
|
+
// is Tier 2 of the priority chain — for users who didn't set `WHOOP_TIMEZONE`
|
|
29
|
+
// explicitly, we auto-detect from their Whoop profile's `timezone_offset`
|
|
30
|
+
// (which their phone keeps current).
|
|
31
|
+
let cachedProfileTz = null;
|
|
32
|
+
export function setProfileTimezone(tzOrOffset) {
|
|
33
|
+
cachedProfileTz = tzOrOffset;
|
|
34
|
+
}
|
|
35
|
+
export function getProfileTimezone() {
|
|
36
|
+
return cachedProfileTz;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Resolves the timezone the server should use for outgoing timestamps.
|
|
40
|
+
*
|
|
41
|
+
* Priority chain:
|
|
42
|
+
* 1. WHOOP_TIMEZONE env var (IANA name, explicit override)
|
|
43
|
+
* 2. Cached profile offset (auto-detected from Whoop, e.g. "-0700")
|
|
44
|
+
* 3. Server's system timezone (Intl default — UTC inside Docker / Fly)
|
|
45
|
+
*/
|
|
46
|
+
export function getTimezone() {
|
|
47
|
+
return (process.env.WHOOP_TIMEZONE
|
|
48
|
+
?? cachedProfileTz
|
|
49
|
+
?? Intl.DateTimeFormat().resolvedOptions().timeZone);
|
|
50
|
+
}
|
|
51
|
+
export function isUtcIso(s) {
|
|
52
|
+
return UTC_ISO_RE.test(s);
|
|
53
|
+
}
|
|
54
|
+
const pad2 = (n) => String(n).padStart(2, "0");
|
|
55
|
+
/**
|
|
56
|
+
* Return the UTC offset (e.g. "-07:00") observed in `tz` at the given instant.
|
|
57
|
+
* Handles IANA names (via Intl) and fixed offsets ("-0700" / "-07:00").
|
|
58
|
+
*/
|
|
59
|
+
function offsetAt(instant, tz) {
|
|
60
|
+
const fixed = parseFixedOffset(tz);
|
|
61
|
+
if (fixed)
|
|
62
|
+
return `${fixed.sign}${fixed.hh}:${fixed.mm}`;
|
|
63
|
+
const parts = new Intl.DateTimeFormat("en-US", {
|
|
64
|
+
timeZone: tz,
|
|
65
|
+
timeZoneName: "longOffset",
|
|
66
|
+
year: "numeric",
|
|
67
|
+
month: "2-digit",
|
|
68
|
+
day: "2-digit",
|
|
69
|
+
}).formatToParts(instant);
|
|
70
|
+
const raw = parts.find((p) => p.type === "timeZoneName")?.value ?? "GMT";
|
|
71
|
+
const m = raw.match(/GMT([+-])(\d{1,2})(?::?(\d{2}))?/);
|
|
72
|
+
if (!m)
|
|
73
|
+
return "+00:00";
|
|
74
|
+
return `${m[1]}${m[2].padStart(2, "0")}:${(m[3] ?? "00").padStart(2, "0")}`;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Build an ISO-8601 timestamp with explicit offset for a wall-clock time on a
|
|
78
|
+
* given calendar date in `tz`. Used by projections (stress, in-workout HR) whose
|
|
79
|
+
* source data carries only local clock labels ("9:38 AM") with no absolute
|
|
80
|
+
* timestamp.
|
|
81
|
+
*
|
|
82
|
+
* wallClockIso("2026-05-30", 9, 38, "America/Los_Angeles")
|
|
83
|
+
* → "2026-05-30T09:38:00-07:00"
|
|
84
|
+
*
|
|
85
|
+
* The offset is resolved for that wall-clock instant, so it's DST-correct except
|
|
86
|
+
* within the ~1h fold around a transition, which is immaterial here.
|
|
87
|
+
*/
|
|
88
|
+
export function wallClockIso(date, hour, minute, tz = getTimezone()) {
|
|
89
|
+
const ref = new Date(`${date}T${pad2(hour)}:${pad2(minute)}:00Z`);
|
|
90
|
+
const offset = Number.isNaN(ref.getTime()) ? "+00:00" : offsetAt(ref, tz);
|
|
91
|
+
return `${date}T${pad2(hour)}:${pad2(minute)}:00${offset}`;
|
|
92
|
+
}
|
|
93
|
+
/** "9:38 AM" / "11:52 PM" / "14:05" → minutes since midnight (0..1439), or null. */
|
|
94
|
+
export function clockLabelToMinutes(label) {
|
|
95
|
+
if (!label)
|
|
96
|
+
return null;
|
|
97
|
+
const m = label.trim().match(/^(\d{1,2}):(\d{2})\s*([AP]M)?$/i);
|
|
98
|
+
if (!m)
|
|
99
|
+
return null;
|
|
100
|
+
let h = Number(m[1]);
|
|
101
|
+
const min = Number(m[2]);
|
|
102
|
+
const ap = m[3]?.toUpperCase();
|
|
103
|
+
if (ap === "PM" && h !== 12)
|
|
104
|
+
h += 12;
|
|
105
|
+
if (ap === "AM" && h === 12)
|
|
106
|
+
h = 0;
|
|
107
|
+
if (h > 23 || min > 59)
|
|
108
|
+
return null;
|
|
109
|
+
return h * 60 + min;
|
|
110
|
+
}
|
|
111
|
+
function parseFixedOffset(s) {
|
|
112
|
+
const m = s.match(FIXED_OFFSET_RE);
|
|
113
|
+
if (!m)
|
|
114
|
+
return null;
|
|
115
|
+
return { sign: m[1], hh: m[2], mm: m[3] };
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Convert a UTC ISO timestamp ("2026-05-25T22:30:00.000Z") to the user's local
|
|
119
|
+
* timezone, formatted as ISO 8601 with explicit offset
|
|
120
|
+
* ("2026-05-25T15:30:00-07:00"). Preserves millisecond precision if present.
|
|
121
|
+
*
|
|
122
|
+
* Invalid input is returned unchanged (defensive — we'd rather pass through
|
|
123
|
+
* something funky than throw and break the whole tool call).
|
|
124
|
+
*/
|
|
125
|
+
export function toLocalIso(utcIso, tz = getTimezone()) {
|
|
126
|
+
if (!isUtcIso(utcIso))
|
|
127
|
+
return utcIso;
|
|
128
|
+
const date = new Date(utcIso);
|
|
129
|
+
if (Number.isNaN(date.getTime()))
|
|
130
|
+
return utcIso;
|
|
131
|
+
// Fixed-offset TZ ("-0700" or "-07:00") — Whoop's profile.timezone_offset uses
|
|
132
|
+
// this form. Intl.DateTimeFormat only accepts IANA names, so we shift the
|
|
133
|
+
// instant by the offset and format manually.
|
|
134
|
+
const fixedOffset = parseFixedOffset(tz);
|
|
135
|
+
if (fixedOffset) {
|
|
136
|
+
const offsetMin = (Number(fixedOffset.hh) * 60 + Number(fixedOffset.mm))
|
|
137
|
+
* (fixedOffset.sign === "+" ? 1 : -1);
|
|
138
|
+
const shifted = new Date(date.getTime() + offsetMin * 60 * 1000);
|
|
139
|
+
const pad = (n, w = 2) => String(n).padStart(w, "0");
|
|
140
|
+
const Y = shifted.getUTCFullYear();
|
|
141
|
+
const M = pad(shifted.getUTCMonth() + 1);
|
|
142
|
+
const D = pad(shifted.getUTCDate());
|
|
143
|
+
const h = pad(shifted.getUTCHours());
|
|
144
|
+
const m = pad(shifted.getUTCMinutes());
|
|
145
|
+
const s = pad(shifted.getUTCSeconds());
|
|
146
|
+
const msMatch = utcIso.match(/\.(\d+)(?:Z|\+00:?00)$/);
|
|
147
|
+
const fraction = msMatch ? `.${msMatch[1]}` : "";
|
|
148
|
+
return `${Y}-${M}-${D}T${h}:${m}:${s}${fraction}${fixedOffset.sign}${fixedOffset.hh}:${fixedOffset.mm}`;
|
|
149
|
+
}
|
|
150
|
+
// IANA name path — uses Intl.DateTimeFormat which knows about DST transitions.
|
|
151
|
+
const parts = new Intl.DateTimeFormat("en-US", {
|
|
152
|
+
timeZone: tz,
|
|
153
|
+
year: "numeric",
|
|
154
|
+
month: "2-digit",
|
|
155
|
+
day: "2-digit",
|
|
156
|
+
hour: "2-digit",
|
|
157
|
+
minute: "2-digit",
|
|
158
|
+
second: "2-digit",
|
|
159
|
+
hour12: false,
|
|
160
|
+
timeZoneName: "longOffset",
|
|
161
|
+
}).formatToParts(date);
|
|
162
|
+
const get = (type) => parts.find((p) => p.type === type)?.value ?? "";
|
|
163
|
+
const Y = get("year");
|
|
164
|
+
const M = get("month");
|
|
165
|
+
const D = get("day");
|
|
166
|
+
// Intl.DateTimeFormat with hour12:false sometimes returns "24" for midnight
|
|
167
|
+
// in certain locales — normalize to "00".
|
|
168
|
+
let h = get("hour");
|
|
169
|
+
if (h === "24")
|
|
170
|
+
h = "00";
|
|
171
|
+
const m = get("minute");
|
|
172
|
+
const s = get("second");
|
|
173
|
+
// Preserve sub-second precision from the original.
|
|
174
|
+
const msMatch = utcIso.match(/\.(\d+)(?:Z|\+00:?00)$/);
|
|
175
|
+
const fraction = msMatch ? `.${msMatch[1]}` : "";
|
|
176
|
+
const offsetRaw = get("timeZoneName"); // "GMT-07:00" or "GMT" for UTC
|
|
177
|
+
let offset = "+00:00";
|
|
178
|
+
const match = offsetRaw.match(/GMT([+-])(\d{1,2})(?::?(\d{2}))?/);
|
|
179
|
+
if (match) {
|
|
180
|
+
const sign = match[1];
|
|
181
|
+
const hh = match[2].padStart(2, "0");
|
|
182
|
+
const mm = (match[3] ?? "00").padStart(2, "0");
|
|
183
|
+
offset = `${sign}${hh}:${mm}`;
|
|
184
|
+
}
|
|
185
|
+
return `${Y}-${M}-${D}T${h}:${m}:${s}${fraction}${offset}`;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Walk an arbitrary JSON-ish value and convert every UTC-ISO string to local
|
|
189
|
+
* time. Non-string values are returned as-is. Object keys are preserved.
|
|
190
|
+
*
|
|
191
|
+
* This is the single function `jsonOut()` calls — it covers every tool's
|
|
192
|
+
* response without each projection needing to know about timezones.
|
|
193
|
+
*/
|
|
194
|
+
export function localizeTimestamps(value, tz = getTimezone()) {
|
|
195
|
+
if (typeof value === "string") {
|
|
196
|
+
return (isUtcIso(value) ? toLocalIso(value, tz) : value);
|
|
197
|
+
}
|
|
198
|
+
if (Array.isArray(value)) {
|
|
199
|
+
return value.map((item) => localizeTimestamps(item, tz));
|
|
200
|
+
}
|
|
201
|
+
if (value !== null && typeof value === "object") {
|
|
202
|
+
const out = {};
|
|
203
|
+
for (const [k, v] of Object.entries(value)) {
|
|
204
|
+
out[k] = localizeTimestamps(v, tz);
|
|
205
|
+
}
|
|
206
|
+
return out;
|
|
207
|
+
}
|
|
208
|
+
return value;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Returns the calendar components of an instant as observed in the user's
|
|
212
|
+
* timezone. Handles both IANA names (`America/Los_Angeles`) and fixed offsets
|
|
213
|
+
* (`-0700` / `-07:00`, the form Whoop's profile returns).
|
|
214
|
+
*
|
|
215
|
+
* This is what makes "today" correct on a UTC server: `new Date()` is the
|
|
216
|
+
* right instant, but `.getDate()` would give the server's (UTC) calendar day.
|
|
217
|
+
* This converts to the user's calendar day instead.
|
|
218
|
+
*/
|
|
219
|
+
export function zonedParts(date = new Date(), tz = getTimezone()) {
|
|
220
|
+
const fixed = parseFixedOffset(tz);
|
|
221
|
+
if (fixed) {
|
|
222
|
+
const offsetMin = (Number(fixed.hh) * 60 + Number(fixed.mm)) * (fixed.sign === "+" ? 1 : -1);
|
|
223
|
+
const shifted = new Date(date.getTime() + offsetMin * 60 * 1000);
|
|
224
|
+
return {
|
|
225
|
+
year: shifted.getUTCFullYear(),
|
|
226
|
+
month: shifted.getUTCMonth() + 1,
|
|
227
|
+
day: shifted.getUTCDate(),
|
|
228
|
+
hour: shifted.getUTCHours(),
|
|
229
|
+
minute: shifted.getUTCMinutes(),
|
|
230
|
+
second: shifted.getUTCSeconds(),
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
const parts = new Intl.DateTimeFormat("en-US", {
|
|
234
|
+
timeZone: tz,
|
|
235
|
+
year: "numeric",
|
|
236
|
+
month: "2-digit",
|
|
237
|
+
day: "2-digit",
|
|
238
|
+
hour: "2-digit",
|
|
239
|
+
minute: "2-digit",
|
|
240
|
+
second: "2-digit",
|
|
241
|
+
hour12: false,
|
|
242
|
+
}).formatToParts(date);
|
|
243
|
+
const get = (t) => Number(parts.find((p) => p.type === t)?.value ?? "0");
|
|
244
|
+
let hour = get("hour");
|
|
245
|
+
if (hour === 24)
|
|
246
|
+
hour = 0; // some locales render midnight as 24
|
|
247
|
+
return { year: get("year"), month: get("month"), day: get("day"), hour, minute: get("minute"), second: get("second") };
|
|
248
|
+
}
|
|
249
|
+
//# sourceMappingURL=timezone.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timezone.js","sourceRoot":"","sources":["../../src/lib/timezone.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,kFAAkF;AAClF,EAAE;AACF,+EAA+E;AAC/E,+EAA+E;AAC/E,0EAA0E;AAC1E,iCAAiC;AACjC,EAAE;AACF,gFAAgF;AAChF,8EAA8E;AAC9E,4EAA4E;AAC5E,iCAAiC;AACjC,EAAE;AACF,UAAU;AACV,0EAA0E;AAC1E,EAAE;AACF,+EAA+E;AAC/E,mE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C,aAAa,EAAE;SAChC,CAAC;IACJ,CAAC;IACD,MAAM,KAAK,GAAG,IAAI,IAAI,CAAC,cAAc,CAAC,OAAO,EAAE;QAC7C,QAAQ,EAAE,EAAE;QACZ,IAAI,EAAE,SAAS;QACf,KAAK,EAAE,SAAS;QAChB,GAAG,EAAE,SAAS;QACd,IAAI,EAAE,SAAS;QACf,MAAM,EAAE,SAAS;QACjB,MAAM,EAAE,SAAS;QACjB,MAAM,EAAE,KAAK;KACd,CAAC,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;IACvB,MAAM,GAAG,GAAG,CAAC,CAA+B,EAAU,EAAE,CACtD,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,EAAE,KAAK,IAAI,GAAG,CAAC,CAAC;IACxD,IAAI,IAAI,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC;IACvB,IAAI,IAAI,KAAK,EAAE;QAAE,IAAI,GAAG,CAAC,CAAC,CAAC,qCAAqC;IAChE,OAAO,EAAE,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,GAAG,CAAC,OAAO,CAAC,EAAE,GAAG,EAAE,GAAG,CAAC,KAAK,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,CAAC,QAAQ,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;AACzH,CAAC"}
|
package/dist/lib/walk.js
ADDED
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
// Tree-walking + type-coercion helpers used across projections.
|
|
2
|
+
// Defensive: every helper short-circuits on bad shapes instead of throwing.
|
|
3
|
+
export function isObject(x) {
|
|
4
|
+
return typeof x === "object" && x !== null && !Array.isArray(x);
|
|
5
|
+
}
|
|
6
|
+
export function asArray(x) {
|
|
7
|
+
return Array.isArray(x) ? x : [];
|
|
8
|
+
}
|
|
9
|
+
export function asString(x) {
|
|
10
|
+
return typeof x === "string" ? x : null;
|
|
11
|
+
}
|
|
12
|
+
export function asNumber(x) {
|
|
13
|
+
if (typeof x === "number")
|
|
14
|
+
return x;
|
|
15
|
+
if (typeof x === "string") {
|
|
16
|
+
const cleaned = x.replace(/,/g, "");
|
|
17
|
+
const n = Number(cleaned);
|
|
18
|
+
return Number.isFinite(n) ? n : null;
|
|
19
|
+
}
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
export function asBool(x) {
|
|
23
|
+
if (typeof x === "boolean")
|
|
24
|
+
return x;
|
|
25
|
+
if (x === "true")
|
|
26
|
+
return true;
|
|
27
|
+
if (x === "false")
|
|
28
|
+
return false;
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
export function asMedal(x) {
|
|
32
|
+
if (x === "BADGE_GOLD")
|
|
33
|
+
return "GOLD";
|
|
34
|
+
if (x === "BADGE_SILVER")
|
|
35
|
+
return "SILVER";
|
|
36
|
+
if (x === "BADGE_BRONZE")
|
|
37
|
+
return "BRONZE";
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
export function findFirst(node, predicate) {
|
|
41
|
+
if (isObject(node)) {
|
|
42
|
+
if (predicate(node))
|
|
43
|
+
return node;
|
|
44
|
+
for (const v of Object.values(node)) {
|
|
45
|
+
const found = findFirst(v, predicate);
|
|
46
|
+
if (found)
|
|
47
|
+
return found;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
else if (Array.isArray(node)) {
|
|
51
|
+
for (const v of node) {
|
|
52
|
+
const found = findFirst(v, predicate);
|
|
53
|
+
if (found)
|
|
54
|
+
return found;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
export function findAll(node, predicate) {
|
|
60
|
+
const out = [];
|
|
61
|
+
if (isObject(node)) {
|
|
62
|
+
if (predicate(node))
|
|
63
|
+
out.push(node);
|
|
64
|
+
for (const v of Object.values(node))
|
|
65
|
+
out.push(...findAll(v, predicate));
|
|
66
|
+
}
|
|
67
|
+
else if (Array.isArray(node)) {
|
|
68
|
+
for (const v of node)
|
|
69
|
+
out.push(...findAll(v, predicate));
|
|
70
|
+
}
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
73
|
+
export function findByType(node, type) {
|
|
74
|
+
return findFirst(node, (n) => n.type === type);
|
|
75
|
+
}
|
|
76
|
+
export function findAllByType(node, type) {
|
|
77
|
+
return findAll(node, (n) => n.type === type);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Find a GRAPHING_CARD whose content.title contains the given substring (case-insensitive).
|
|
81
|
+
* Used by recovery / strain / trend projections that identify cards by display title.
|
|
82
|
+
*/
|
|
83
|
+
export function findCardByTitle(node, titleSubstr) {
|
|
84
|
+
const upper = titleSubstr.toUpperCase();
|
|
85
|
+
return findFirst(node, (n) => {
|
|
86
|
+
if (n.type !== "GRAPHING_CARD")
|
|
87
|
+
return false;
|
|
88
|
+
const content = isObject(n.content) ? n.content : null;
|
|
89
|
+
const title = content && typeof content.title === "string" ? content.title : "";
|
|
90
|
+
return title.toUpperCase().includes(upper);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Find a DETAILS_GRAPHING_CARD whose content.card_title contains the substring.
|
|
95
|
+
*/
|
|
96
|
+
export function findDetailsCardByTitle(node, titleSubstr) {
|
|
97
|
+
const upper = titleSubstr.toUpperCase();
|
|
98
|
+
return findFirst(node, (n) => {
|
|
99
|
+
if (n.type !== "DETAILS_GRAPHING_CARD")
|
|
100
|
+
return false;
|
|
101
|
+
const content = isObject(n.content) ? n.content : null;
|
|
102
|
+
const title = content && typeof content.card_title === "string" ? content.card_title : "";
|
|
103
|
+
return title.toUpperCase().includes(upper);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Extract today's value from a GRAPHING_CARD. Handles two plot shapes:
|
|
108
|
+
* - segments[].points[last].graph_label.label (line plots)
|
|
109
|
+
* - bar_groups[last].top_label.label (bar plots)
|
|
110
|
+
* Returns the label as a string (often has units like "78%" or "1:41" or "4,880").
|
|
111
|
+
*/
|
|
112
|
+
export function latestGraphLabel(card) {
|
|
113
|
+
if (!card)
|
|
114
|
+
return null;
|
|
115
|
+
const content = isObject(card.content) ? card.content : {};
|
|
116
|
+
const graph = isObject(content.graph) ? content.graph : {};
|
|
117
|
+
const plots = asArray(graph.plots);
|
|
118
|
+
for (const p of plots) {
|
|
119
|
+
if (!isObject(p))
|
|
120
|
+
continue;
|
|
121
|
+
const plot = isObject(p.plot) ? p.plot : null;
|
|
122
|
+
if (!plot)
|
|
123
|
+
continue;
|
|
124
|
+
// Bar plot
|
|
125
|
+
const bars = asArray(plot.bar_groups);
|
|
126
|
+
if (bars.length > 0) {
|
|
127
|
+
const last = bars[bars.length - 1];
|
|
128
|
+
if (isObject(last)) {
|
|
129
|
+
const topLabel = isObject(last.top_label) ? last.top_label : null;
|
|
130
|
+
const label = topLabel && asString(topLabel.label);
|
|
131
|
+
if (label)
|
|
132
|
+
return label;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
// Line plot
|
|
136
|
+
const segments = asArray(plot.segments);
|
|
137
|
+
for (let i = segments.length - 1; i >= 0; i--) {
|
|
138
|
+
const seg = segments[i];
|
|
139
|
+
if (!isObject(seg))
|
|
140
|
+
continue;
|
|
141
|
+
const points = asArray(seg.points);
|
|
142
|
+
for (let j = points.length - 1; j >= 0; j--) {
|
|
143
|
+
const pt = points[j];
|
|
144
|
+
if (!isObject(pt))
|
|
145
|
+
continue;
|
|
146
|
+
const graphLabel = isObject(pt.graph_label) ? pt.graph_label : null;
|
|
147
|
+
const label = graphLabel && asString(graphLabel.label);
|
|
148
|
+
if (label)
|
|
149
|
+
return label;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Extract numeric value from a label like "78%", "42", "14.7", "1,792".
|
|
157
|
+
* Strips trailing % and commas. Returns null for time labels like "1:41".
|
|
158
|
+
*/
|
|
159
|
+
export function labelToNumber(label) {
|
|
160
|
+
if (label === null)
|
|
161
|
+
return null;
|
|
162
|
+
// Time format like "7:24" or "1:41" — not a number
|
|
163
|
+
if (/^\d+:\d+$/.test(label))
|
|
164
|
+
return null;
|
|
165
|
+
const cleaned = label.replace(/,/g, "").replace(/%$/, "");
|
|
166
|
+
const n = Number(cleaned);
|
|
167
|
+
return Number.isFinite(n) ? n : null;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Convert "H:MM" or "HH:MM" to milliseconds.
|
|
171
|
+
*/
|
|
172
|
+
export function timeLabelToMs(label) {
|
|
173
|
+
if (label === null)
|
|
174
|
+
return null;
|
|
175
|
+
const m = label.match(/^(\d+):(\d+)$/);
|
|
176
|
+
if (!m)
|
|
177
|
+
return null;
|
|
178
|
+
const hours = Number(m[1]);
|
|
179
|
+
const minutes = Number(m[2]);
|
|
180
|
+
if (!Number.isFinite(hours) || !Number.isFinite(minutes))
|
|
181
|
+
return null;
|
|
182
|
+
return (hours * 3600 + minutes * 60) * 1000;
|
|
183
|
+
}
|
|
184
|
+
export function cardPoints(card) {
|
|
185
|
+
if (!card)
|
|
186
|
+
return [];
|
|
187
|
+
const content = isObject(card.content) ? card.content : {};
|
|
188
|
+
const graph = isObject(content.graph) ? content.graph : {};
|
|
189
|
+
const out = [];
|
|
190
|
+
for (const p of asArray(graph.plots)) {
|
|
191
|
+
if (!isObject(p))
|
|
192
|
+
continue;
|
|
193
|
+
const plot = isObject(p.plot) ? p.plot : null;
|
|
194
|
+
if (!plot)
|
|
195
|
+
continue;
|
|
196
|
+
for (const seg of asArray(plot.segments)) {
|
|
197
|
+
if (!isObject(seg))
|
|
198
|
+
continue;
|
|
199
|
+
for (const pt of asArray(seg.points)) {
|
|
200
|
+
if (!isObject(pt))
|
|
201
|
+
continue;
|
|
202
|
+
const label = isObject(pt.graph_label) ? asString(pt.graph_label.label) : null;
|
|
203
|
+
const dsd = isObject(pt.data_scrubber_details) ? pt.data_scrubber_details : {};
|
|
204
|
+
out.push({
|
|
205
|
+
date: asString(dsd.primary_contextual_display),
|
|
206
|
+
value: labelToNumber(label) ?? asNumber(dsd.value),
|
|
207
|
+
label,
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
for (const group of asArray(plot.bar_groups)) {
|
|
212
|
+
if (!isObject(group))
|
|
213
|
+
continue;
|
|
214
|
+
const topLabel = isObject(group.top_label) ? asString(group.top_label.label) : null;
|
|
215
|
+
const dsd = isObject(group.data_scrubber_details) ? group.data_scrubber_details : {};
|
|
216
|
+
out.push({
|
|
217
|
+
date: asString(dsd.primary_contextual_display),
|
|
218
|
+
value: labelToNumber(topLabel),
|
|
219
|
+
label: topLabel,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return out;
|
|
224
|
+
}
|
|
225
|
+
function extractSet(row) {
|
|
226
|
+
if (!isObject(row))
|
|
227
|
+
return null;
|
|
228
|
+
const items = asArray(row.items);
|
|
229
|
+
if (items.length < 2)
|
|
230
|
+
return null;
|
|
231
|
+
const repsCell = items[0];
|
|
232
|
+
const weightCell = items[1];
|
|
233
|
+
if (!isObject(repsCell) || !isObject(weightCell))
|
|
234
|
+
return null;
|
|
235
|
+
const reps = asNumber(repsCell.value);
|
|
236
|
+
if (reps === null)
|
|
237
|
+
return null;
|
|
238
|
+
const weightOrTimeUnits = asString(weightCell.units);
|
|
239
|
+
const weightOrTimeValue = asNumber(weightCell.value);
|
|
240
|
+
const isTime = weightOrTimeUnits === "sec" || weightOrTimeUnits === "s";
|
|
241
|
+
return {
|
|
242
|
+
reps,
|
|
243
|
+
weight: isTime ? null : weightOrTimeValue,
|
|
244
|
+
units: isTime ? null : weightOrTimeUnits,
|
|
245
|
+
time_seconds: isTime ? (weightOrTimeValue !== null ? Math.round(weightOrTimeValue) : null) : null,
|
|
246
|
+
medal: asMedal(weightCell.achievement_icon),
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
function extractBreakdownSets(node) {
|
|
250
|
+
if (!isObject(node))
|
|
251
|
+
return [];
|
|
252
|
+
if (node.type !== "EXERCISE_BREAKDOWN")
|
|
253
|
+
return [];
|
|
254
|
+
const content = isObject(node.content) ? node.content : {};
|
|
255
|
+
return asArray(content.rows)
|
|
256
|
+
.map(extractSet)
|
|
257
|
+
.filter((s) => s !== null);
|
|
258
|
+
}
|
|
259
|
+
export function extractSession(card) {
|
|
260
|
+
if (!isObject(card))
|
|
261
|
+
return null;
|
|
262
|
+
const content = isObject(card.content) ? card.content : {};
|
|
263
|
+
let headerContent = null;
|
|
264
|
+
if (isObject(content.header_content)) {
|
|
265
|
+
const hc = content.header_content;
|
|
266
|
+
headerContent = isObject(hc.content) ? hc.content : hc;
|
|
267
|
+
}
|
|
268
|
+
if (!headerContent)
|
|
269
|
+
return null;
|
|
270
|
+
const recordDate = asString(headerContent.record_date) ?? "";
|
|
271
|
+
const subtitle = isObject(headerContent.record_subtitle) ? headerContent.record_subtitle : {};
|
|
272
|
+
const title = isObject(headerContent.record_title) ? headerContent.record_title : {};
|
|
273
|
+
const medal = asMedal(headerContent.achievement_icon);
|
|
274
|
+
const reps = asNumber(subtitle.value) ?? 0;
|
|
275
|
+
const weight = asNumber(title.value);
|
|
276
|
+
const units = asString(title.unit);
|
|
277
|
+
const blocks = asArray(content.expanded_content);
|
|
278
|
+
const breakdowns = blocks.filter((b) => isObject(b) && b.type === "EXERCISE_BREAKDOWN");
|
|
279
|
+
const sets = breakdowns.length > 0 ? extractBreakdownSets(breakdowns[0]) : [];
|
|
280
|
+
let totalVolume = null;
|
|
281
|
+
let totalVolumeUnits = null;
|
|
282
|
+
if (breakdowns.length >= 2) {
|
|
283
|
+
const second = breakdowns[1];
|
|
284
|
+
const secondContent = isObject(second.content) ? second.content : {};
|
|
285
|
+
const rows = asArray(secondContent.rows);
|
|
286
|
+
const totalRow = rows[0];
|
|
287
|
+
if (isObject(totalRow)) {
|
|
288
|
+
const items = asArray(totalRow.items);
|
|
289
|
+
if (items.length >= 2 && isObject(items[1])) {
|
|
290
|
+
totalVolume = asNumber(items[1].value);
|
|
291
|
+
totalVolumeUnits = asString(items[1].units);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
let activityId = null;
|
|
296
|
+
for (const b of blocks) {
|
|
297
|
+
if (!isObject(b) || b.type !== "CARD_BUTTON")
|
|
298
|
+
continue;
|
|
299
|
+
const bContent = isObject(b.content) ? b.content : {};
|
|
300
|
+
const dest = isObject(bContent.destination) ? bContent.destination : null;
|
|
301
|
+
const params = dest && isObject(dest.parameters) ? dest.parameters : null;
|
|
302
|
+
if (params && typeof params.activity_id === "string") {
|
|
303
|
+
activityId = params.activity_id;
|
|
304
|
+
break;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
return {
|
|
308
|
+
date: recordDate,
|
|
309
|
+
top_set: { reps, weight, units, medal },
|
|
310
|
+
sets,
|
|
311
|
+
total_volume: totalVolume,
|
|
312
|
+
total_volume_units: totalVolumeUnits,
|
|
313
|
+
activity_id: activityId,
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
export function extractSessions(response) {
|
|
317
|
+
if (!isObject(response))
|
|
318
|
+
return [];
|
|
319
|
+
return asArray(response.items)
|
|
320
|
+
.map(extractSession)
|
|
321
|
+
.filter((s) => s !== null);
|
|
322
|
+
}
|
|
323
|
+
export function extractPrTiles(response) {
|
|
324
|
+
if (!isObject(response))
|
|
325
|
+
return [];
|
|
326
|
+
return asArray(response.tiles)
|
|
327
|
+
.map((t) => {
|
|
328
|
+
if (!isObject(t))
|
|
329
|
+
return null;
|
|
330
|
+
const exerciseId = asString(t.exercise_id);
|
|
331
|
+
const name = asString(t.name);
|
|
332
|
+
if (!exerciseId || !name)
|
|
333
|
+
return null;
|
|
334
|
+
return {
|
|
335
|
+
exercise_id: exerciseId,
|
|
336
|
+
name,
|
|
337
|
+
muscle_groups: asArray(t.muscle_groups).filter((x) => typeof x === "string"),
|
|
338
|
+
equipment: asString(t.equipment) ?? "",
|
|
339
|
+
pr_value: asNumber(t.volume_input_value),
|
|
340
|
+
pr_units: asString(t.volume_input_units),
|
|
341
|
+
image_url: asString(t.image_url),
|
|
342
|
+
custom_exercise: t.custom_exercise === true,
|
|
343
|
+
};
|
|
344
|
+
})
|
|
345
|
+
.filter((t) => t !== null);
|
|
346
|
+
}
|
|
347
|
+
export function extractGraphPoints(node) {
|
|
348
|
+
if (!isObject(node))
|
|
349
|
+
return [];
|
|
350
|
+
let graph = node;
|
|
351
|
+
if (isObject(node.graph))
|
|
352
|
+
graph = node.graph;
|
|
353
|
+
const plots = asArray(graph.plots);
|
|
354
|
+
const points = [];
|
|
355
|
+
for (const p of plots) {
|
|
356
|
+
if (!isObject(p))
|
|
357
|
+
continue;
|
|
358
|
+
const plot = isObject(p.plot) ? p.plot : null;
|
|
359
|
+
if (!plot)
|
|
360
|
+
continue;
|
|
361
|
+
for (const seg of asArray(plot.segments)) {
|
|
362
|
+
if (!isObject(seg))
|
|
363
|
+
continue;
|
|
364
|
+
for (const pt of asArray(seg.points)) {
|
|
365
|
+
if (!isObject(pt))
|
|
366
|
+
continue;
|
|
367
|
+
const dsd = isObject(pt.data_scrubber_details) ? pt.data_scrubber_details : {};
|
|
368
|
+
points.push({
|
|
369
|
+
primary_label: asString(dsd.primary_contextual_display),
|
|
370
|
+
secondary_label: asString(dsd.secondary_contextual_display),
|
|
371
|
+
value: asNumber(dsd.value),
|
|
372
|
+
value_display: asString(dsd.value_display),
|
|
373
|
+
unit: asString(dsd.unit_display),
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
for (const group of asArray(plot.bar_groups)) {
|
|
378
|
+
if (!isObject(group))
|
|
379
|
+
continue;
|
|
380
|
+
for (const bar of asArray(group.bars)) {
|
|
381
|
+
if (!isObject(bar))
|
|
382
|
+
continue;
|
|
383
|
+
const dsd = isObject(bar.data_scrubber_details) ? bar.data_scrubber_details : {};
|
|
384
|
+
points.push({
|
|
385
|
+
primary_label: asString(dsd.primary_contextual_display),
|
|
386
|
+
secondary_label: asString(dsd.secondary_contextual_display),
|
|
387
|
+
value: asNumber(dsd.value),
|
|
388
|
+
value_display: asString(dsd.value_display),
|
|
389
|
+
unit: asString(dsd.unit_display),
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
return points;
|
|
395
|
+
}
|
|
396
|
+
//# sourceMappingURL=walk.js.map
|