From 8c929fc554ae541d3e7781cbfd4b1acf8eb564e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Bombeck?= Date: Thu, 14 May 2026 17:09:46 +0200 Subject: [PATCH] refactor(apple-health): collapse sleep branch in mapAppleHealthEntry Builds the base output object once with the four shared fields and appends sleepStage only when the mapping carries a sleep-stage table. Drops one duplicated object literal and one return path; the function keeps the same null-on-bad-input semantics for unmapped, invalid date, missing stage, and unknown stage cases. --- src/lib/measurements/apple-health-mapping.ts | 24 ++++++++------------ 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/lib/measurements/apple-health-mapping.ts b/src/lib/measurements/apple-health-mapping.ts index eaf7d9511..f642e94d2 100644 --- a/src/lib/measurements/apple-health-mapping.ts +++ b/src/lib/measurements/apple-health-mapping.ts @@ -438,23 +438,19 @@ export function mapAppleHealthEntry( const value = mapping.convertToDbUnit(input.value); - if (mapping.sleepStageMap !== undefined) { - if (input.sleepStage === undefined) return null; - const stage = mapping.sleepStageMap[input.sleepStage]; - if (!stage) return null; - return { - type: mapping.measurementType, - value, - unit: mapping.dbUnit, - takenAt, - sleepStage: stage, - }; - } - - return { + const out: AppleHealthEntryOutput = { type: mapping.measurementType, value, unit: mapping.dbUnit, takenAt, }; + + if (mapping.sleepStageMap) { + if (input.sleepStage === undefined) return null; + const stage = mapping.sleepStageMap[input.sleepStage]; + if (!stage) return null; + out.sleepStage = stage; + } + + return out; }