Skip to content

Commit

Permalink
refactor!: remove name field from data output
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroentvb committed May 17, 2022
1 parent af5a530 commit 36ba01e
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 64 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import windReport from './partials/windfinder-report.js';

import type { WindfinderData } from './interfaces/windfinder.js';
import type { WindguruData } from './interfaces/windguru.js';
import type { WindyData } from './interfaces/windy.js';
import type { WindyModel } from './interfaces/windy.js';
import type { WindReport } from './interfaces/wind-report.js';

export {
Expand All @@ -18,6 +18,6 @@ export {
// Expose types
WindfinderData,
WindguruData,
WindyData,
WindyModel,
WindReport
};
1 change: 0 additions & 1 deletion src/interfaces/wind-report.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export interface WindReport {
name: string
spot: string
report: WindReportItem[]
}
Expand Down
1 change: 0 additions & 1 deletion src/interfaces/windfinder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export interface WindfinderData {
name: string
spot: string
days: WindfinderDataDay[]
}
Expand Down
7 changes: 0 additions & 7 deletions src/interfaces/windy.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
export interface WindyData {
name: string;
models: WindyModel[];
url?: string;
}

export interface WindyModel {
name: string;
days: WindyModelDay[];
Expand All @@ -22,7 +16,6 @@ export interface WindyModelHour {
}

export interface ExtractedWindyData {
name: string;
date: (string | undefined)[];
models: ExtractedWindyModelData[];
}
Expand Down
1 change: 0 additions & 1 deletion src/partials/data-parsers/windfinder-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export default class Windfinder extends DataHelper {

parse(): this {
this.parsedData = {
name: 'Windfinder',
spot: this.extractedData.spot,
days: this.extractedData.date
.map((_, i) => this.getWindfinderDay(i))
Expand Down
1 change: 0 additions & 1 deletion src/partials/data-parsers/windfinder-report-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export default class Report {
}));

this.parsedData = {
name: 'Windfinder report',
spot: this.spot,
report
};
Expand Down
94 changes: 45 additions & 49 deletions src/partials/data-parsers/windy-parser.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import DataHelper from '../utils/data-helper.js';
import { reverseDate } from '../utils/windy-utils.js';
import type { ExtractedWindyData, WindyData, WindyModelDay, WindyModelHour } from '../../interfaces/windy.js';
import type { ExtractedWindyData, WindyModel, WindyModelDay, WindyModelHour } from '../../interfaces/windy.js';

export default class Windy extends DataHelper {
private extractedData!: ExtractedWindyData;
private parsedData!: WindyData;
private parsedData!: WindyModel[];

constructor(html: string) {
super(html);
Expand All @@ -14,7 +14,6 @@ export default class Windy extends DataHelper {

extract(): this {
this.extractedData = {
name: 'Windy',
date: this.getDataArray(['.sticky-title-wrapper', '.td-days'], (el) => this.$(el).data('day')),
models: this.getDataArray(['.legend-left', '.legend-windCombined'], (el, i) => {
const windData: { windspeed: number[], windgust: number[], winddirection: number[] } = {
Expand Down Expand Up @@ -52,56 +51,53 @@ export default class Windy extends DataHelper {
}

parse(): this {
this.parsedData = {
name: this.extractedData.name,
models: this.extractedData.models.map((model) => {
const hours: WindyModelHour[] = model.time.map((hour, i) => ({
hour,
windspeed: model.windspeed[i],
windgust: model.windgust[i],
winddirection: model.winddirection[i]
}));
const days: WindyModelDay[] = [];

let dayIndex = 0;
let previousHour: WindyModelHour;

hours.forEach((hour, i) => {
if (i === 0) {
days[0] = {
date: this.extractedData.date[0] ? reverseDate(this.extractedData.date[0]) : null,
hours: []
};

days[0].hours.push(hour);
} else if (hour.hour < previousHour.hour) {
// New day
dayIndex++;

days[dayIndex] = {
date: this.extractedData.date[dayIndex] ? reverseDate(this.extractedData.date[dayIndex] as string) : null,
hours: []
};

days[dayIndex].hours.push(hour);
} else {
days[dayIndex].hours.push(hour);
}

previousHour = hour;
});

return {
name: model.name,
days
};
})
};
this.parsedData = this.extractedData.models.map((model) => {
const hours: WindyModelHour[] = model.time.map((hour, i) => ({
hour,
windspeed: model.windspeed[i],
windgust: model.windgust[i],
winddirection: model.winddirection[i]
}));
const days: WindyModelDay[] = [];

let dayIndex = 0;
let previousHour: WindyModelHour;

hours.forEach((hour, i) => {
if (i === 0) {
days[0] = {
date: this.extractedData.date[0] ? reverseDate(this.extractedData.date[0]) : null,
hours: []
};

days[0].hours.push(hour);
} else if (hour.hour < previousHour.hour) {
// New day
dayIndex++;

days[dayIndex] = {
date: this.extractedData.date[dayIndex] ? reverseDate(this.extractedData.date[dayIndex] as string) : null,
hours: []
};

days[dayIndex].hours.push(hour);
} else {
days[dayIndex].hours.push(hour);
}

previousHour = hour;
});

return {
name: model.name,
days
};
});

return this;
}

get(): WindyData {
get(): WindyModel[] {
return this.parsedData;
}

Expand Down
4 changes: 2 additions & 2 deletions src/partials/windy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { getWindyUrl } from '../partials/utils/url-builder.js';

import { PPTR_TIMEOUT, REQUEST_TIMEOUT } from '../constants/index.js';

import type { WindyData } from '../interfaces/windy.js';
import type { WindyModel } from '../interfaces/windy.js';


export default async function windy(lat: string | number, long: string | number): Promise<WindyData> {
export default async function windy(lat: string | number, long: string | number): Promise<WindyModel[]> {
typeCheckWindy(lat, long);

const url = getWindyUrl(lat, long);
Expand Down

0 comments on commit 36ba01e

Please sign in to comment.