Skip to content

Commit 85e5e2c

Browse files
committed
Prune some bytes
1 parent 1bc7e96 commit 85e5e2c

File tree

2 files changed

+42
-55
lines changed

2 files changed

+42
-55
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"size-limit": [
5050
{
5151
"path": "dist/index.js",
52-
"limit": "2.2 kB"
52+
"limit": "2 kB"
5353
}
5454
],
5555
"ts-scripts": {

src/index.ts

Lines changed: 41 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -122,51 +122,6 @@ function errorMessage(text: string, originalPath: string | undefined) {
122122
return message;
123123
}
124124

125-
class Iter {
126-
private _tokens: Array<LexToken>;
127-
private _index = 0;
128-
129-
constructor(
130-
tokens: Array<LexToken>,
131-
private originalPath: string,
132-
) {
133-
this._index = 0;
134-
this._tokens = tokens;
135-
}
136-
137-
peek(): LexToken {
138-
return this._tokens[this._index];
139-
}
140-
141-
tryConsume(type: TokenType): string | undefined {
142-
const token = this.peek();
143-
if (token.type !== type) return;
144-
this._index++;
145-
return token.value;
146-
}
147-
148-
consume(type: TokenType): string {
149-
const value = this.tryConsume(type);
150-
if (value !== undefined) return value;
151-
const { type: nextType, index } = this.peek();
152-
throw new TypeError(
153-
errorMessage(
154-
`Unexpected ${nextType} at index ${index}, expected ${type}`,
155-
this.originalPath,
156-
),
157-
);
158-
}
159-
160-
text(): string {
161-
let result = "";
162-
let value: string | undefined;
163-
while ((value = this.tryConsume("CHAR") || this.tryConsume("ESCAPED"))) {
164-
result += value;
165-
}
166-
return result;
167-
}
168-
}
169-
170125
/**
171126
* Plain text.
172127
*/
@@ -232,6 +187,7 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
232187
const chars = [...str];
233188
const tokens: Array<LexToken> = [];
234189
let i = 0;
190+
let index = 0;
235191

236192
function name() {
237193
let value = "";
@@ -295,14 +251,46 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
295251

296252
tokens.push({ type: "END", index: i, value: "" });
297253

298-
function consume(it: Iter, endType: TokenType): Token[] {
254+
function peek(): LexToken {
255+
return tokens[index];
256+
}
257+
258+
function tryConsume(type: TokenType): string | undefined {
259+
const token = peek();
260+
if (token.type !== type) return;
261+
index++;
262+
return token.value;
263+
}
264+
265+
function consume(type: TokenType): string {
266+
const value = tryConsume(type);
267+
if (value !== undefined) return value;
268+
const { type: nextType, index } = peek();
269+
throw new TypeError(
270+
errorMessage(
271+
`Unexpected ${nextType} at index ${index}, expected ${type}`,
272+
str,
273+
),
274+
);
275+
}
276+
277+
function text(): string {
278+
let result = "";
279+
let value: string | undefined;
280+
while ((value = tryConsume("CHAR") || tryConsume("ESCAPED"))) {
281+
result += value;
282+
}
283+
return result;
284+
}
285+
286+
function consumeUntil(endType: TokenType): Token[] {
299287
const tokens: Token[] = [];
300288

301289
while (true) {
302-
const path = it.text();
290+
const path = text();
303291
if (path) tokens.push({ type: "text", value: encodePath(path) });
304292

305-
const param = it.tryConsume("PARAM");
293+
const param = tryConsume("PARAM");
306294
if (param) {
307295
tokens.push({
308296
type: "param",
@@ -311,7 +299,7 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
311299
continue;
312300
}
313301

314-
const wildcard = it.tryConsume("WILDCARD");
302+
const wildcard = tryConsume("WILDCARD");
315303
if (wildcard) {
316304
tokens.push({
317305
type: "wildcard",
@@ -320,22 +308,21 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
320308
continue;
321309
}
322310

323-
const open = it.tryConsume("{");
311+
const open = tryConsume("{");
324312
if (open) {
325313
tokens.push({
326314
type: "group",
327-
tokens: consume(it, "}"),
315+
tokens: consumeUntil("}"),
328316
});
329317
continue;
330318
}
331319

332-
it.consume(endType);
320+
consume(endType);
333321
return tokens;
334322
}
335323
}
336324

337-
const it = new Iter(tokens, str);
338-
return new TokenData(consume(it, "END"), str);
325+
return new TokenData(consumeUntil("END"), str);
339326
}
340327

341328
/**

0 commit comments

Comments
 (0)