Skip to content

Commit

Permalink
feat: make parseStringParam return type depend on defaultValue
Browse files Browse the repository at this point in the history
  • Loading branch information
thevuong committed Oct 24, 2023
1 parent 2d396c5 commit 9f639f9
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions packages/lib/src/search-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ function parseQueryValues(value: string[] | string): string[] {
return Array.isArray(value) ? value : [value];
}

export function parseStringParam(
export function parseStringParam<T extends string | undefined>(
paramValue: string[] | string | undefined,
defaultValue: string | undefined = undefined,
): string | undefined {
defaultValue: T = undefined as T,
): string | T {
const parsedValue = parseQueryValue(paramValue);

return parsedValue || defaultValue;
}

export function parseNumberParam(
export function parseNumberParam<T extends number | undefined>(
paramValue: string[] | string | undefined,
defaultValue: number | undefined = NaN,
): number {
defaultValue: T = undefined as T,
): number | T {
const parsedValue = parseQueryValue(paramValue);

return Number(parsedValue) || defaultValue;
Expand All @@ -37,21 +37,21 @@ export function parseJoinedStringParam(
return parsedValue?.split(separator) || [];
}

export function parseStringParams(
export function parseStringParams<T extends string[] | undefined>(
paramValue: string[] | string | undefined,
defaultValue: string[] | undefined = undefined,
): string[] | undefined {
defaultValue: T = undefined as T,
): string[] | T {
if (!paramValue || paramValue.length === 0) {
return defaultValue;
}

return parseQueryValues(paramValue);
}

export function parseNumberParams(
export function parseNumberParams<T extends number[] | undefined>(
paramValue: string[] | string | undefined,
defaultValue: number[] | undefined = undefined,
): number[] | undefined {
defaultValue: T = undefined as T,
): number[] | T {
if (!paramValue) {
return defaultValue;
}
Expand Down

0 comments on commit 9f639f9

Please sign in to comment.