Skip to content

Commit

Permalink
Format code using prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
beheh committed Nov 7, 2017
1 parent 8058194 commit 382b9a4
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 45 deletions.
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"useTabs": true,
"trailingComma": "es5"
}
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ language: node_js
node_js:
- "7"
script:
- yarn run build
- yarn build
- yarn lint
deploy:
provider: npm
email: benedict@hearthsim.net
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
"scripts": {
"prepublishOnly": "tsc",
"build": "tsc",
"dev": "tsc --watch"
"dev": "tsc --watch",
"format": "prettier --write \"{src/**/*.ts,*.js,*.ts}\"",
"lint": "prettier --list-different \"{src/**/*.ts,*.js,*.ts}\""
},
"devDependencies": {
"prettier": "^1.8.1",
"typescript": "^2.2.1"
},
"dependencies": {
Expand Down
3 changes: 1 addition & 2 deletions src/CacheProxy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {StorageBackend} from "./StorageBackend";
import { StorageBackend } from "./StorageBackend";

export default class CacheProxy implements StorageBackend {

private cache: any;
public backend: StorageBackend;

Expand Down
77 changes: 46 additions & 31 deletions src/HearthstoneJSON.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import "isomorphic-fetch";
import {StorageBackend} from "./StorageBackend";
import { StorageBackend } from "./StorageBackend";
import NoOpStorageBackend from "./NoOpStorageBackend";
import LocalStorageBackend from "./LocalStorageBackend";
import CacheProxy from "./CacheProxy";
import {REVISIONS} from "./constants";
import {Build, BuildNumber, Locale} from "./types";
import {CardData} from "hearthstonejson";
import { REVISIONS } from "./constants";
import { Build, BuildNumber, Locale } from "./types";
import { CardData } from "hearthstonejson";

export default class HearthstoneJSON {

public storage: StorageBackend;
public storagePrefix = "hsjson-";

Expand All @@ -21,11 +20,9 @@ export default class HearthstoneJSON {
constructor(storage?: StorageBackend) {
if (storage === null) {
this.storage = new NoOpStorageBackend();
}
else if (typeof storage === "undefined") {
} else if (typeof storage === "undefined") {
this.storage = new CacheProxy(new LocalStorageBackend());
}
else if (storage) {
} else if (storage) {
this.storage = storage;
}
}
Expand All @@ -35,9 +32,13 @@ export default class HearthstoneJSON {
};

public extractBuild = (url: string): Build => {
const endpointExpression = new RegExp(this.endpoint.replace(/[\/.]/g, "\\$&"));
const endpointExpression = new RegExp(
this.endpoint.replace(/[\/.]/g, "\\$&")
);
const pathExpression = /((\d+)|(latest))\/[a-zA-Z]+\/cards\.json/;
const pattern = new RegExp("^" + endpointExpression.source + pathExpression.source + "$");
const pattern = new RegExp(
"^" + endpointExpression.source + pathExpression.source + "$"
);
const matches = pattern.exec(url);
if (!matches) {
throw new Error('No build found in url "' + url + '"');
Expand All @@ -55,7 +56,7 @@ export default class HearthstoneJSON {
this.fallback = false;
return this.getSpecificBuild(build, locale).catch(() => {
this.fallback = true;
return this.fetchLatestBuild(locale)
return this.fetchLatestBuild(locale);
});
}

Expand All @@ -67,7 +68,10 @@ export default class HearthstoneJSON {
return this.fetchLatestBuild(locale);
}

protected getSpecificBuild(build: BuildNumber, locale: Locale): Promise<CardData[]> {
protected getSpecificBuild(
build: BuildNumber,
locale: Locale
): Promise<CardData[]> {
const key = this.generateKey(build, locale);
let bypassCache = false;
if (this.storage.has(key)) {
Expand All @@ -87,35 +91,42 @@ export default class HearthstoneJSON {
bypassCache = true;
}
this.cached = false;
return this.fetchSpecificBuild(build, locale, bypassCache)
.catch((error) => {
// possibly invalid CORS header in cache
return this.fetchSpecificBuild(build, locale, true);
});
return this.fetchSpecificBuild(build, locale, bypassCache).catch(error => {
// possibly invalid CORS header in cache
return this.fetchSpecificBuild(build, locale, true);
});
}

protected fetchSpecificBuild(build: BuildNumber, locale: Locale, bypassCache?: boolean): Promise<CardData[]> {
protected fetchSpecificBuild(
build: BuildNumber,
locale: Locale,
bypassCache?: boolean
): Promise<CardData[]> {
const headers = new Headers();
headers.set("accept", "application/json; charset=utf-8");
return fetch(this.createUrl(build, locale), {
method: "GET",
mode: "cors",
cache: bypassCache ? "reload" : "default",
headers,
}).then((response: Response): Promise<CardData[]> => {
const statusCode = response.status;
if (statusCode !== 200) {
throw new Error("Expected status code 200, got " + statusCode);
}
return response.json();
}).then((payload: CardData[]) => {
this.store(build, locale, payload);
return payload;
});
})
.then((response: Response): Promise<CardData[]> => {
const statusCode = response.status;
if (statusCode !== 200) {
throw new Error("Expected status code 200, got " + statusCode);
}
return response.json();
})
.then((payload: CardData[]) => {
this.store(build, locale, payload);
return payload;
});
}

protected fetchLatestBuild(locale: Locale): Promise<CardData[]> {
return this.fetchLatestBuildNumber(locale).then((build: BuildNumber) => this.getSpecificBuild(build, locale));
return this.fetchLatestBuildNumber(locale).then((build: BuildNumber) =>
this.getSpecificBuild(build, locale)
);
}

protected fetchLatestBuildNumber(locale: Locale): Promise<BuildNumber> {
Expand All @@ -140,7 +151,11 @@ export default class HearthstoneJSON {
});
}

protected store(buildNumber: BuildNumber, locale: Locale, payload: CardData[]): void {
protected store(
buildNumber: BuildNumber,
locale: Locale,
payload: CardData[]
): void {
if (!payload.length) {
// this doesn't look right - refuse to cache this
return;
Expand Down
11 changes: 4 additions & 7 deletions src/LocalStorageBackend.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {StorageBackend} from "./StorageBackend";
import { StorageBackend } from "./StorageBackend";

export default class LocalStorageBackend implements StorageBackend {

private _available(): boolean {
try {
return "localStorage" in window && window["localStorage"] !== null;
Expand All @@ -14,7 +13,7 @@ export default class LocalStorageBackend implements StorageBackend {
if (!this._available()) {
return false;
}
return typeof(localStorage[key]) === "string";
return typeof localStorage[key] === "string";
}

public set(key: string, value: any): void {
Expand All @@ -26,12 +25,10 @@ export default class LocalStorageBackend implements StorageBackend {
try {
localStorage.setItem(key, compressed);
break;
}
catch (e) {
} catch (e) {
try {
localStorage.removeItem(localStorage.key(0));
}
catch (e) {
} catch (e) {
break;
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/NoOpStorageBackend.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {StorageBackend} from "./StorageBackend";
import { StorageBackend } from "./StorageBackend";

export default class NoOpStorageBackend implements StorageBackend {

public has(key: string): boolean {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export type BuildNumber = number;
export type Build = BuildNumber|"latest";
export type Build = BuildNumber | "latest";
export type Locale = string;
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ node-fetch@^1.0.1:
encoding "^0.1.11"
is-stream "^1.0.1"

prettier@^1.8.1:
version "1.8.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.8.1.tgz#91064d778c08c85ac1cbe6b23195c34310d039f9"

typescript@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.2.1.tgz#4862b662b988a4c8ff691cc7969622d24db76ae9"
Expand Down

0 comments on commit 382b9a4

Please sign in to comment.