Skip to content

Replace moment with dayjs in sdc-populate - #2056

Open
KyleOps wants to merge 3 commits into
mainfrom
issue/2052
Open

Replace moment with dayjs in sdc-populate#2056
KyleOps wants to merge 3 commits into
mainfrom
issue/2052

Conversation

@KyleOps

@KyleOps KyleOps commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Fixes #2052

moment was used in exactly three places, all inside checkIsDateTime and convertDateTimeToDate in constructResponse.ts, and dayjs was already imported by the same file. The three calls now use dayjs, moment is removed from packages/sdc-populate and from apps/smart-forms-app (which listed it without importing it), and the root lockfile is regenerated. moment is in maintenance mode and was the heaviest dependency in the populate chain (5.2 MB installed versus 1.9 MB for dayjs), so this removes a dependency rather than swapping one for another.

The module extends dayjs with the customParseFormat plugin locally: strict parsing against a format list silently degrades to lenient parsing without it.

Why a dependency swap ships a test file: checkIsDateTime decides whether a populated answer becomes valueDate or valueDateTime, and moment and dayjs strict parsing are not identical in general. The new constructResponse.test.ts locks that discrimination across offsets, fractional seconds, out-of-range components, and non-ISO orderings. The file passes identically against the old moment implementation and the new dayjs one, with the test run pinned to TZ=Australia/Sydney.

Testing: full sdc-populate suite green (20 suites, 185 tests) and the renderer consumer suite green.

Follow-up, not in this PR: smart-forms-renderer carries an identical checkIsDateTime in calculatedExpression.ts; consolidating the two copies is a separate change.

checkIsDateTime() and convertDateTimeToDate() in constructResponse.ts were
the only users of moment in the package, and both already used dayjs in the
same function. Carrying two date libraries for three lines is unnecessary, so
the moment calls now use dayjs and moment is dropped from dependencies.

Strict parsing against a list of accepted formats needs the dayjs
customParseFormat plugin. Without it dayjs ignores the format argument and
falls back to a lenient parse, which would widen what checkIsDateTime()
accepts, so the plugin is extended in the module rather than relying on a
consumer having extended it.

Add constructResponse.test.ts covering the date versus dateTime
discrimination that decides whether a populated answer becomes a valueDate or
a valueDateTime: empty and whitespace input, bare year, bare year-month,
date, ISO dateTime with a Z offset, a numeric offset, fractional seconds and
no offset, an offset that crosses a year boundary, non-ISO ordering,
time-only values, and out-of-range date components. The suite passes
unchanged against both the moment and the dayjs implementations.
KyleOps added 2 commits August 6, 2026 00:17
Nothing under apps/smart-forms-app/src imports moment, so the app was
carrying it for no reason. Removing it here finishes the moment removal
across the monorepo in one lockfile change.
The checkIsTime regex is untouched by the moment to dayjs swap, so the
parity suite stays scoped to the functions whose behaviour the swap
could change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sdc-populate depends on both moment and dayjs for three lines of date handling

1 participant