Skip to content

Commit

Permalink
perf(format): reuse matches instead of created when replacing (#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
whatwewant authored and iamkun committed Feb 2, 2019
1 parent 1e185b5 commit 10b79d8
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,34 +272,36 @@ class Dayjs {
return Utils.padStart(this.$H < 13 ? this.$H : this.$H - 12, match === 'hh' ? 2 : 1, '0')
}

const matches = {
YY: String(this.$y).slice(-2),
YYYY: String(this.$y),
M: String(this.$M + 1),
MM: Utils.padStart(this.$M + 1, 2, '0'),
MMM: getShort(locale.monthsShort, this.$M, months, 3),
MMMM: months[this.$M],
D: String(this.$D),
DD: Utils.padStart(this.$D, 2, '0'),
d: String(this.$W),
dd: getShort(locale.weekdaysMin, this.$W, weekdays, 2),
ddd: getShort(locale.weekdaysShort, this.$W, weekdays, 3),
dddd: weekdays[this.$W],
H: String(this.$H),
HH: Utils.padStart(this.$H, 2, '0'),
h: get$H('h'),
hh: get$H('hh'),
a: this.$H < 12 ? 'am' : 'pm',
A: this.$H < 12 ? 'AM' : 'PM',
m: String(this.$m),
mm: Utils.padStart(this.$m, 2, '0'),
s: String(this.$s),
ss: Utils.padStart(this.$s, 2, '0'),
SSS: Utils.padStart(this.$ms, 3, '0'),
Z: zoneStr
}

return str.replace(C.REGEX_FORMAT, (match) => {
if (match.indexOf('[') > -1) return match.replace(/\[|\]/g, '')
return {
YY: String(this.$y).slice(-2),
YYYY: String(this.$y),
M: String(this.$M + 1),
MM: Utils.padStart(this.$M + 1, 2, '0'),
MMM: getShort(locale.monthsShort, this.$M, months, 3),
MMMM: months[this.$M],
D: String(this.$D),
DD: Utils.padStart(this.$D, 2, '0'),
d: String(this.$W),
dd: getShort(locale.weekdaysMin, this.$W, weekdays, 2),
ddd: getShort(locale.weekdaysShort, this.$W, weekdays, 3),
dddd: weekdays[this.$W],
H: String(this.$H),
HH: Utils.padStart(this.$H, 2, '0'),
h: get$H(match),
hh: get$H(match),
a: this.$H < 12 ? 'am' : 'pm',
A: this.$H < 12 ? 'AM' : 'PM',
m: String(this.$m),
mm: Utils.padStart(this.$m, 2, '0'),
s: String(this.$s),
ss: Utils.padStart(this.$s, 2, '0'),
SSS: Utils.padStart(this.$ms, 3, '0'),
Z: zoneStr
}[match] || zoneStr.replace(':', '') // 'ZZ'
return matches[match] || zoneStr.replace(':', '') // 'ZZ'
})
}

Expand Down

0 comments on commit 10b79d8

Please sign in to comment.