Skip to content

Commit

Permalink
Use nullish coalescing, optional catch bindings and logical nullish a…
Browse files Browse the repository at this point in the history
…ssignment operator (Fixes #93)
  • Loading branch information
scottohara committed Oct 29, 2021
1 parent c00c68b commit a6e9bf0
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 52 deletions.
8 changes: 4 additions & 4 deletions cypress/support/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,23 @@ Cypress.Commands.add("createTestData", ({ programs = [], settings = [] }: TestDa

await programsStore.save({
ProgramID: programId,
Name: undefined === programName ? `Program ${programIndex}` : programName
Name: programName ?? `Program ${programIndex}`
});

series.forEach(async ({ seriesName, nowShowing, episodes }, seriesIndex): Promise<void> => {
const seriesId = `${programId}-${seriesIndex}`;

await seriesStore.save({
SeriesID: seriesId,
Name: undefined === seriesName ? `Series ${seriesIndex}` : seriesName,
NowShowing: undefined === nowShowing ? null : nowShowing,
Name: seriesName ?? `Series ${seriesIndex}`,
NowShowing: nowShowing ?? null,
ProgramID: programId
});

episodes.forEach(async ({ episodeName, status = "", statusDate = "", unverified = "false", unscheduled = "false" }, episodeIndex): Promise<void> => {
await episodesStore.save({
EpisodeID: `${seriesId}-${episodeIndex}`,
Name: undefined === episodeName ? `Episode ${episodeIndex}` : episodeName,
Name: episodeName ?? `Episode ${episodeIndex}`,
SeriesID: seriesId,
Status: status,
StatusDate: statusDate,
Expand Down
3 changes: 2 additions & 1 deletion cypress/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"types": [
"cypress",
"node"
]
],
"target": "ES2019"
},
"exclude": [
"node_modules"
Expand Down
2 changes: 1 addition & 1 deletion spec/public/controllers/dataSync-controller_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ describe("DataSyncController", (): void => {
beforeEach((): SyncMock => (sync = new SyncMock(null, "1")));

scenarios.forEach((scenario: Scenario): void => {
describe(null === scenario.type ? "invalid sync type" : scenario.type, (): void => {
describe(scenario.type ?? "invalid sync type", (): void => {
let model: Model | undefined;

beforeEach(async (): Promise<void> => {
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/dataSync-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ export default class DataSyncController extends ViewController {
if (!response.ok) {
this.syncError("Save error", type, `Error saving ${type.toLowerCase()}`);
}
} catch (_e: unknown) {
} catch {
// No op
}

Expand Down
2 changes: 1 addition & 1 deletion src/controllers/episodes-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default class EpisodesController extends ViewController {
leftButton: {
eventHandler: this.goBack.bind(this) as NavButtonEventHandler,
style: "backButton",
label: undefined === this.listItem.source ? "Series" : this.listItem.source
label: this.listItem.source ?? "Series"
},
rightButton: {
eventHandler: this.addItem.bind(this) as NavButtonEventHandler,
Expand Down
9 changes: 2 additions & 7 deletions src/controllers/program-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,8 @@ export default class ProgramController extends ViewController {
public constructor(listItem?: ProgramListItem) {
super();

// If a list item was not passed, we're adding a new program
if (undefined === listItem) {
this.listItem = { program: new Program(null, "", 0, 0, 0, 0, 0) };
} else {
// Otherwise we're editing an existing program
this.listItem = listItem;
}
// If a list item was passed, we're editing an existing program, otherwise we're adding a new program
this.listItem = listItem ?? { program: new Program(null, "", 0, 0, 0, 0, 0) };
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/series-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export default class SeriesController extends ViewController {

// Set the series details
$("#seriesName").val(String(this.listItem.series.seriesName));
$("#nowShowing").val(null === this.listItem.series.nowShowing ? "" : String(this.listItem.series.nowShowing));
$("#nowShowing").val(this.listItem.series.nowShowing ?? "");
$("#moveTo").val(String(this.listItem.series.programId));
}

Expand Down
16 changes: 7 additions & 9 deletions src/models/episode-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export default class Episode extends Base {

try {
episodeList = await Promise.all((await (await this.db).episodesStore.listBySeries(seriesId)).map((ep: PersistedEpisode): Episode => new Episode(ep.EpisodeID, ep.Name, ep.Status, ep.StatusDate, ep.SeriesID, "true" === ep.Unverified, "true" === ep.Unscheduled, ep.Sequence, ep.SeriesName, ep.ProgramName)));
} catch (_e: unknown) {
} catch {
// No op
}

Expand All @@ -91,7 +91,7 @@ export default class Episode extends Base {

try {
episodeList = await Promise.all((await (await this.db).episodesStore.listByUnscheduled()).map((ep: PersistedEpisode): Episode => new Episode(ep.EpisodeID, ep.Name, ep.Status, ep.StatusDate, ep.SeriesID, "true" === ep.Unverified, "true" === ep.Unscheduled, ep.Sequence, ep.SeriesName, ep.ProgramName)));
} catch (_e: unknown) {
} catch {
// No op
}

Expand Down Expand Up @@ -121,7 +121,7 @@ export default class Episode extends Base {
if (undefined !== ep) {
({ EpisodeID, Name, Status, StatusDate, Unverified, Unscheduled, Sequence, SeriesID } = ep);
}
} catch (_e: unknown) {
} catch {
// No op
}

Expand All @@ -139,7 +139,7 @@ export default class Episode extends Base {

try {
count = await (await this.db).episodesStore.totalCount();
} catch (_e: unknown) {
} catch {
// No op
}

Expand All @@ -158,7 +158,7 @@ export default class Episode extends Base {

try {
count = await (await this.db).episodesStore.countByStatus(status);
} catch (_e: unknown) {
} catch {
// No op
}

Expand Down Expand Up @@ -204,9 +204,7 @@ export default class Episode extends Base {
*/
public async save(): Promise<string | undefined> {
// If an id has not been set (ie. is a new episode to be added), generate a new UUID
if (null === this.id) {
this.id = v4();
}
this.id ??= v4();

try {
await (await this.db).episodesStore.save({
Expand All @@ -221,7 +219,7 @@ export default class Episode extends Base {
});

return this.id;
} catch (_e: unknown) {
} catch {
// No op
}

Expand Down
14 changes: 6 additions & 8 deletions src/models/program-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default class Program extends Base {

try {
programList = await Promise.all((await (await this.db).programsStore.list()).map((prog: PersistedProgram): Program => new Program(prog.ProgramID, prog.Name, prog.SeriesCount, prog.EpisodeCount, prog.WatchedCount, prog.RecordedCount, prog.ExpectedCount)));
} catch (_e: unknown) {
} catch {
// No op
}

Expand All @@ -103,7 +103,7 @@ export default class Program extends Base {
if (undefined !== prog) {
({ ProgramID, Name } = prog);
}
} catch (_e: unknown) {
} catch {
// No op
}

Expand All @@ -121,7 +121,7 @@ export default class Program extends Base {

try {
count = await (await this.db).programsStore.count();
} catch (_e: unknown) {
} catch {
// No op
}

Expand Down Expand Up @@ -167,9 +167,7 @@ export default class Program extends Base {
*/
public async save(): Promise<string | undefined> {
// If an id has not been set (ie. is a new program to be added), generate a new UUID
if (null === this.id) {
this.id = v4();
}
this.id ??= v4();

try {
await (await this.db).programsStore.save({
Expand All @@ -178,7 +176,7 @@ export default class Program extends Base {
});

return this.id;
} catch (_e: unknown) {
} catch {
// No op
}

Expand Down Expand Up @@ -227,7 +225,7 @@ export default class Program extends Base {
* @desc Returns the group that the program belongs to
*/
public get programGroup(): string {
return null === this.programName ? "" : this.programName.substring(0, 1).toUpperCase();
return this.programName?.substring(0, 1).toUpperCase() ?? "";
}

/**
Expand Down
18 changes: 8 additions & 10 deletions src/models/series-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export default class Series extends Base {

try {
seriesList = await Promise.all((await (await this.db).seriesStore.listByProgram(programId)).map((series: PersistedSeries): Series => new Series(series.SeriesID, series.Name, series.NowShowing, series.ProgramID, series.ProgramName, series.EpisodeCount, series.WatchedCount, series.RecordedCount, series.ExpectedCount, series.MissedCount, series.StatusWarningCount)));
} catch (_e: unknown) {
} catch {
// No op
}

Expand All @@ -113,7 +113,7 @@ export default class Series extends Base {

try {
seriesList = await Promise.all((await (await this.db).seriesStore.listByNowShowing()).map((series: PersistedSeries): Series => new Series(series.SeriesID, series.Name, series.NowShowing, series.ProgramID, series.ProgramName, series.EpisodeCount, series.WatchedCount, series.RecordedCount, series.ExpectedCount, series.MissedCount, series.StatusWarningCount)));
} catch (_e: unknown) {
} catch {
// No op
}

Expand All @@ -132,7 +132,7 @@ export default class Series extends Base {

try {
seriesList = await Promise.all((await (await this.db).seriesStore.listByStatus(status)).map((series: PersistedSeries): Series => new Series(series.SeriesID, series.Name, series.NowShowing, series.ProgramID, series.ProgramName, series.EpisodeCount, series.WatchedCount, series.RecordedCount, series.ExpectedCount, series.MissedCount, series.StatusWarningCount)));
} catch (_e: unknown) {
} catch {
// No op
}

Expand All @@ -150,7 +150,7 @@ export default class Series extends Base {

try {
seriesList = await Promise.all((await (await this.db).seriesStore.listByIncomplete()).map((series: PersistedSeries): Series => new Series(series.SeriesID, series.Name, series.NowShowing, series.ProgramID, series.ProgramName, series.EpisodeCount, series.WatchedCount, series.RecordedCount, series.ExpectedCount, series.MissedCount, series.StatusWarningCount)));
} catch (_e: unknown) {
} catch {
// No op
}

Expand All @@ -176,7 +176,7 @@ export default class Series extends Base {
if (undefined !== series) {
({ SeriesID, Name, NowShowing, ProgramID } = series);
}
} catch (_e: unknown) {
} catch {
// No op
}

Expand All @@ -194,7 +194,7 @@ export default class Series extends Base {

try {
count = await (await this.db).seriesStore.count();
} catch (_e: unknown) {
} catch {
// No op
}

Expand Down Expand Up @@ -240,9 +240,7 @@ export default class Series extends Base {
*/
public async save(): Promise<string | undefined> {
// If an id has not been set (ie. is a new series to be added), generate a new UUID
if (null === this.id) {
this.id = v4();
}
this.id ??= v4();

try {
await (await this.db).seriesStore.save({
Expand All @@ -253,7 +251,7 @@ export default class Series extends Base {
});

return this.id;
} catch (_e: unknown) {
} catch {
// No op
}

Expand Down
4 changes: 2 additions & 2 deletions src/models/setting-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default class Setting extends Base {
if (undefined !== persistedSetting) {
({ name, value } = persistedSetting);
}
} catch (_e: unknown) {
} catch {
// No op
}

Expand All @@ -63,7 +63,7 @@ export default class Setting extends Base {
await (await this.db).settingsStore.save(String(this.settingName), String(this.settingValue));

return true;
} catch (_e: unknown) {
} catch {
return false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/models/sync-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default class Sync extends Base {

try {
syncList = await Promise.all((await (await this.db).syncsStore.list()).map((sync: PersistedSync): Sync => new Sync(sync.Type, sync.ID, sync.Action)));
} catch (_e: unknown) {
} catch {
// No op
}

Expand All @@ -64,7 +64,7 @@ export default class Sync extends Base {

try {
count = await (await this.db).syncsStore.count();
} catch (_e: unknown) {
} catch {
// No op
}

Expand Down
6 changes: 1 addition & 5 deletions src/stores/series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,7 @@ function create(db: IDBPDatabase<TVManagerDB>): SeriesStore {

// Get set of series with at least one episode in the specified status
(await txEpisodesStore.index("status").getAll(IDBKeyRange.bound([status], [status, "~"]))).forEach((episode: EpisodesStoreObject): void => {
let count: number | undefined = series.get(episode.seriesId);

if (undefined === count) {
count = 0;
}
const count: number = series.get(episode.seriesId) ?? 0;

series.set(episode.seriesId, count + 1);
});
Expand Down

0 comments on commit a6e9bf0

Please sign in to comment.