Skip to content

Commit

Permalink
Strict typing
Browse files Browse the repository at this point in the history
  • Loading branch information
lathonez committed Feb 18, 2016
1 parent cf5e88a commit cc2d919
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 38 deletions.
4 changes: 2 additions & 2 deletions app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class ClickerApp {
];
}

private initializeApp() {
private initializeApp(): void {
this.platform.ready().then(() => {
// The platform is now ready. Note: if this callback fails to fire, follow
// the Troubleshooting guide for a number of possible solutions:
Expand All @@ -50,7 +50,7 @@ export class ClickerApp {
});
}

public openPage(page) {
public openPage(page: any): void {
// close the menu when clicking a link from the menu
this.app.getComponent('leftMenu').close();
// navigate to the new page if it is not the current page
Expand Down
4 changes: 3 additions & 1 deletion app/components/clickerForm/clickerForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class ClickerForm {
this.clickerNameInput = this.form.controls['clickerNameInput'];
}

public newClicker(formValue: Object) {
public newClicker(formValue: Object): boolean {

// need to mark the clickerName control as touched so validation
// will apply after the user has tried to add a clicker
Expand All @@ -45,5 +45,7 @@ export class ClickerForm {

// reset the value of the contorl and all validation / state
this.clickerNameInput = Utils.resetControl(this.clickerNameInput);

return true;
}
}
4 changes: 2 additions & 2 deletions app/models/click.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export class Click {
this.location = location || 'TODO';
}

public getTime() {
public getTime(): number {
return this.time;
}

public getLocation() {
public getLocation(): string {
return this.location;
}
}
10 changes: 5 additions & 5 deletions app/models/clicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ export class Clicker {
this.clicks = [];
}

public doClick() {
public doClick(): void {
this.clicks.push(new Click());
}

public addClick(click: Click) {
public addClick(click: Click): void {
this.clicks.push(click);
}

public getCount() {
public getCount(): number {
return this.clicks.length;
}

public getId() {
public getId(): string {
return this.id;
}

public getName() {
public getName(): string {
return this.name;
}
}
50 changes: 25 additions & 25 deletions app/services/clickers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ export class Clickers {
}

// initialise Ids from SQL storage
private initIds() {
return new Promise((resolve) => {
let ids = [];
private initIds(): Promise<{}> {
return new Promise((resolve: Function) => {
let ids: Array<string> = [];
this.storage.get('ids') // return the promise so we can chain initClickers
.then((rawIds) => {
.then((rawIds: string) => {
// ids are stored as stringified JSON array
ids = JSON.parse(String(rawIds)) || [];
})
Expand All @@ -36,13 +36,13 @@ export class Clickers {
}

// initialise Clickers from SQL storage given an array of ids
private initClickers(ids) {
private initClickers(ids: Array<string>): Promise<{}> {
// get all existing ids
return new Promise((resolve) => {
let clickers = [];
return new Promise((resolve: Function) => {
let clickers: Array<Clicker> = [];
for (let id of ids) {
this.storage.get(id)
.then((clicker) => {
.then((clicker: string) => {
clickers.push(this.initClicker(clicker));
});
}
Expand All @@ -51,33 +51,33 @@ export class Clickers {
}

// initialise a clicker from a raw JSON string out of the DB
private initClicker(clicker) {
const parsedClicker = JSON.parse(clicker);
const newClicker = new Clicker(parsedClicker.id, parsedClicker.name);
private initClicker(clicker: string): Clicker {
const parsedClicker: Object = JSON.parse(clicker);
const newClicker: Clicker = new Clicker(parsedClicker['id'], parsedClicker['name']);

// add the clicks - need to re-instantiate object
for (let click of parsedClicker.clicks) {
for (let click of parsedClicker['clicks']) {
newClicker.addClick(new Click(click.time, click.location));
}

return newClicker;
}

private static initStorage() {
private static initStorage(): SqlStorage {
return new SqlStorage();
}

public getClicker(id) {
return this.clickers.find(clicker => { return clicker.getId() === id; });
public getClicker(id: string): Clicker {
return this.clickers.find((clicker: Clicker) => { return clicker.getId() === id; } );
}

public getClickers() {
public getClickers(): Array<Clicker> {
return this.clickers;
}

public newClicker(name) {
const id = this.uid();
const clicker = new Clicker(id, name);
public newClicker(name: string): string {
const id: string = this.uid();
const clicker: Clicker = new Clicker(id, name);

// add the clicker to the service
this.clickers.push(clicker);
Expand All @@ -91,13 +91,13 @@ export class Clickers {
return id;
}

public removeClicker(id) {
public removeClicker(id: string): void {

// remove clicker from the service
this.clickers = this.clickers.filter(clicker => { return clicker.getId() !== id; });
this.clickers = this.clickers.filter((clicker: Clicker) => { return clicker.getId() !== id; });

// remove from ids array
this.ids = this.ids.filter(filterId => { return filterId !== id; });
this.ids = this.ids.filter((filterId: string) => { return filterId !== id; });

// null id in db
this.storage.remove(id);
Expand All @@ -106,14 +106,14 @@ export class Clickers {
this.storage.set('ids', JSON.stringify(this.ids));
}

public doClick(id) {
const clicker = this.getClicker(id);
public doClick(id: string): void {
const clicker: Clicker = this.getClicker(id);
clicker.doClick();
// save the clicker with updated click in storage
this.storage.set(clicker.getId(), JSON.stringify(clicker));
}

private uid() {
private uid(): string {
return Math.random().toString(35).substr(2, 10);
}
}
2 changes: 1 addition & 1 deletion app/services/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class Utils {
// bit of a hack here to reset the validation / state on the control as well as the value
// expecting a Control.reset() method to do this but there doesn't seem to be one
// http://stackoverflow.com/questions/33084280/how-to-reset-control-value
public static resetControl(control: AbstractControl) {
public static resetControl(control: AbstractControl): AbstractControl {
control['updateValue']('');
control['_touched'] = false;
control['_untouched'] = true;
Expand Down
15 changes: 13 additions & 2 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@
}
],
"triple-equals": [true],
"typedef": [false],
"typedef": [
true,
"call-signature",
"parameter",
"property-declaration",
"variable-declaration",
"member-variable-declaration"
],
"typedef-whitespace": [
true,
{
Expand All @@ -88,7 +95,11 @@
"variable-declaration": "nospace"
}
],
"use-strict": [false],
"use-strict": [
true,
"check-module",
"check-function"
],
"variable-name": [
true,
"check-format",
Expand Down

0 comments on commit cc2d919

Please sign in to comment.