From 330f234b5a02c662a19a62fb5d52e9eaf6932401 Mon Sep 17 00:00:00 2001 From: Bruno Windels <274386+bwindels@users.noreply.github.com> Date: Tue, 26 Apr 2022 14:21:19 +0200 Subject: [PATCH] prefer undefined over null --- src/domain/avatar.ts | 4 ++-- src/matrix/net/MediaRepository.ts | 13 ++++++------- src/platform/web/ui/general/TemplateView.ts | 2 +- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/domain/avatar.ts b/src/domain/avatar.ts index 6f1ef8b0..ecaccebe 100644 --- a/src/domain/avatar.ts +++ b/src/domain/avatar.ts @@ -51,10 +51,10 @@ export function getIdentifierColorNumber(id: string): number { return (hashCode(id) % 8) + 1; } -export function getAvatarHttpUrl(avatarUrl: string, cssSize: number, platform: Platform, mediaRepository: MediaRepository): string | null { +export function getAvatarHttpUrl(avatarUrl: string | undefined, cssSize: number, platform: Platform, mediaRepository: MediaRepository): string | undefined { if (avatarUrl) { const imageSize = cssSize * platform.devicePixelRatio; return mediaRepository.mxcUrlThumbnail(avatarUrl, imageSize, imageSize, "crop"); } - return null; + return undefined; } diff --git a/src/matrix/net/MediaRepository.ts b/src/matrix/net/MediaRepository.ts index 357b17c6..e95ed60c 100644 --- a/src/matrix/net/MediaRepository.ts +++ b/src/matrix/net/MediaRepository.ts @@ -29,32 +29,31 @@ export class MediaRepository { this._platform = platform; } - mxcUrlThumbnail(url: string, width: number, height: number, method: "crop" | "scale"): string | null { + mxcUrlThumbnail(url: string, width: number, height: number, method: "crop" | "scale"): string | undefined { const parts = this._parseMxcUrl(url); if (parts) { const [serverName, mediaId] = parts; const httpUrl = `${this._homeserver}/_matrix/media/r0/thumbnail/${encodeURIComponent(serverName)}/${encodeURIComponent(mediaId)}`; return httpUrl + "?" + encodeQueryParams({width: Math.round(width), height: Math.round(height), method}); } - return null; + return undefined; } - mxcUrl(url: string): string | null { + mxcUrl(url: string): string | undefined { const parts = this._parseMxcUrl(url); if (parts) { const [serverName, mediaId] = parts; return `${this._homeserver}/_matrix/media/r0/download/${encodeURIComponent(serverName)}/${encodeURIComponent(mediaId)}`; - } else { - return null; } + return undefined; } - private _parseMxcUrl(url: string): string[] | null { + private _parseMxcUrl(url: string): string[] | undefined { const prefix = "mxc://"; if (url.startsWith(prefix)) { return url.substr(prefix.length).split("/", 2); } else { - return null; + return undefined; } } diff --git a/src/platform/web/ui/general/TemplateView.ts b/src/platform/web/ui/general/TemplateView.ts index b46de78c..3b65ed9c 100644 --- a/src/platform/web/ui/general/TemplateView.ts +++ b/src/platform/web/ui/general/TemplateView.ts @@ -337,7 +337,7 @@ export class TemplateBuilder { // Special case of mapView for a TemplateView. // Always creates a TemplateView, if this is optional depending // on mappedValue, use `if` or `mapView` - map(mapFn: (value: T) => R, renderFn: (mapped: R, t: Builder, vm: T) => ViewNode): ViewNode { + map(mapFn: (value: T) => R, renderFn: (mapped: R, t: Builder, vm: T) => ViewNode | undefined): ViewNode { return this.mapView(mapFn, mappedValue => { return new InlineTemplateView(this._value, (t, vm) => { const rootNode = renderFn(mappedValue, t, vm);