Skip to content

Commit

Permalink
=== undefined/nullを消す (#1747)
Browse files Browse the repository at this point in the history
* Delete: === undefined/nullを消す

* Fix: テストを修正

* Change: 名前を変更
  • Loading branch information
sevenc-nanashi authored Jan 24, 2024
1 parent 79da035 commit 1ca1585
Show file tree
Hide file tree
Showing 16 changed files with 43 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/background/configMigration014.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function ({
if (fs.existsSync(configPath)) {
const config = JSON.parse(fs.readFileSync(configPath, "utf-8"));

if (config?.__internal__?.migrations?.version === undefined) {
if (config?.__internal__?.migrations?.version == undefined) {
throw new Error("configMigration014: config.json is invalid");
}

Expand Down
10 changes: 5 additions & 5 deletions src/components/help/LibraryPolicy.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
<template v-if="engineInfos.size > 1">
<q-separator v-if="engineIndex > 0" spaced />
<q-item-label header>{{
mapUndefinedPipe(engineInfos.get(engineId), (v) => v.name)
mapNullablePipe(engineInfos.get(engineId), (v) => v.name)
}}</q-item-label>
</template>
<template
v-for="([, characterInfo], characterIndex) in mapUndefinedPipe(
v-for="([, characterInfo], characterIndex) in mapNullablePipe(
engineInfos.get(engineId),
(v) => v.characterInfos
)"
Expand Down Expand Up @@ -53,10 +53,10 @@
</div>
<div class="text-subtitle">
{{
mapUndefinedPipe(
mapNullablePipe(
engineInfos.get(selectedInfo.engine),
(v) => v.characterInfos,
(v) => mapUndefinedPipe(selectedInfo, (i) => v.get(i.character)),
(v) => mapNullablePipe(selectedInfo, (i) => v.get(i.character)),
(v) => v.metas.speakerName
)
}}
Expand All @@ -73,7 +73,7 @@ import { computed, ref } from "vue";
import { useStore } from "@/store";
import { useMarkdownIt } from "@/plugins/markdownItPlugin";
import { EngineId, SpeakerId } from "@/type/preload";
import { mapUndefinedPipe } from "@/helpers/map";
import { mapNullablePipe } from "@/helpers/map";
type DetailKey = { engine: EngineId; character: SpeakerId };
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/SelectionHelperForQInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class SelectionHelperForQInput {

// this.start が number | null なので null も受け付ける
setCursorPosition(index: number | null) {
if (index === null) return;
if (index == undefined) return;

this.nativeEl.selectionStart = this.nativeEl.selectionEnd = index;
}
Expand Down Expand Up @@ -50,7 +50,7 @@ export class SelectionHelperForQInput {
get isEmpty() {
const start = this.nativeEl.selectionStart;
const end = this.nativeEl.selectionEnd;
return start === null || end === null || start === end;
return start == undefined || end == undefined || start === end;
}

private get nativeEl() {
Expand Down
16 changes: 8 additions & 8 deletions src/helpers/map.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
export function mapUndefinedPipe<T, U1>(
export function mapNullablePipe<T, U1>(
source: T | undefined,
fn1: (_: NonNullable<T>) => U1 | undefined
): U1 | undefined;
export function mapUndefinedPipe<T, U1, U2>(
export function mapNullablePipe<T, U1, U2>(
source: T | undefined,
fn1: (_: NonNullable<T>) => U1 | undefined,
fn2: (_: NonNullable<U1>) => U2 | undefined
): U2 | undefined;
export function mapUndefinedPipe<T, U1, U2, U3>(
export function mapNullablePipe<T, U1, U2, U3>(
source: T | undefined,
fn1: (_: NonNullable<T>) => U1 | undefined,
fn2: (_: NonNullable<U1>) => U2 | undefined,
fn3: (_: NonNullable<U2>) => U3 | undefined
): U3 | undefined;
/**
* 一連の関数を実行する。途中でundefinedを返すとその後undefinedを返す
* 一連の関数を実行する。途中でundefinedかnullを返すとその後undefinedを返す
*/
// eslint-disable-next-line @typescript-eslint/ban-types, @typescript-eslint/no-explicit-any
export function mapUndefinedPipe(source: any, ...fn: Function[]) {
export function mapNullablePipe(source: any, ...fn: Function[]) {
return fn.reduce((prev, curr) => {
if (prev === undefined) {
if (prev == undefined) {
return undefined;
}
return curr(prev);
}, source);
}

export const undefinedToDefault = <T>(
export const nullableToDefault = <T>(
defaultValue: T,
maybeValue: T | undefined
): T => {
if (maybeValue === undefined) {
if (maybeValue == undefined) {
return defaultValue;
}
return maybeValue;
Expand Down
6 changes: 3 additions & 3 deletions src/helpers/previewSliderHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export const previewSliderHelper = (props: Props): PreviewSliderHelper => {
previewValue.value = value;
};
const changePreviewValue = async () => {
if (previewValue.value === null)
if (previewValue.value == null)
throw new Error("previewValue.value === null");
if (modelValue.value !== previewValue.value && props.onChange) {
await props.onChange(previewValue.value);
Expand All @@ -128,7 +128,7 @@ export const previewSliderHelper = (props: Props): PreviewSliderHelper => {
};
// start awaiting
const fireChange = () => {
if (awaitingChange !== null) awaitingChange.cancel();
if (awaitingChange != null) awaitingChange.cancel();
isAwaiting.value = true;
awaitingChange = new CancelableFinary(changePreviewValue(), endAwaiting);
};
Expand Down Expand Up @@ -165,7 +165,7 @@ export const previewSliderHelper = (props: Props): PreviewSliderHelper => {
);
// This function is called when the q-slider fire onWheel.
const onWheel = (event: Events["onWheel"]) => {
if (disableScroll.value || disable.value || currentValue.value === null)
if (disableScroll.value || disable.value || currentValue.value == null)
return;
event.preventDefault();
const deltaY = event.deltaY;
Expand Down
2 changes: 1 addition & 1 deletion src/infrastructures/EngineConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const OpenAPIEngineConnectorFactoryImpl = (): IEngineConnectorFactory => {
return {
instance: (host: string) => {
const cached = instanceMapper[host];
if (cached !== undefined) {
if (cached != undefined) {
return cached;
}
const api = new DefaultApi(new Configuration({ basePath: host }));
Expand Down
4 changes: 2 additions & 2 deletions src/store/audioPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const audioPlayerStoreState: AudioPlayerStoreState = {
export const audioPlayerStore = createPartialStore<AudioPlayerStoreTypes>({
ACTIVE_AUDIO_ELEM_CURRENT_TIME: {
getter: (state) => {
return state._activeAudioKey !== undefined
return state._activeAudioKey != undefined
? getAudioElement().currentTime
: undefined;
},
Expand Down Expand Up @@ -61,7 +61,7 @@ export const audioPlayerStore = createPartialStore<AudioPlayerStoreTypes>({
) {
const audioElement = getAudioElement();

if (offset !== undefined) {
if (offset != undefined) {
audioElement.currentTime = offset;
}

Expand Down
2 changes: 1 addition & 1 deletion src/store/dictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const dictionaryStore = createPartialStore<DictionaryStoreTypes>({
// 同じ単語IDで登録するために、1つのエンジンで登録したあと全エンジンに同期する。
const engineId: EngineId | undefined = state.engineIds[0];

if (engineId === undefined)
if (engineId == undefined)
throw new Error(`No such engine registered: index == 0`);
await dispatch("INSTANTIATE_ENGINE_CONNECTOR", {
engineId,
Expand Down
8 changes: 4 additions & 4 deletions src/store/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export const engineStore = createPartialStore<EngineStoreTypes>({
IS_ENGINE_READY: {
getter: (state) => (engineId) => {
const engineState: EngineState | undefined = state.engineStates[engineId];
if (engineState === undefined)
if (engineState == undefined)
throw new Error(`No such engineState set: engineId == ${engineId}`);

return engineState === "READY";
Expand All @@ -159,12 +159,12 @@ export const engineStore = createPartialStore<EngineStoreTypes>({
action: createUILockAction(
async ({ state, commit, dispatch }, { engineId }) => {
let engineState: EngineState | undefined = state.engineStates[engineId];
if (engineState === undefined)
if (engineState == undefined)
throw new Error(`No such engineState set: engineId == ${engineId}`);

for (let i = 0; i < 100; i++) {
engineState = state.engineStates[engineId]; // FIXME: explicit undefined
if (engineState === undefined)
if (engineState == undefined)
throw new Error(`No such engineState set: engineId == ${engineId}`);

if (engineState === "FAILED_STARTING") {
Expand Down Expand Up @@ -268,7 +268,7 @@ export const engineStore = createPartialStore<EngineStoreTypes>({
DETECTED_ENGINE_ERROR: {
action({ state, commit }, { engineId }) {
const engineState: EngineState | undefined = state.engineStates[engineId];
if (engineState === undefined)
if (engineState == undefined)
throw new Error(`No such engineState set: engineId == ${engineId}`);

switch (engineState) {
Expand Down
4 changes: 2 additions & 2 deletions src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export const indexStore = createPartialStore<IndexStoreTypes>({
const defaultStyleId = defaultStyleIds.find(
(styleId) => styleId.speakerUuid == speakerUuid
);
if (defaultStyleId === undefined) {
if (defaultStyleId == undefined) {
return true;
}

Expand Down Expand Up @@ -244,7 +244,7 @@ export const indexStore = createPartialStore<IndexStoreTypes>({
audioItem.voice.styleId
);

if (characterInfo === undefined)
if (characterInfo == undefined)
throw new Error("assert characterInfo !== undefined");

const speakerUuid = characterInfo.metas.speakerUuid;
Expand Down
6 changes: 3 additions & 3 deletions src/store/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ export const presetStore = createPartialStore<PresetStoreTypes>({

const presetConfig = await window.electron.getSetting("presets");
if (
presetConfig === undefined ||
presetConfig.items === undefined ||
presetConfig.keys === undefined
presetConfig == undefined ||
presetConfig.items == undefined ||
presetConfig.keys == undefined
)
return;
commit("SET_PRESET_ITEMS", {
Expand Down
6 changes: 3 additions & 3 deletions src/store/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export const projectStore = createPartialStore<ProjectStoreTypes>({

// set phoneme length
// 0.7 未満のプロジェクトファイルは styleId ではなく characterIndex なので、ここだけ characterIndex とした
if (audioItem.characterIndex === undefined)
if (audioItem.characterIndex == undefined)
throw new Error("audioItem.characterIndex === undefined");
await context
.dispatch("FETCH_MORA_DATA", {
Expand Down Expand Up @@ -227,7 +227,7 @@ export const projectStore = createPartialStore<ProjectStoreTypes>({
) {
for (const audioItemsKey in projectData.audioItems) {
const audioItem = projectData.audioItems[audioItemsKey];
if (audioItem.speaker !== null) {
if (audioItem.speaker != null) {
audioItem.styleId = audioItem.speaker;
delete audioItem.speaker;
}
Expand All @@ -239,7 +239,7 @@ export const projectStore = createPartialStore<ProjectStoreTypes>({
) {
for (const audioItemsKey in projectData.audioItems) {
const audioItem = projectData.audioItems[audioItemsKey];
if (audioItem.engineId === undefined) {
if (audioItem.engineId == undefined) {
audioItem.engineId = engineId;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/store/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const proxyStoreCreator = (_engineFactory: IEngineConnectorFactory) => {
action({ state }, payload) {
const engineId = payload.engineId;
const engineInfo: EngineInfo | undefined = state.engineInfos[engineId];
if (engineInfo === undefined)
if (engineInfo == undefined)
return Promise.reject(
new Error(`No such engineInfo registered: engineId == ${engineId}`)
);
Expand Down
2 changes: 1 addition & 1 deletion src/store/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ function replaceTag(
): string {
const result = template.replace(/\$(.+?)\$/g, (match, p1) => {
const replaceTagId = replaceTagStringToTagId[p1];
if (replaceTagId === undefined) {
if (replaceTagId == undefined) {
return match;
}
return replacer[replaceTagId] ?? "";
Expand Down
2 changes: 1 addition & 1 deletion src/type/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const failure: Failure = <C extends string>(
error: codeOrError,
};
} else if (codeOrError == undefined || typeof codeOrError === "string") {
if (error === undefined) {
if (error == undefined) {
throw new Error("Error must be specified");
}
return { ok: false as const, code: codeOrError, error };
Expand Down
15 changes: 5 additions & 10 deletions tests/unit/lib/map.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";

import { mapUndefinedPipe, undefinedToDefault } from "@/helpers/map";
import { mapNullablePipe, nullableToDefault } from "@/helpers/map";

type DummyType = {
outerValue?: {
Expand All @@ -19,7 +19,7 @@ describe("mapUndefinedPipe", () => {
};
const map = new Map<string, DummyType>([[key, value]]);
expect(
mapUndefinedPipe(
mapNullablePipe(
map.get(key),
(v) => v.outerValue,
(v) => v.innerValue
Expand All @@ -36,7 +36,7 @@ describe("mapUndefinedPipe", () => {
};
const map = new Map<string, DummyType>([[key, value]]);
expect(
mapUndefinedPipe(
mapNullablePipe(
map.get(key),
(v) => v.outerValue,
(v) => v.innerValue
Expand All @@ -48,16 +48,11 @@ describe("mapUndefinedPipe", () => {
describe("undefinedToDefault", () => {
it("値がある時はそのまま返す", () => {
const actualValue = "value";
expect(undefinedToDefault("test", actualValue)).toEqual(actualValue);
expect(nullableToDefault("test", actualValue)).toEqual(actualValue);
});

it("値がない時はdefaultValueを返す", () => {
const defaultValue = "test";
expect(undefinedToDefault(defaultValue, undefined)).toEqual(defaultValue);
});

it("undefinedのみを値がない状態とみなす", () => {
const defaultValue = "test";
expect(undefinedToDefault(defaultValue, null)).toBeNull();
expect(nullableToDefault(defaultValue, undefined)).toEqual(defaultValue);
});
});

0 comments on commit 1ca1585

Please sign in to comment.