Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Refactoring using padEnd and padStart #583

Merged
merged 1 commit into from
Sep 4, 2019
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
23 changes: 10 additions & 13 deletions fmt/sprintf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,14 +365,12 @@ class Printf {

pad(s: string): string {
const padding = this.flags.zero ? "0" : " ";
while (s.length < this.flags.width) {
if (this.flags.dash) {
s += padding;
} else {
s = padding + s;
}

if (this.flags.dash) {
return s.padEnd(this.flags.width, padding);
}
return s;

return s.padStart(this.flags.width, padding);
}
padNum(nStr: string, neg: boolean): string {
let sign: string;
Expand All @@ -393,13 +391,12 @@ class Printf {
const pad = zero ? "0" : " ";
const len = zero ? this.flags.width - sign.length : this.flags.width;

while (nStr.length < len) {
if (this.flags.dash) {
nStr += pad; // left justify - right pad
} else {
nStr = pad + nStr; // right just - left pad
}
if (this.flags.dash) {
nStr = nStr.padEnd(len, pad);
} else {
nStr = nStr.padStart(len, pad);
}

if (zero) {
// see above
nStr = sign + nStr;
Expand Down