1export function padStart(number, digits = 2, emptyDigit = 0) {
2 let length = 0;
3 let n = Math.abs(number);
4 let absoluteNumber = n;
5 do {
6 n /= 10;
7 length++;
8 } while (n >= 1);
9 const prefix = Array(Math.max((digits - length) + 1, 0)).join(emptyDigit);
10 return number < 0 ? `-${prefix}${absoluteNumber}` : prefix + number;
11}
12
13export default padStart;
14
1var n = 123
2
3String("00000" + n).slice(-5); // returns 00123
4("00000" + n).slice(-5); // returns 00123
5(" " + n).slice(-5); // returns " 123" (with two spaces)