Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

perf(utils): replace pad with String.padStart #3371

Merged
merged 2 commits into from
Feb 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 15 additions & 23 deletions packages/pg/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,6 @@ function prepareObject(val, seen) {
return JSON.stringify(val)
}

function pad(number, digits) {
number = '' + number
while (number.length < digits) {
number = '0' + number
}
return number
}

function dateToString(date) {
var offset = -date.getTimezoneOffset()

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

var ret =
pad(year, 4) +
String(year).padStart(4, '0') +
'-' +
pad(date.getMonth() + 1, 2) +
String(date.getMonth() + 1).padStart(2, '0') +
'-' +
pad(date.getDate(), 2) +
String(date.getDate()).padStart(2, '0') +
'T' +
pad(date.getHours(), 2) +
String(date.getHours()).padStart(2, '0') +
':' +
pad(date.getMinutes(), 2) +
String(date.getMinutes()).padStart(2, '0') +
':' +
pad(date.getSeconds(), 2) +
String(date.getSeconds()).padStart(2, '0') +
'.' +
pad(date.getMilliseconds(), 3)
String(date.getMilliseconds()).padStart(3, '0')

if (offset < 0) {
ret += '-'
Expand All @@ -125,7 +117,7 @@ function dateToString(date) {
ret += '+'
}

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

var ret =
pad(year, 4) +
String(year).padStart(4, '0') +
'-' +
pad(date.getUTCMonth() + 1, 2) +
String(date.getUTCMonth() + 1).padStart(2, '0') +
'-' +
pad(date.getUTCDate(), 2) +
String(date.getUTCDate()).padStart(2, '0') +
'T' +
pad(date.getUTCHours(), 2) +
String(date.getUTCHours()).padStart(2, '0') +
':' +
pad(date.getUTCMinutes(), 2) +
String(date.getUTCMinutes()).padStart(2, '0') +
':' +
pad(date.getUTCSeconds(), 2) +
String(date.getUTCSeconds()).padStart(2, '0') +
'.' +
pad(date.getUTCMilliseconds(), 3)
String(date.getUTCMilliseconds()).padStart(3, '0')

ret += '+00:00'
if (isBCYear) ret += ' BC'
Expand Down