diff --git a/src/try-path.ts b/src/try-path.ts index d483968..8db9b4d 100644 --- a/src/try-path.ts +++ b/src/try-path.ts @@ -1,12 +1,15 @@ import * as path from "path"; import { MappingEntry } from "./mapping-entry"; import { dirname } from "path"; -import { removeExtension } from "./filesystem"; -export interface TryPath { - readonly type: "file" | "extension" | "index" | "package"; +export type TryPath = { + readonly type: "file" | "index" | "package"; readonly path: string; -} +} | { + readonly type: "extension"; + readonly path: string; + readonly extensionLength: number; +}; /** * Builds a list of all physical paths to try by: @@ -41,7 +44,7 @@ export function getPathsToTry( pathsToTry.push({ type: "file", path: physicalPath }); pathsToTry.push( ...extensions.map( - e => ({ type: "extension", path: physicalPath + e } as TryPath) + e => ({ type: "extension", path: physicalPath + e, extensionLength: e.length } as TryPath) ) ); pathsToTry.push({ @@ -67,7 +70,7 @@ export function getStrippedPath(tryPath: TryPath): string { : tryPath.type === "file" ? tryPath.path : tryPath.type === "extension" - ? removeExtension(tryPath.path) + ? tryPath.path.slice(0, -tryPath.extensionLength) : tryPath.type === "package" ? tryPath.path : exhaustiveTypeException(tryPath.type); @@ -80,7 +83,7 @@ export function exhaustiveTypeException(check: never): never { /** * Matches pattern with a single star against search. * Star must match at least one character to be considered a match. - * @param patttern for example "foo*" + * @param patttern for example "foo*" * @param search for example "fooawesomebar" * @returns the part of search that * matches, or undefined if no match. */ diff --git a/test/try-path-tests.ts b/test/try-path-tests.ts index da8efdf..85c810f 100644 --- a/test/try-path-tests.ts +++ b/test/try-path-tests.ts @@ -49,11 +49,13 @@ describe("mapping-entry", () => { { type: "file", path: join("/absolute", "base", "url", "foo2", "bar") }, { type: "extension", - path: join("/absolute", "base", "url", "foo2", "bar.ts") + path: join("/absolute", "base", "url", "foo2", "bar.ts"), + extensionLength: 3 }, { type: "extension", - path: join("/absolute", "base", "url", "foo2", "bar.tsx") + path: join("/absolute", "base", "url", "foo2", "bar.tsx"), + extensionLength: 4 }, { type: "package", @@ -69,8 +71,16 @@ describe("mapping-entry", () => { }, // "*" { type: "file", path: join("/absolute", "base", "url", "foo1") }, - { type: "extension", path: join("/absolute", "base", "url", "foo1.ts") }, - { type: "extension", path: join("/absolute", "base", "url", "foo1.tsx") }, + { + type: "extension", + path: join("/absolute", "base", "url", "foo1.ts"), + extensionLength: 3 + }, + { + type: "extension", + path: join("/absolute", "base", "url", "foo1.tsx"), + extensionLength: 4 + }, { type: "package", path: join("/absolute", "base", "url", "foo1", "package.json")