Skip to content

Commit 2d2a2e6

Browse files
fernandodevelonohsory1324
authored andcommitted
fix: Optimize format method (iamkun#2313)
1 parent de9fade commit 2d2a2e6

File tree

1 file changed

+55
-26
lines changed

1 file changed

+55
-26
lines changed

src/index.js

+55-26
Original file line numberDiff line numberDiff line change
@@ -278,34 +278,63 @@ class Dayjs {
278278
return isLowercase ? m.toLowerCase() : m
279279
})
280280

281-
const matches = {
282-
YY: String(this.$y).slice(-2),
283-
YYYY: Utils.s(this.$y, 4, '0'),
284-
M: $M + 1,
285-
MM: Utils.s($M + 1, 2, '0'),
286-
MMM: getShort(locale.monthsShort, $M, months, 3),
287-
MMMM: getShort(months, $M),
288-
D: this.$D,
289-
DD: Utils.s(this.$D, 2, '0'),
290-
d: String(this.$W),
291-
dd: getShort(locale.weekdaysMin, this.$W, weekdays, 2),
292-
ddd: getShort(locale.weekdaysShort, this.$W, weekdays, 3),
293-
dddd: weekdays[this.$W],
294-
H: String($H),
295-
HH: Utils.s($H, 2, '0'),
296-
h: get$H(1),
297-
hh: get$H(2),
298-
a: meridiemFunc($H, $m, true),
299-
A: meridiemFunc($H, $m, false),
300-
m: String($m),
301-
mm: Utils.s($m, 2, '0'),
302-
s: String(this.$s),
303-
ss: Utils.s(this.$s, 2, '0'),
304-
SSS: Utils.s(this.$ms, 3, '0'),
305-
Z: zoneStr // 'ZZ' logic below
281+
const matches = (match) => {
282+
switch (match) {
283+
case 'YY':
284+
return String(this.$y).slice(-2)
285+
case 'YYYY':
286+
return Utils.s(this.$y, 4, '0')
287+
case 'M':
288+
return $M + 1
289+
case 'MM':
290+
return Utils.s($M + 1, 2, '0')
291+
case 'MMM':
292+
return getShort(locale.monthsShort, $M, months, 3)
293+
case 'MMMM':
294+
return getShort(months, $M)
295+
case 'D':
296+
return this.$D
297+
case 'DD':
298+
return Utils.s(this.$D, 2, '0')
299+
case 'd':
300+
return String(this.$W)
301+
case 'dd':
302+
return getShort(locale.weekdaysMin, this.$W, weekdays, 2)
303+
case 'ddd':
304+
return getShort(locale.weekdaysShort, this.$W, weekdays, 3)
305+
case 'dddd':
306+
return weekdays[this.$W]
307+
case 'H':
308+
return String($H)
309+
case 'HH':
310+
return Utils.s($H, 2, '0')
311+
case 'h':
312+
return get$H(1)
313+
case 'hh':
314+
return get$H(2)
315+
case 'a':
316+
return meridiemFunc($H, $m, true)
317+
case 'A':
318+
return meridiemFunc($H, $m, false)
319+
case 'm':
320+
return String($m)
321+
case 'mm':
322+
return Utils.s($m, 2, '0')
323+
case 's':
324+
return String(this.$s)
325+
case 'ss':
326+
return Utils.s(this.$s, 2, '0')
327+
case 'SSS':
328+
return Utils.s(this.$ms, 3, '0')
329+
case 'Z':
330+
return zoneStr // 'ZZ' logic below
331+
default:
332+
break
333+
}
334+
return null
306335
}
307336

308-
return str.replace(C.REGEX_FORMAT, (match, $1) => $1 || matches[match] || zoneStr.replace(':', '')) // 'ZZ'
337+
return str.replace(C.REGEX_FORMAT, (match, $1) => $1 || matches(match) || zoneStr.replace(':', '')) // 'ZZ'
309338
}
310339

311340
utcOffset() {

0 commit comments

Comments
 (0)