Skip to content

Commit

Permalink
Use trim
Browse files Browse the repository at this point in the history
  • Loading branch information
Giancarlo Anemone committed Mar 18, 2019
1 parent 055bf12 commit 59bb8d9
Showing 1 changed file with 8 additions and 25 deletions.
33 changes: 8 additions & 25 deletions packages/apollo-codegen-flow/src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,18 @@ import { stripIndent } from "common-tags";

type Printable = t.Node | string;

function trimLeadingEmptyLines(result: string) {
let hasSeenContent = false;
return result
.split("\n")
.filter(line => {
if (!hasSeenContent && line.trim() === "") {
return false;
}
hasSeenContent = true;
return true;
})
.join("\n");
}

export default class Printer {
private printQueue: Printable[] = [];

public print(): string {
return trimLeadingEmptyLines(this.printQueue.reduce(
(document: string, printable) => {
if (typeof printable === "string") {
return document + printable;
} else {
const documentPart = generate(printable).code;
return document + this.fixCommas(documentPart);
}
},
""
) as string);
return (this.printQueue.reduce((document: string, printable) => {
if (typeof printable === "string") {
return document + printable;
} else {
const documentPart = generate(printable).code;
return document + this.fixCommas(documentPart);
}
}, "") as string).trim();
}

public enqueue(printable: Printable) {
Expand Down

0 comments on commit 59bb8d9

Please sign in to comment.