From 85604f98063cdf3263ecac013544af4a6a441bd3 Mon Sep 17 00:00:00 2001 From: koding88 Date: Mon, 24 Aug 2026 23:04:37 +0700 Subject: [PATCH] fix(core): treat mixed-case ms unit as millisecond instead of minute prettyUnit consulted the special map before lowercasing, then stripped a trailing 's', so 'MS'/'Ms'/'mS' resolved to the string 'm'. Consequences while every other unit was case-insensitive: - get('MS') threw TypeError (this.m is not a function) - set('MS', n) was a silent no-op - startOf/endOf('MS') were clone-only no-ops Look up the special map again after lowercasing so mixed-case ms behaves exactly like 'ms'. --- src/utils.js | 3 ++- test/get-set.test.js | 9 +++++++++ test/utils.test.js | 3 +++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/utils.js b/src/utils.js index 324bc5e11..f3e4c20b3 100644 --- a/src/utils.js +++ b/src/utils.js @@ -40,7 +40,8 @@ const prettyUnit = (u) => { ms: C.MS, Q: C.Q } - return special[u] || String(u || '').toLowerCase().replace(/s$/, '') + const lower = String(u || '').toLowerCase() + return special[u] || special[lower] || lower.replace(/s$/, '') } const isUndefined = s => s === undefined diff --git a/test/get-set.test.js b/test/get-set.test.js index dae4d476c..23b04bebd 100644 --- a/test/get-set.test.js +++ b/test/get-set.test.js @@ -64,6 +64,15 @@ it('Millisecond', () => { expect(dayjs().millisecond()).toBe(moment().millisecond()) expect(dayjs().millisecond(0).valueOf()).toBe(moment().millisecond(0).valueOf()) expect(dayjs().millisecond(1).valueOf()).toBe(moment().millisecond(1).valueOf()) + expect(dayjs().get('MS')).toBe(dayjs().get('ms')) +}) + +it('Set Millisecond with short and mixed-case units', () => { + const d = dayjs('2024-06-15T12:34:56.789') + expect(d.get('MS')).toBe(789) + expect(d.set('MS', 500).millisecond()).toBe(500) + expect(d.set('Ms', 250).millisecond()).toBe(250) + expect(d.startOf('MS').valueOf()).toBe(d.startOf('ms').valueOf()) }) it('Set Day', () => { diff --git a/test/utils.test.js b/test/utils.test.js index 0d7e6e044..d40ebc36a 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -18,6 +18,9 @@ it('PrettyUnit', () => { expect(prettyUnit('m')).toBe('minute') expect(prettyUnit('s')).toBe('second') expect(prettyUnit('ms')).toBe('millisecond') + expect(prettyUnit('MS')).toBe('millisecond') + expect(prettyUnit('Ms')).toBe('millisecond') + expect(prettyUnit('mS')).toBe('millisecond') expect(prettyUnit('Q')).toBe('quarter') expect(prettyUnit()).toBe('') })