Skip to content
This repository has been archived by the owner on May 27, 2020. It is now read-only.

Commit

Permalink
feat: make manifest be a iterator.
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Feb 23, 2020
1 parent 27ca82c commit 8c7b7ce
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
7 changes: 7 additions & 0 deletions core/manifest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ test("core / manifest", () => {
path.join(denoDir, "deps", "https", "example.com", "manifest.json")
) as IManifest;

for (const [key, value] of exampleOriginManifest) {
expect(typeof key).toBe("string");
expect(typeof value).toBe("string");

expect(exampleOriginManifest.getHashFromUrlPath(key)).toBe(value);
}

expect(exampleOriginManifest).not.toBe(undefined);
expect(exampleOriginManifest.origin).toEqual("https://example.com");
expect(exampleOriginManifest.filepath).toEqual(
Expand Down
47 changes: 37 additions & 10 deletions core/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import * as fs from "fs";
import { getDenoDepsDir } from "./deno";
import { pathExistsSync, str2regexpStr } from "./util";

type urlPathAndQuerySHA256 = string;
type hash = string;

export interface IManifest {
origin: string;
filepath: string;
getHashFromUrlPath(urlPathAndQuery: string): urlPathAndQuerySHA256 | void;
getUrlPathFromHash(hash: urlPathAndQuerySHA256): string | void;
getHashFromUrlPath(urlPath: string): hash | void;
getUrlPathFromHash(hash: hash): string | void;
[Symbol.iterator](): Iterator<string[]>;
}

export class Manifest implements IManifest {
Expand All @@ -32,18 +33,44 @@ export class Manifest implements IManifest {
constructor(
public origin: string,
public filepath: string,
private map: { [urlPathAndQuery: string]: urlPathAndQuerySHA256 }
private map: { [urlPath: string]: hash }
) {}
getHashFromUrlPath(urlPathAndQuery: string): urlPathAndQuerySHA256 | void {
return this.map[urlPathAndQuery];
getHashFromUrlPath(urlPath: string): hash | void {
return this.map[urlPath];
}
getUrlPathFromHash(hash: urlPathAndQuerySHA256): string | void {
for (const urlPathAndQuery in this.map) {
const _hash = this.map[urlPathAndQuery];
getUrlPathFromHash(hash: hash): string | void {
for (const urlPath in this.map) {
const _hash = this.map[urlPath];
if (_hash === hash) {
return urlPathAndQuery;
return urlPath;
}
}
return;
}
[Symbol.iterator](): Iterator<string[]> {
const keys = Object.keys(this.map);

let currentIndex = 0;

return {
next: () => {
if (currentIndex === keys.length) {
return {
value: [],
done: true
};
}

const key = keys[currentIndex];
const value = this.map[key];

currentIndex++;

return {
value: [key, value],
done: false
};
}
};
}
}

0 comments on commit 8c7b7ce

Please sign in to comment.