Skip to content

Commit 1bc7e96

Browse files
committed
Remove another function in stack
1 parent 35ff289 commit 1bc7e96

File tree

2 files changed

+70
-78
lines changed

2 files changed

+70
-78
lines changed

src/index.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ describe("path-to-regexp", () => {
152152
} catch (error) {
153153
const stack = (error as Error).stack
154154
?.split("\n")
155-
.slice(0, 6)
155+
.slice(0, 5)
156156
.join("\n");
157157
expect(stack).toContain("index.spec.ts");
158158
}

src/index.ts

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

125-
/**
126-
* Tokenize input string.
127-
*/
128-
function lexer(str: string): Iter {
129-
const chars = [...str];
130-
const tokens: Array<LexToken> = [];
131-
let i = 0;
132-
133-
function name() {
134-
let value = "";
135-
136-
if (ID_START.test(chars[++i])) {
137-
value += chars[i];
138-
while (ID_CONTINUE.test(chars[++i])) {
139-
value += chars[i];
140-
}
141-
} else if (chars[i] === '"') {
142-
let pos = i;
143-
144-
while (i < chars.length) {
145-
if (chars[++i] === '"') {
146-
i++;
147-
pos = 0;
148-
break;
149-
}
150-
151-
if (chars[i] === "\\") {
152-
value += chars[++i];
153-
} else {
154-
value += chars[i];
155-
}
156-
}
157-
158-
if (pos) {
159-
throw new TypeError(
160-
errorMessage(`Unterminated quote at index ${pos}`, str),
161-
);
162-
}
163-
}
164-
165-
if (!value) {
166-
throw new TypeError(
167-
errorMessage(`Missing parameter name at index ${i}`, str),
168-
);
169-
}
170-
171-
return value;
172-
}
173-
174-
while (i < chars.length) {
175-
const value = chars[i];
176-
const type = SIMPLE_TOKENS[value];
177-
178-
if (type) {
179-
tokens.push({ type, index: i++, value });
180-
} else if (value === "\\") {
181-
tokens.push({ type: "ESCAPED", index: i++, value: chars[i++] });
182-
} else if (value === ":") {
183-
const value = name();
184-
tokens.push({ type: "PARAM", index: i, value });
185-
} else if (value === "*") {
186-
const value = name();
187-
tokens.push({ type: "WILDCARD", index: i, value });
188-
} else {
189-
tokens.push({ type: "CHAR", index: i, value: chars[i++] });
190-
}
191-
}
192-
193-
tokens.push({ type: "END", index: i, value: "" });
194-
return new Iter(tokens, str);
195-
}
196-
197125
class Iter {
198126
private _tokens: Array<LexToken>;
199127
private _index = 0;
@@ -301,9 +229,73 @@ export class TokenData {
301229
*/
302230
export function parse(str: string, options: ParseOptions = {}): TokenData {
303231
const { encodePath = NOOP_VALUE } = options;
304-
const it = lexer(str);
232+
const chars = [...str];
233+
const tokens: Array<LexToken> = [];
234+
let i = 0;
235+
236+
function name() {
237+
let value = "";
238+
239+
if (ID_START.test(chars[++i])) {
240+
value += chars[i];
241+
while (ID_CONTINUE.test(chars[++i])) {
242+
value += chars[i];
243+
}
244+
} else if (chars[i] === '"') {
245+
let pos = i;
246+
247+
while (i < chars.length) {
248+
if (chars[++i] === '"') {
249+
i++;
250+
pos = 0;
251+
break;
252+
}
253+
254+
if (chars[i] === "\\") {
255+
value += chars[++i];
256+
} else {
257+
value += chars[i];
258+
}
259+
}
260+
261+
if (pos) {
262+
throw new TypeError(
263+
errorMessage(`Unterminated quote at index ${pos}`, str),
264+
);
265+
}
266+
}
267+
268+
if (!value) {
269+
throw new TypeError(
270+
errorMessage(`Missing parameter name at index ${i}`, str),
271+
);
272+
}
273+
274+
return value;
275+
}
276+
277+
while (i < chars.length) {
278+
const value = chars[i];
279+
const type = SIMPLE_TOKENS[value];
280+
281+
if (type) {
282+
tokens.push({ type, index: i++, value });
283+
} else if (value === "\\") {
284+
tokens.push({ type: "ESCAPED", index: i++, value: chars[i++] });
285+
} else if (value === ":") {
286+
const value = name();
287+
tokens.push({ type: "PARAM", index: i, value });
288+
} else if (value === "*") {
289+
const value = name();
290+
tokens.push({ type: "WILDCARD", index: i, value });
291+
} else {
292+
tokens.push({ type: "CHAR", index: i, value: chars[i++] });
293+
}
294+
}
295+
296+
tokens.push({ type: "END", index: i, value: "" });
305297

306-
function consume(endType: TokenType): Token[] {
298+
function consume(it: Iter, endType: TokenType): Token[] {
307299
const tokens: Token[] = [];
308300

309301
while (true) {
@@ -332,7 +324,7 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
332324
if (open) {
333325
tokens.push({
334326
type: "group",
335-
tokens: consume("}"),
327+
tokens: consume(it, "}"),
336328
});
337329
continue;
338330
}
@@ -342,8 +334,8 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
342334
}
343335
}
344336

345-
const tokens = consume("END");
346-
return new TokenData(tokens, str);
337+
const it = new Iter(tokens, str);
338+
return new TokenData(consume(it, "END"), str);
347339
}
348340

349341
/**

0 commit comments

Comments
 (0)