Replace moment with dayjs in sdc-populate - #2056
Open
KyleOps wants to merge 3 commits into
Open
Conversation
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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2052
momentwas used in exactly three places, all insidecheckIsDateTimeandconvertDateTimeToDateinconstructResponse.ts, anddayjswas already imported by the same file. The three calls now usedayjs,momentis removed frompackages/sdc-populateand fromapps/smart-forms-app(which listed it without importing it), and the root lockfile is regenerated.momentis in maintenance mode and was the heaviest dependency in the populate chain (5.2 MB installed versus 1.9 MB fordayjs), so this removes a dependency rather than swapping one for another.The module extends
dayjswith thecustomParseFormatplugin locally: strict parsing against a format list silently degrades to lenient parsing without it.Why a dependency swap ships a test file:
checkIsDateTimedecides whether a populated answer becomesvalueDateorvalueDateTime, and moment and dayjs strict parsing are not identical in general. The newconstructResponse.test.tslocks that discrimination across offsets, fractional seconds, out-of-range components, and non-ISO orderings. The file passes identically against the oldmomentimplementation and the newdayjsone, with the test run pinned toTZ=Australia/Sydney.Testing: full
sdc-populatesuite green (20 suites, 185 tests) and the renderer consumer suite green.Follow-up, not in this PR:
smart-forms-renderercarries an identicalcheckIsDateTimeincalculatedExpression.ts; consolidating the two copies is a separate change.