diff --git a/lib/parse.ts b/lib/parse.ts index 2f814217..1c741178 100644 --- a/lib/parse.ts +++ b/lib/parse.ts @@ -19,7 +19,13 @@ export default parse; */ async function parse(path: string, $refs: $Refs, options: Options) { // Remove the URL fragment, if any - path = url.stripHash(path); + const hashIndex = path.indexOf("#"); + let hash = ""; + if (hashIndex >= 0) { + hash = path.substring(hashIndex); + // Remove the URL fragment, if any + path = path.substring(0, hashIndex); + } // Add a new $Ref for this file, even though we don't have the value yet. // This ensures that we don't simultaneously read & parse the same file multiple times @@ -28,6 +34,7 @@ async function parse(path: string, $refs: $Refs, options: Options) { // This "file object" will be passed to all resolvers and parsers. const file = { url: path, + hash, extension: url.getExtension(path), } as FileInfo; @@ -103,8 +110,6 @@ async function readFile(file: FileInfo, options: Options, $refs: $Refs): Promise * The promise resolves with the parsed file contents and the parser that was used. */ async function parseFile(file: FileInfo, options: Options, $refs: $Refs) { - // console.log('Parsing %s', file.url); - // Find the parsers that can read this file type. // If none of the parsers are an exact match for this file, then we'll try ALL of them. // This handles situations where the file IS a supported type, just with an unknown extension. diff --git a/lib/pointer.ts b/lib/pointer.ts index 8ff3dd02..e5b5d62a 100644 --- a/lib/pointer.ts +++ b/lib/pointer.ts @@ -190,9 +190,9 @@ class Pointer { * @param [originalPath] * @returns */ - static parse(path: string, originalPath?: string) { + static parse(path: string, originalPath?: string): string[] { // Get the JSON pointer from the path's hash - let pointer = url.getHash(path).substr(1); + const pointer = url.getHash(path).substring(1); // If there's no pointer, then there are no tokens, // so return an empty array @@ -201,18 +201,18 @@ class Pointer { } // Split into an array - pointer = pointer.split("/"); + const split = pointer.split("/"); // Decode each part, according to RFC 6901 - for (let i = 0; i < pointer.length; i++) { - pointer[i] = safeDecodeURIComponent(pointer[i].replace(escapedSlash, "/").replace(escapedTilde, "~")); + for (let i = 0; i < split.length; i++) { + split[i] = safeDecodeURIComponent(split[i].replace(escapedSlash, "/").replace(escapedTilde, "~")); } - if (pointer[0] !== "") { - throw new InvalidPointerError(pointer, originalPath === undefined ? path : originalPath); + if (split[0] !== "") { + throw new InvalidPointerError(split, originalPath === undefined ? path : originalPath); } - return pointer.slice(1); + return split.slice(1); } /** diff --git a/lib/types/index.ts b/lib/types/index.ts index 4340f78a..4d93e22a 100644 --- a/lib/types/index.ts +++ b/lib/types/index.ts @@ -130,6 +130,11 @@ export interface FileInfo { */ url: string; + /** + * The hash (URL fragment) of the file URL, including the # symbol. If the URL doesn't have a hash, then this will be an empty string. + */ + hash: string; + /** * The lowercase file extension, such as ".json", ".yaml", ".txt", etc. */ diff --git a/lib/util/url.ts b/lib/util/url.ts index bff79576..3826fa91 100644 --- a/lib/util/url.ts +++ b/lib/util/url.ts @@ -105,10 +105,10 @@ export function stripQuery(path: any) { * @param path * @returns */ -export function getHash(path: any) { +export function getHash(path: string) { const hashIndex = path.indexOf("#"); if (hashIndex >= 0) { - return path.substr(hashIndex); + return path.substring(hashIndex); } return "#"; } @@ -119,10 +119,10 @@ export function getHash(path: any) { * @param path * @returns */ -export function stripHash(path: any) { +export function stripHash(path: string) { const hashIndex = path.indexOf("#"); if (hashIndex >= 0) { - path = path.substr(0, hashIndex); + path = path.substring(0, hashIndex); } return path; } @@ -133,7 +133,7 @@ export function stripHash(path: any) { * @param path * @returns */ -export function isHttp(path: any) { +export function isHttp(path: string) { const protocol = getProtocol(path); if (protocol === "http" || protocol === "https") { return true;