Skip to content

Commit

Permalink
refactor: use isNil everywhere, replace Boolean with !!
Browse files Browse the repository at this point in the history
Signed-off-by: Fernando Fernández <ferferga@hotmail.com>
  • Loading branch information
ferferga committed Dec 29, 2024
1 parent 50fa008 commit a402b2b
Show file tree
Hide file tree
Showing 17 changed files with 42 additions and 33 deletions.
2 changes: 1 addition & 1 deletion frontend/scripts/virtual-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const vuetifyExports = localeNames
/**
* Get commit hash
*/
const commit_available = !Number(process.env.IS_STABLE) && Boolean(process.env.COMMIT_HASH);
const commit_available = !Number(process.env.IS_STABLE) && !!process.env.COMMIT_HASH;
const commit_hash = (commit_available && `'${process.env.COMMIT_HASH}'`) || undefined;

/**
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/Item/Card/ItemCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</template>
<template #upper-content>
<VProgressCircular
v-if="refreshProgress !== undefined"
v-if="!isNil(refreshProgress)"
:model-value="refreshProgress"
:indeterminate="refreshProgress === 0"
size="24" />
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/components/Item/Identify/IdentifyDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ import { useI18n } from 'vue-i18n';
import { useConfirmDialog } from '@/composables/use-confirm-dialog';
import { useSnackbar } from '@/composables/use-snackbar';
import { remote } from '@/plugins/remote';
import { isArray, isStr } from '@/utils/validation';
import { isArray, isNil, isStr } from '@/utils/validation';
interface IdentifyField {
key: string;
Expand Down Expand Up @@ -175,7 +175,7 @@ const searchFields = computed<IdentifyField[]>(() => {
const missingProviders = availableProviders
.filter(p => !populatedKeys.includes(p.Key ?? ''))
.map(p => p.Key)
.filter((p): p is string => p !== undefined);
.filter((p): p is string => !isNil(p));
for (const key of missingProviders) {
result.push({
Expand All @@ -195,7 +195,7 @@ const fieldsInputs = ref<IdentifyField[]>(
structuredClone(toRaw(searchFields.value))
);
const tabName = computed(() =>
searchResults.value === undefined ? 'searchMenu' : 'resultsMenu'
isNil(searchResults.value) ? 'searchMenu' : 'resultsMenu'
);
const progress = computed(() => {
switch (tabName.value) {
Expand Down Expand Up @@ -232,7 +232,7 @@ async function getItemRemoteSearch(
const searcher = remote.sdk.newUserApi(getItemLookupApi);
const itemId = item.Id;
if (itemId === undefined) {
if (isNil(itemId)) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/Item/ItemMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ import IMdiShuffle from 'virtual:icons/mdi/shuffle';
import { computed, getCurrentInstance, onMounted, shallowRef, useId, watch } from 'vue';
import { useI18n } from 'vue-i18n';
import { useRoute, useRouter } from 'vue-router';
import { isStr } from '@/utils/validation';
import { isNil, isStr } from '@/utils/validation';
import {
canIdentify,
canInstantMix,
Expand Down Expand Up @@ -157,7 +157,7 @@ watch(isActive, newVal =>
const errorMessage = t('anErrorHappened');
const isItemRefreshing = computed(
() => taskManager.getTask(item.Id ?? '') !== undefined
() => !isNil(taskManager.getTask(item.Id ?? ''))
);
const itemDeletionName = computed(() => {
const parentName = item.Name ?? undefined;
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/Item/MediaSourceSelector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
v-model="currentSource"
:items="selectSources"
:label="label"
:single-line="label === undefined"
:single-line="isNil(label)"
hide-details
class="text-truncate">
<template #selection="{ item: i }">
Expand All @@ -20,6 +20,7 @@
<script setup lang="ts">
import { computed, ref, watch } from 'vue';
import type { MediaSourceInfo } from '@jellyfin/sdk/lib/generated-client';
import { isNil } from '@/utils/validation';
import { getItemizedSelect } from '@/utils/forms';
const { sources, defaultSourceIndex, label } = defineProps<{
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/Item/MediaStreamSelector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import IMdiSurroundSound from 'virtual:icons/mdi/surround-sound';
import { watchImmediate } from '@vueuse/core';
import { getLocaleName } from '@/utils/i18n';
import { upperFirst } from '@/utils/data-manipulation';
import { isNil } from '@/utils/validation';
const { mediaStreams, type, defaultStreamIndex } = defineProps<{
mediaStreams: MediaStream[];
Expand Down Expand Up @@ -122,7 +123,7 @@ const trackIndex = ref<number | null>(defaultStreamIndex ?? mediaStreams.find(tr
*/
if (
(type === 'Video' || type === 'Audio')
&& trackIndex.value === null
&& isNil(trackIndex.value)
&& selectItems.value[0]
) {
// eslint-disable-next-line unicorn/no-null
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/components/Item/Metadata/MetadataEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ import { computed, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { watchImmediate } from '@vueuse/core';
import { getItemImageUrl } from '@/utils/images';
import { isArray } from '@/utils/validation';
import { isArray, isNil } from '@/utils/validation';
import { remote } from '@/plugins/remote';
import { useSnackbar } from '@/composables/use-snackbar';
import { useDateFns } from '@/composables/use-datefns';
Expand Down Expand Up @@ -266,7 +266,7 @@ const contentOption = ref<ContentOption>();
const contentType = ref<string>();
const genresModel = computed({
get() {
return metadata.value?.Genres === null ? undefined : metadata.value?.Genres;
return metadata.value?.Genres ?? undefined;
},
set(newVal) {
if (isArray(newVal) && metadata.value) {
Expand All @@ -276,7 +276,7 @@ const genresModel = computed({
});
const tagsModel = computed({
get() {
return metadata.value?.Tags === null ? undefined : metadata.value?.Tags;
return metadata.value?.Tags ?? undefined;
},
set(newVal) {
if (isArray(newVal) && metadata.value) {
Expand Down Expand Up @@ -341,7 +341,7 @@ async function getData(): Promise<void> {
value: r.Value ?? ''
};
}
}).filter((r): r is ContentOption => r !== undefined) ?? [];
}).filter((r): r is ContentOption => !isNil(r)) ?? [];
contentOption.value
= contentOptions.value.find(r => r.value === options.ContentType)
?? contentOptions.value[0];
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/Item/Metadata/PersonEditor.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<VDialog
max-width="30%"
:model-value="person !== undefined"
:model-value="!isNil(person)"
@update:model-value="emit('close')">
<VCard>
<VCardTitle>{{ t('editPerson') }}</VCardTitle>
Expand Down Expand Up @@ -83,6 +83,7 @@ import { useI18n } from 'vue-i18n';
import { type BaseItemPerson, ImageType } from '@jellyfin/sdk/lib/generated-client';
import { watchImmediate } from '@vueuse/core';
import { getItemImageUrl } from '@/utils/images';
import { isNil } from '@/utils/validation';
const { person } = defineProps<{ person: BaseItemPerson | undefined }>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
<template #append>
<VProgressCircular
v-if="task.progress !== 100"
:indeterminate="
task.progress === undefined || task.progress === 0
"
:indeterminate="!!task.progress"
:model-value="task.progress"
size="24" />
<VIcon v-else>
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/composables/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ function _sharedInternalLogic<T extends Record<K, (...args: any[]) => any>, K ex
ops: Required<ComposableOps>
): (this: any, ...args: ComposableParams<T, K, U>) => Promise<ReturnPayload<T, K, typeof ofBaseItem>> | ReturnPayload<T, K, typeof ofBaseItem> {
const offlineParams: OfflineParams<T, K>[] = [];
const isFuncDefined = (): boolean => unref(api) !== undefined && unref(methodName) !== undefined;
const isFuncDefined = (): boolean => !isNil(unref(api)) && !isNil(unref(methodName));

const loading = shallowRef<boolean | undefined>(false);
const argsRef = shallowRef<Parameters<T[K]>>();
Expand All @@ -205,7 +205,7 @@ function _sharedInternalLogic<T extends Record<K, (...args: any[]) => any>, K ex

return currentCachedRequest;
});
const isCached = computed(() => Boolean(cachedData.value));
const isCached = computed(() => !!cachedData.value);
const data = computed<ReturnData<T, K, typeof ofBaseItem>>(() => {
if (ops.skipCache.request && result.value) {
if (ofBaseItem) {
Expand Down Expand Up @@ -276,7 +276,7 @@ function _sharedInternalLogic<T extends Record<K, (...args: any[]) => any>, K ex

argsRef.value = normalizeArgs();

if (getCurrentScope() !== undefined) {
if (!isNil(getCurrentScope())) {
const handleArgsChange = async (_: typeof args, old: typeof args | undefined): Promise<void> => {
const normalizedArgs = normalizeArgs();

Expand Down
3 changes: 2 additions & 1 deletion frontend/src/pages/settings/apikeys.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
" />
<VDialog
width="auto"
:model-value="confirmRevoke !== undefined"
:model-value="!isNil(confirmRevoke)"
@update:model-value="confirmRevoke = undefined">
<VCard>
<VCardText>
Expand Down Expand Up @@ -115,6 +115,7 @@ import { useI18n } from 'vue-i18n';
import { remote } from '@/plugins/remote';
import { useSnackbar } from '@/composables/use-snackbar';
import { useDateFns } from '@/composables/use-datefns';
import { isNil } from '@/utils/validation';
const { t } = useI18n();
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/pages/settings/devices.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
</VCol>
<VDialog
width="auto"
:model-value="confirmDelete !== undefined"
:model-value="!isNil(confirmDelete)"
@update:model-value="confirmDelete = undefined">
<VCard>
<VCardText>
Expand Down Expand Up @@ -107,6 +107,7 @@ import { useI18n } from 'vue-i18n';
import { remote } from '@/plugins/remote';
import { useSnackbar } from '@/composables/use-snackbar';
import { useDateFns } from '@/composables/use-datefns';
import { isNil } from '@/utils/validation';
const { t } = useI18n();
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/store/player-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class PlayerElementStore extends CommonStore<PlayerElementState, 'isStretched' |
srcLang: sub.Language ?? undefined,
type: sub.DeliveryMethod ?? SubtitleDeliveryMethod.Drop,
srcIndex: sub.srcIndex,
codec: sub.Codec === null ? undefined : sub.Codec?.toLowerCase()
codec: sub.Codec?.toLowerCase()
}))
);

Expand All @@ -134,7 +134,7 @@ class PlayerElementStore extends CommonStore<PlayerElementState, 'isStretched' |
*/
public readonly currentItemExternalParsedSubtitleTracks = computed(() =>
this.currentItemParsedSubtitleTracks.value?.filter(
sub => sub.codec !== undefined && sub.src !== undefined
sub => !isNil(sub.codec) && !isNil(sub.src)
)
);

Expand Down
6 changes: 3 additions & 3 deletions frontend/src/store/task-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { watch } from 'vue';
import { CommonStore } from '@/store/super/common-store';
import { remote } from '@/plugins/remote';
import { apiStore } from '@/store/api';
import { isArray, isObj, isStr, sealed } from '@/utils/validation';
import { isArray, isNil, isObj, isStr, sealed } from '@/utils/validation';

/**
* == INTERFACES AND TYPES ==
Expand Down Expand Up @@ -62,7 +62,7 @@ class TaskManagerStore extends CommonStore<TaskManagerState, 'tasks'> {
);
}

if (this.getTask(task.id) === undefined) {
if (isNil(this.getTask(task.id))) {
this._state.value.tasks.push(task);
}
};
Expand Down Expand Up @@ -124,7 +124,7 @@ class TaskManagerStore extends CommonStore<TaskManagerState, 'tasks'> {
* Start task if update its received and it doesn't exist in the store.
* Usually when a running task is started somewhere else and the client is accssed later
*/
if (taskPayload === undefined) {
if (isNil(taskPayload)) {
const item = apiStore.getItemById(data.ItemId);

if (item?.Id && item.Name) {
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/utils/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export function canIdentify(item: BaseItemDto): boolean {
* @returns Whether the item can be played on this client or not
*/
export function canPlay(item: BaseItemDto | undefined): boolean {
if (item === undefined) {
if (isNil(item)) {
return false;
}

Expand Down Expand Up @@ -257,7 +257,7 @@ export function canPlay(item: BaseItemDto | undefined): boolean {
* Check if an item can be resumed
*/
export function canResume(item: BaseItemDto): boolean {
return Boolean(item.UserData?.PlaybackPositionTicks && item.UserData.PlaybackPositionTicks > 0);
return !!(item.UserData?.PlaybackPositionTicks && item.UserData.PlaybackPositionTicks > 0);
}
/**
* Determine if an item can be mark as played
Expand Down Expand Up @@ -470,7 +470,7 @@ export function getItemIdFromSourceIndex(
item: BaseItemDto,
sourceIndex?: number
): string {
if (sourceIndex === undefined) {
if (isNil(sourceIndex)) {
return item.Id ?? '';
}

Expand Down
8 changes: 7 additions & 1 deletion frontend/src/utils/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ export function isFunc(value: unknown): value is (...args: unknown[]) => unknown
* Check if the value is undefined
*/
export function isUndef(value: unknown): value is undefined {
return value === undefined;
/**
* typeof val === 'undefined' will never throw an error, even if the variable is not declared,
* while val === undefined will.
* It's also faster than val === undefined and handles better the void operator
*/
// eslint-disable-next-line unicorn/no-typeof-undefined
return typeof value === 'undefined';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/configs/lint/rules/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import gitignore from 'eslint-config-flat-gitignore';
import fileProgress from 'eslint-plugin-file-progress';
import { eqeqeqConfig } from '../shared';

const CI_environment = Boolean(process.env.CI);
const CI_environment = !!process.env.CI;

/**
* Gets ESLint's minimal configuration for the monorepo
Expand Down

0 comments on commit a402b2b

Please sign in to comment.