Skip to content

Commit 48445fb

Browse files
committed
feat(hash): include hash in FileInfo #256
1 parent 8a42204 commit 48445fb

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

lib/parse.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ export default parse;
1919
*/
2020
async function parse(path: string, $refs: $Refs, options: Options) {
2121
// Remove the URL fragment, if any
22-
path = url.stripHash(path);
22+
const hashIndex = path.indexOf("#");
23+
let hash = "";
24+
if (hashIndex >= 0) {
25+
hash = path.substring(hashIndex);
26+
// Remove the URL fragment, if any
27+
path = path.substring(0, hashIndex);
28+
}
2329

2430
// Add a new $Ref for this file, even though we don't have the value yet.
2531
// 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) {
2834
// This "file object" will be passed to all resolvers and parsers.
2935
const file = {
3036
url: path,
37+
hash,
3138
extension: url.getExtension(path),
3239
} as FileInfo;
3340

@@ -103,8 +110,6 @@ async function readFile(file: FileInfo, options: Options, $refs: $Refs): Promise
103110
* The promise resolves with the parsed file contents and the parser that was used.
104111
*/
105112
async function parseFile(file: FileInfo, options: Options, $refs: $Refs) {
106-
// console.log('Parsing %s', file.url);
107-
108113
// Find the parsers that can read this file type.
109114
// If none of the parsers are an exact match for this file, then we'll try ALL of them.
110115
// This handles situations where the file IS a supported type, just with an unknown extension.

lib/pointer.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,9 @@ class Pointer {
190190
* @param [originalPath]
191191
* @returns
192192
*/
193-
static parse(path: string, originalPath?: string) {
193+
static parse(path: string, originalPath?: string): string[] {
194194
// Get the JSON pointer from the path's hash
195-
let pointer = url.getHash(path).substr(1);
195+
const pointer = url.getHash(path).substring(1);
196196

197197
// If there's no pointer, then there are no tokens,
198198
// so return an empty array
@@ -201,18 +201,18 @@ class Pointer {
201201
}
202202

203203
// Split into an array
204-
pointer = pointer.split("/");
204+
const split = pointer.split("/");
205205

206206
// Decode each part, according to RFC 6901
207-
for (let i = 0; i < pointer.length; i++) {
208-
pointer[i] = safeDecodeURIComponent(pointer[i].replace(escapedSlash, "/").replace(escapedTilde, "~"));
207+
for (let i = 0; i < split.length; i++) {
208+
split[i] = safeDecodeURIComponent(split[i].replace(escapedSlash, "/").replace(escapedTilde, "~"));
209209
}
210210

211-
if (pointer[0] !== "") {
212-
throw new InvalidPointerError(pointer, originalPath === undefined ? path : originalPath);
211+
if (split[0] !== "") {
212+
throw new InvalidPointerError(split, originalPath === undefined ? path : originalPath);
213213
}
214214

215-
return pointer.slice(1);
215+
return split.slice(1);
216216
}
217217

218218
/**

lib/types/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ export interface FileInfo {
130130
*/
131131
url: string;
132132

133+
/**
134+
* 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.
135+
*/
136+
hash: string;
137+
133138
/**
134139
* The lowercase file extension, such as ".json", ".yaml", ".txt", etc.
135140
*/

lib/util/url.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ export function stripQuery(path: any) {
105105
* @param path
106106
* @returns
107107
*/
108-
export function getHash(path: any) {
108+
export function getHash(path: string) {
109109
const hashIndex = path.indexOf("#");
110110
if (hashIndex >= 0) {
111-
return path.substr(hashIndex);
111+
return path.substring(hashIndex);
112112
}
113113
return "#";
114114
}
@@ -119,10 +119,10 @@ export function getHash(path: any) {
119119
* @param path
120120
* @returns
121121
*/
122-
export function stripHash(path: any) {
122+
export function stripHash(path: string) {
123123
const hashIndex = path.indexOf("#");
124124
if (hashIndex >= 0) {
125-
path = path.substr(0, hashIndex);
125+
path = path.substring(0, hashIndex);
126126
}
127127
return path;
128128
}
@@ -133,7 +133,7 @@ export function stripHash(path: any) {
133133
* @param path
134134
* @returns
135135
*/
136-
export function isHttp(path: any) {
136+
export function isHttp(path: string) {
137137
const protocol = getProtocol(path);
138138
if (protocol === "http" || protocol === "https") {
139139
return true;

0 commit comments

Comments
 (0)