Skip to content

Commit f10f569

Browse files
authored
perf(utils): replace pad with String.padStart (#3371)
1 parent 3c48f22 commit f10f569

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

packages/pg/lib/utils.js

+15-23
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,6 @@ function prepareObject(val, seen) {
8888
return JSON.stringify(val)
8989
}
9090

91-
function pad(number, digits) {
92-
number = '' + number
93-
while (number.length < digits) {
94-
number = '0' + number
95-
}
96-
return number
97-
}
98-
9991
function dateToString(date) {
10092
var offset = -date.getTimezoneOffset()
10193

@@ -104,19 +96,19 @@ function dateToString(date) {
10496
if (isBCYear) year = Math.abs(year) + 1 // negative years are 1 off their BC representation
10597

10698
var ret =
107-
pad(year, 4) +
99+
String(year).padStart(4, '0') +
108100
'-' +
109-
pad(date.getMonth() + 1, 2) +
101+
String(date.getMonth() + 1).padStart(2, '0') +
110102
'-' +
111-
pad(date.getDate(), 2) +
103+
String(date.getDate()).padStart(2, '0') +
112104
'T' +
113-
pad(date.getHours(), 2) +
105+
String(date.getHours()).padStart(2, '0') +
114106
':' +
115-
pad(date.getMinutes(), 2) +
107+
String(date.getMinutes()).padStart(2, '0') +
116108
':' +
117-
pad(date.getSeconds(), 2) +
109+
String(date.getSeconds()).padStart(2, '0') +
118110
'.' +
119-
pad(date.getMilliseconds(), 3)
111+
String(date.getMilliseconds()).padStart(3, '0')
120112

121113
if (offset < 0) {
122114
ret += '-'
@@ -125,7 +117,7 @@ function dateToString(date) {
125117
ret += '+'
126118
}
127119

128-
ret += pad(Math.floor(offset / 60), 2) + ':' + pad(offset % 60, 2)
120+
ret += String(Math.floor(offset / 60)).padStart(2, '0') + ':' + String(offset % 60).padStart(2, '0')
129121
if (isBCYear) ret += ' BC'
130122
return ret
131123
}
@@ -136,19 +128,19 @@ function dateToStringUTC(date) {
136128
if (isBCYear) year = Math.abs(year) + 1 // negative years are 1 off their BC representation
137129

138130
var ret =
139-
pad(year, 4) +
131+
String(year).padStart(4, '0') +
140132
'-' +
141-
pad(date.getUTCMonth() + 1, 2) +
133+
String(date.getUTCMonth() + 1).padStart(2, '0') +
142134
'-' +
143-
pad(date.getUTCDate(), 2) +
135+
String(date.getUTCDate()).padStart(2, '0') +
144136
'T' +
145-
pad(date.getUTCHours(), 2) +
137+
String(date.getUTCHours()).padStart(2, '0') +
146138
':' +
147-
pad(date.getUTCMinutes(), 2) +
139+
String(date.getUTCMinutes()).padStart(2, '0') +
148140
':' +
149-
pad(date.getUTCSeconds(), 2) +
141+
String(date.getUTCSeconds()).padStart(2, '0') +
150142
'.' +
151-
pad(date.getUTCMilliseconds(), 3)
143+
String(date.getUTCMilliseconds()).padStart(3, '0')
152144

153145
ret += '+00:00'
154146
if (isBCYear) ret += ' BC'

0 commit comments

Comments
 (0)