Drop .getLast().

This commit is contained in:
Logan Smyth 2016-07-17 10:32:09 -07:00
parent fdc5b7cb5d
commit 9f49c99774
2 changed files with 19 additions and 16 deletions

View File

@ -97,10 +97,23 @@ export default class Buffer {
if (this._queue.length > 0 && this._queue[0][0] === ";") this._queue.shift();
}
endsWith(str: string): boolean {
endsWith(suffix: string): boolean {
// Fast path to avoid iterating over this._queue.
if (suffix.length === 1) {
let last;
if (this._queue.length > 0) {
const str = this._queue[0][0];
last = str[str.length - 1];
} else {
last = this._last;
}
return last === suffix;
}
const end = this._last + this._queue.reduce((acc, item) => item[0] + acc, "");
if (str.length <= end.length) {
return end.slice(-str.length) === str;
if (suffix.length <= end.length) {
return end.slice(-suffix.length) === suffix;
}
// We assume that everything being matched is at most a single token plus some whitespace,
@ -108,15 +121,6 @@ export default class Buffer {
return false;
}
getLast(): string {
if (this._queue.length > 0) {
const last = this._queue[0][0];
return last[last.length - 1];
}
return this._last;
}
hasContent(): boolean {
return this._queue.length > 0 || !!this._last;
}

View File

@ -131,14 +131,13 @@ export default class Printer {
*/
token(str: string): void {
const last = this._buf.getLast();
// space is mandatory to avoid outputting <!--
// http://javascript.spec.whatwg.org/#comment-syntax
if ((str === "--" && last === "!") ||
if ((str === "--" && this.endsWith("!")) ||
// Need spaces for operators of the same kind to avoid: `a+++b`
(str[0] === "+" && last === "+") ||
(str[0] === "-" && last === "-")) {
(str[0] === "+" && this.endsWith("+")) ||
(str[0] === "-" && this.endsWith("-"))) {
this._space();
}