null/undefined cleanup

This commit is contained in:
Patrick Haßel 2024-10-17 15:38:26 +02:00
parent 99407a8cdf
commit 3cfc0dd890
4 changed files with 18 additions and 18 deletions

View File

@ -40,7 +40,7 @@ export class ApiService {
.subscribe(next); .subscribe(next);
} }
getNone<T>(path: any[], next: Next<void> | undefined = undefined): Subscription { getNone<T>(path: any[], next?: Next<void>): Subscription {
return this.http.get<void>(getUrl('http', path)).subscribe(next); return this.http.get<void>(getUrl('http', path)).subscribe(next);
} }
@ -48,7 +48,7 @@ export class ApiService {
return this.http.get(getUrl('http', path), {responseType: "text"}).subscribe(next); return this.http.get(getUrl('http', path), {responseType: "text"}).subscribe(next);
} }
getSingle<T>(path: any[], fromJson: FromJson<T>, next: Next<T> | undefined = undefined): Subscription { getSingle<T>(path: any[], fromJson: FromJson<T>, next?: Next<T>): Subscription {
return this.http.get<any>(getUrl('http', path)).pipe(map(fromJson)).subscribe(next); return this.http.get<any>(getUrl('http', path)).pipe(map(fromJson)).subscribe(next);
} }
@ -56,11 +56,11 @@ export class ApiService {
return this.http.get<any[]>(getUrl('http', path)).pipe(map(list => list.map(fromJson))).subscribe(next); return this.http.get<any[]>(getUrl('http', path)).pipe(map(list => list.map(fromJson))).subscribe(next);
} }
postNone(path: any[], data: any, next: Next<void> | undefined = undefined): Subscription { postNone(path: any[], data: any, next?: Next<void>): Subscription {
return this.http.post<void>(getUrl('http', path), data).subscribe(next); return this.http.post<void>(getUrl('http', path), data).subscribe(next);
} }
postSingle<T>(path: any[], data: any, fromJson: FromJson<T>, next: Next<T> | undefined = undefined): Subscription { postSingle<T>(path: any[], data: any, fromJson: FromJson<T>, next?: Next<T>): Subscription {
return this.http.post<any>(getUrl('http', path), data).pipe(map(fromJson)).subscribe(next); return this.http.post<any>(getUrl('http', path), data).pipe(map(fromJson)).subscribe(next);
} }

View File

@ -30,7 +30,7 @@ export class Value implements IValue {
} }
static map(iValue: IValue | null, mapping: GetValue): Value | null { static map(iValue: IValue | null, mapping: GetValue): Value | null {
if (!iValue || !iValue.value) { if (iValue === null || iValue.value === null) {
return null; return null;
} }
const value = mapping(iValue.value); const value = mapping(iValue.value);
@ -54,14 +54,14 @@ export class Value implements IValue {
} }
static bi(a: IValue | null, b: IValue | null, combineValue: CombineValue) { static bi(a: IValue | null, b: IValue | null, combineValue: CombineValue) {
if (!a) { if (a === null) {
return new Value(null, null, b?.unit || null); return new Value(null, null, b?.unit || null);
} }
if (!b) { if (b === null) {
return new Value(null, null, a?.unit || null); return new Value(null, null, a?.unit || null);
} }
const oldestDate = !a.date || !b.date ? null : dateMin(a.date, b.date); const oldestDate = a.date === null || b.date === null ? null : dateMin(a.date, b.date);
const difference = !a.value || !b.value ? null : combineValue(a.value, b.value); const difference = a.value === null || b.value === null ? null : combineValue(a.value, b.value);
return new Value(oldestDate, difference, a.unit); return new Value(oldestDate, difference, a.unit);
} }

View File

@ -1,42 +1,42 @@
import {FromJson} from "./types"; import {FromJson} from "./types";
export function validateNumber(json: any): number { export function validateNumber(json: any): number {
if (!(typeof json == "number")) { if (!(typeof json === "number")) {
throw new Error("Not a number: " + json + " (" + typeof json + "): " + JSON.stringify(json)); throw new Error("Not a number: " + json + " (" + typeof json + "): " + JSON.stringify(json));
} }
return json; return json;
} }
export function validateNumberOrNull(json: any): number | null { export function validateNumberOrNull(json: any): number | null {
if (json == null) { if (json === null || json === undefined) {
return null; return null;
} }
return validateNumber(json); return validateNumber(json);
} }
export function validateBoolean(json: any): boolean { export function validateBoolean(json: any): boolean {
if (!(typeof json == "boolean")) { if (!(typeof json === "boolean")) {
throw new Error("Not a boolean: " + json + " (" + typeof json + "): " + JSON.stringify(json)); throw new Error("Not a boolean: " + json + " (" + typeof json + "): " + JSON.stringify(json));
} }
return json; return json;
} }
export function validateBooleanOrNull(json: any): boolean | null { export function validateBooleanOrNull(json: any): boolean | null {
if (json == null) { if (json === null || json === undefined) {
return null; return null;
} }
return validateBoolean(json); return validateBoolean(json);
} }
export function validateString(json: any): string { export function validateString(json: any): string {
if (!(typeof json == "string")) { if (!(typeof json === "string")) {
throw new Error("Not a string: " + json + " (" + typeof json + "): " + JSON.stringify(json)); throw new Error("Not a string: " + json + " (" + typeof json + "): " + JSON.stringify(json));
} }
return json; return json;
} }
export function validateStringOrNull(json: any): string | null { export function validateStringOrNull(json: any): string | null {
if (json == null) { if (json === null || json === undefined) {
return null; return null;
} }
return validateString(json); return validateString(json);
@ -47,7 +47,7 @@ export function validateDate(json: any): Date {
} }
export function validateDateOrNull(json: any): Date | null { export function validateDateOrNull(json: any): Date | null {
if (json == null) { if (json === null || json === undefined) {
return null; return null;
} }
return validateDate(json); return validateDate(json);
@ -58,7 +58,7 @@ export function validateList<T>(json: any, fromJson: FromJson<T>): T[] {
} }
export function validateLocalDateOrNull(json: any): Date | null { export function validateLocalDateOrNull(json: any): Date | null {
if (json == null) { if (json === null || json === undefined) {
return null; return null;
} }
return validateLocalDate(json); return validateLocalDate(json);

View File

@ -57,7 +57,7 @@ export class DashboardComponent implements OnInit {
if (fresh.name !== name) { if (fresh.name !== name) {
return old; return old;
} }
if (old == null || old.lastDate.getTime() <= fresh.lastDate.getTime()) { if (old === null || old.lastDate.getTime() <= fresh.lastDate.getTime()) {
return fresh; return fresh;
} }
return old; return old;