-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Hiroshiba <[email protected]>
- Loading branch information
Showing
9 changed files
with
347 additions
and
232 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
export function mapUndefinedPipe<T, U1>( | ||
source: T | undefined, | ||
fn1: (_: NonNullable<T>) => U1 | undefined | ||
): U1 | undefined; | ||
export function mapUndefinedPipe<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>( | ||
source: T | undefined, | ||
fn1: (_: NonNullable<T>) => U1 | undefined, | ||
fn2: (_: NonNullable<U1>) => U2 | undefined, | ||
fn3: (_: NonNullable<U2>) => U3 | undefined | ||
): U3 | undefined; | ||
/** | ||
* 一連の関数を実行する。途中でundefinedを返すとその後undefinedを返す。 | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/ban-types, @typescript-eslint/no-explicit-any | ||
export function mapUndefinedPipe(source: any, ...fn: Function[]) { | ||
return fn.reduce((prev, curr) => { | ||
if (prev === undefined) { | ||
return undefined; | ||
} | ||
return curr(prev); | ||
}, source); | ||
} | ||
|
||
export const undefinedToDefault = <T>( | ||
defaultValue: T, | ||
maybeValue: T | undefined | ||
): T => { | ||
if (maybeValue === undefined) { | ||
return defaultValue; | ||
} | ||
return maybeValue; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import { mapUndefinedPipe, undefinedToDefault } from "@/helpers/map"; | ||
|
||
type DummyType = { | ||
outerValue?: { | ||
innerValue?: string; | ||
}; | ||
}; | ||
|
||
describe("mapUndefinedPipe", () => { | ||
it("値をunwrap出来る", () => { | ||
const key = "test"; | ||
const innerValue = "value"; | ||
const value: DummyType = { | ||
outerValue: { | ||
innerValue, | ||
}, | ||
}; | ||
const map = new Map<string, DummyType>([[key, value]]); | ||
expect( | ||
mapUndefinedPipe( | ||
map.get(key), | ||
(v) => v.outerValue, | ||
(v) => v.innerValue | ||
) | ||
).toEqual(innerValue); | ||
}); | ||
|
||
it("途中でundefinedを返すとその後undefinedを返す", () => { | ||
const key = "test"; | ||
const value: DummyType = { | ||
outerValue: { | ||
innerValue: undefined, | ||
}, | ||
}; | ||
const map = new Map<string, DummyType>([[key, value]]); | ||
expect( | ||
mapUndefinedPipe( | ||
map.get(key), | ||
(v) => v.outerValue, | ||
(v) => v.innerValue | ||
) | ||
).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe("undefinedToDefault", () => { | ||
it("値がある時はそのまま返す", () => { | ||
const actualValue = "value"; | ||
expect(undefinedToDefault("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(); | ||
}); | ||
}); |