修复 src/utils.js 中 monthDiff 的 || 0 兜底,使 .diff(other, 'year'|'month', true) 在任一操作数为 Invalid Date 时… - #3218
Closed
znx-sys wants to merge 1 commit into
Closed
修复 src/utils.js 中 monthDiff 的 || 0 兜底,使 .diff(other, 'year'|'month', true) 在任一操作数为 Invalid Date 时…#3218znx-sys wants to merge 1 commit into
znx-sys wants to merge 1 commit into
Conversation
…true) 在任一操作数为 Invalid Date 时返回 NaN,相关单测通过
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.
PR: 修复 monthDiff 的
|| 0兜底,使 Invalid Date 下 month/year diff 返回 NaN背景
dayjs('invalid').diff(other, 'month' | 'year', true)当前返回0而非NaN,掩盖了无效日期比较。day/hour 等基于毫秒的 diff 路径在同样输入下已正确返回 NaN,唯独 month/year(及派生的 quarter)路径表现不一致。根因
src/utils.js的monthDiff(原 24-25 行)返回表达式为:Invalid Date 时
$y/$M为 NaN,整条数值链(wholeMonthDiff、anchor、除法结果)均为 NaN,最终被|| 0兜底为0。NaN 在该链中本可正常传播(absFloor(NaN)返回 NaN,不阻断;utc/timezone 插件 diff 均纯透传至此),|| 0是该路径上唯一的 NaN 吞噬点,修复点唯一。修改点
1.
src/utils.js(monthDiff 返回表达式,仅此一处)|| 0兜底,NaN 得以透传;-0,实测(jest 22.4.4)expect(-0).toEqual(0)失败,会打破现有display.test.js:215/218。以+ 0归一化(IEEE:-0 + 0 = +0、NaN + 0 = NaN、x + 0 = x精确),与 moment 的-0归一化语义一致。absFloor(其|| 0仅处理-0,不影响 NaN 传播)、未改 API 签名、未改毫秒路径与插件透传。2.
test/display.test.js(新增 1 个用例)使用
toBeNaN()而非toBe(NaN)(后者对 NaN 恒失败)。置于现有describe('Difference')内。范围外(随修复自然变化,非独立改动):quarter(
getMonth()/3)与float=false(经 absFloor,Math.floor(NaN)=NaN)路径随 monthDiff 修复同样由 0 变 NaN,属派生行为,测试通过。按任务边界不处理duration.humanize()的 "a month" 症状(PR #3024 已处理)。验证证据
Utils.m(dayjs('invalid'), now) === 0、diff('month'|'year', true) === 0直连断言复现成功;参考用例输出快照存baseline-before.txt。baseline-after.txt)逐字节一致(含负值/跨年/月末锚点/同瞬时刻);9 行 INVALID 全部由 0 变 NaN。npm test退出码 0,93 套件 / 795 测试全部通过(coverage 全绿,UTC 等时区分段亦通过)。(注:执行者自述的 94/796 与测试报告的 93/795 存在数量差异,Reviewer 已核实为环境段落拆分所致,T5 分段等价复跑确认;本 PR 以测试报告的 93/795 为准。)+0。风险
diff('month'/'year')在 Invalid Date 输入下返回 0 的下游代码会观察到返回值变为 NaN——这是本次修复的目的语义,但属于可见行为变化。float=false路径的 NaN 化为派生效果,如上游合并需一并知悉。|| 0,本修复对上游有参考价值(仅上报,未提交上游)。未尽事项
Fixes #3186