Skip to content

Commit

Permalink
perf(utils): replace pad with String.padStart (#3371)
Browse files Browse the repository at this point in the history
  • Loading branch information
cesco69 authored Feb 7, 2025
1 parent 3c48f22 commit f10f569
Showing 1 changed file with 15 additions and 23 deletions.
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

0 comments on commit f10f569

Please sign in to comment.