Skip to content

Commit

Permalink
Specify an error message.
Browse files Browse the repository at this point in the history
  • Loading branch information
arran4 committed Jun 30, 2023
1 parent ded776a commit 2e174d4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,29 @@ By using the `props` component you both have created:
```typescript
export function GetDateArrayPropOrDefaultFunction<R>(props: Record<string, any> | undefined | null, prop: string, defaultFunction: () => R): Date[] | R;
export function GetDateArrayPropOrDefault<R>(props: Record<string, any> | undefined | null, prop: string, defaultValue: R): Date[] | R;
export function GetDateArrayPropOrThrow(props: Record<string, any> | undefined | null, prop: string): Date[];
export function GetDateArrayPropOrThrow(props: Record<string, any> | undefined | null, prop: string, errorMessage?: string): Date[];
export function GetDatePropOrDefaultFunction<R>(props: Record<string, any> | undefined | null, prop: string, defaultFunction: () => R): R | Date;
export function GetDatePropOrDefault<R>(props: Record<string, any> | undefined | null, prop: string, defaultValue: R): R | Date;
export function GetDatePropOrThrow(props: Record<string, any> | undefined | null, prop: string): Date;
export function GetDatePropOrThrow(props: Record<string, any> | undefined | null, prop: string, errorMessage?: string): Date;
export function GetNumberPropOrDefaultFunction<R extends number | null>(props: Record<string, any> | undefined | null, prop: string, defaultFunction: () => R): R;
export function GetNumberPropOrDefault<R extends number | null>(props: Record<string, any> | undefined | null, prop: string, defaultValue: R): R;
export function GetNumberPropOrThrow<R extends number | null>(props: Record<string, any> | undefined | null, prop: string): R;
export function GetNumberPropOrThrow<R extends number | null>(props: Record<string, any> | undefined | null, prop: string, errorMessage?: string): R;
export function GetObjectArrayFunctionPropOrDefault<Y, X extends Y[] | null>(props: Record<string, any> | undefined | null, prop: string, constructorFunc: ConstructorFunc<Y>, defaultValue: X): X;
export function GetObjectArrayFunctionPropOrThrow<Y, X extends Y[] | null>(props: Record<string, any> | undefined | null, prop: string, constructorFunc: ConstructorFunc<Y>): X;
export function GetObjectArrayPropOrDefaultFunction<Y, X extends Y[] | null>(props: Record<string, any> | undefined | null, prop: string, constructorFunc: ConstructorFunc<Y>, defaultValue: () => X): X;
export function GetObjectArrayPropOrDefault<Y, X extends Y[] | null>(props: Record<string, any> | undefined | null, prop: string, defaultValue: X): X;
export function GetObjectArrayPropOrThrow<Y, X extends Y[] | null>(props: Record<string, any> | undefined | null, prop: string): X;
export function GetObjectArrayPropOrThrow<Y, X extends Y[] | null>(props: Record<string, any> | undefined | null, prop: string, errorMessage?: string): X;
export function GetObjectFunctionPropOrDefault<Y>(props: Record<string, any> | undefined | null, prop: string, constructorFunc: ConstructorFunc<Y>, defaultValue: Y): Y;
export function GetObjectFunctionPropOrThrow<Y>(props: Record<string, any> | undefined | null, prop: string, constructorFunc: ConstructorFunc<Y>): Y;
export function GetObjectPropOrDefaultFunction<Y>(props: Record<string, any> | undefined | null, prop: string, constructorFunc: ConstructorFunc<Y>, defaultValue: () => Y): Y;
export function GetObjectPropOrDefault<Y>(props: Record<string, any> | undefined | null, prop: string, defaultValue: Y): Y;
export function GetObjectPropOrThrow<Y>(props: Record<string, any> | undefined | null, prop: string): Y;
export function GetObjectPropOrThrow<Y>(props: Record<string, any> | undefined | null, prop: string, errorMessage?: string): Y;
export function GetStringArrayPropOrDefaultFunction<R>(props: Record<string, any> | undefined | null, prop: string, defaultFunction: () => R): string[] | R;
export function GetStringArrayPropOrDefault<R>(props: Record<string, any> | undefined | null, prop: string, defaultValue: R): string[] | R;
export function GetStringArrayPropOrThrow<T>(props: Record<string, any> | undefined | null, prop: string): string[];
export function GetStringArrayPropOrThrow<T>(props: Record<string, any> | undefined | null, prop: string, errorMessage?: string): string[];
export function GetStringPropOrDefaultFunction<R extends string | null>(props: Record<string, any> | undefined | null, prop: string, defaultFunction: () => R): R;
export function GetStringPropOrDefault<R extends string | null>(props: Record<string, any> | undefined | null, prop: string, defaultValue: R): R;
export function GetStringPropOrThrow<R extends string | null>(props: Record<string, any> | undefined | null, prop: string): R;
export function GetStringPropOrThrow<R extends string | null>(props: Record<string, any> | undefined | null, prop: string, errorMessage?: string): R;
export type ConstructorFunc<Y> = (params: Partial<Exclude<Y | undefined, null>>) => Y;
```

Expand Down
20 changes: 10 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function GetStringPropOrDefault<R extends string | null>(props: Record<st
return GetStringPropOrDefaultFunction<R>(props, prop, () => defaultValue);
}

export function GetStringPropOrThrow<R extends string | null>(props: Record<string, any> | undefined | null, prop: string): R {
export function GetStringPropOrThrow<R extends string | null>(props: Record<string, any> | undefined | null, prop: string, message? : string): R {
if (props) {
if (prop in props) {
let v = props[prop];
Expand All @@ -19,7 +19,7 @@ export function GetStringPropOrThrow<R extends string | null>(props: Record<stri
}
}
}
throw new Error(`${prop} not found as string in ${typeof props}`)
throw new Error(message ?? `${prop} not found as string in ${typeof props}`)
}

export function GetNumberPropOrDefaultFunction<R extends number | null>(props: Record<string, any> | undefined | null, prop: string, defaultFunction: () => R): R {
Expand All @@ -34,7 +34,7 @@ export function GetNumberPropOrDefault<R extends number | null>(props: Record<st
return GetNumberPropOrDefaultFunction<R>(props, prop, () => defaultValue);
}

export function GetNumberPropOrThrow<R extends number | null>(props: Record<string, any> | undefined | null, prop: string): R {
export function GetNumberPropOrThrow<R extends number | null>(props: Record<string, any> | undefined | null, prop: string, message? : string): R {
if (props) {
if (prop in props) {
let v = props[prop];
Expand All @@ -46,7 +46,7 @@ export function GetNumberPropOrThrow<R extends number | null>(props: Record<stri
}
}
}
throw new Error(`${prop} not found as number in ${typeof props}`)
throw new Error(message ?? `${prop} not found as number in ${typeof props}`)
}

export function GetDatePropOrDefaultFunction<R>(props: Record<string, any> | undefined | null, prop: string, defaultFunction: () => R): R | Date {
Expand Down Expand Up @@ -89,7 +89,7 @@ export function GetStringArrayPropOrDefault<R>(props: Record<string, any> | unde
return GetStringArrayPropOrDefaultFunction(props, prop, () => defaultValue);
}

export function GetStringArrayPropOrThrow<T>(props: Record<string, any> | undefined | null, prop: string): string[] {
export function GetStringArrayPropOrThrow<T>(props: Record<string, any> | undefined | null, prop: string, message? : string): string[] {
if (props) {
if (prop in props) {
let v = props[prop];
Expand All @@ -98,7 +98,7 @@ export function GetStringArrayPropOrThrow<T>(props: Record<string, any> | undefi
}
}
}
throw new Error(`${prop} not found as string[] in ${typeof props}`)
throw new Error(message ?? `${prop} not found as string[] in ${typeof props}`)
}

export function GetDateArrayPropOrDefaultFunction<R>(props: Record<string, any> | undefined | null, prop: string, defaultFunction: () => R): Date[] | R {
Expand Down Expand Up @@ -141,7 +141,7 @@ export function GetObjectPropOrThrow<Y>(props: Record<string, any> | undefined |
return GetObjectFunctionPropOrThrow<Y>(props, prop, (e) => e as Y)
}

export function GetObjectFunctionPropOrThrow<Y>(props: Record<string, any> | undefined | null, prop: string, constructorFunc: ConstructorFunc<Y>): Y {
export function GetObjectFunctionPropOrThrow<Y>(props: Record<string, any> | undefined | null, prop: string, constructorFunc: ConstructorFunc<Y>, message? : string): Y {
if (props) {
if (prop in props) {
let v = props[prop];
Expand All @@ -150,7 +150,7 @@ export function GetObjectFunctionPropOrThrow<Y>(props: Record<string, any> | und
}
}
}
throw new Error(`${prop} not found as object in ${typeof props}`)
throw new Error(message ?? `${prop} not found as object in ${typeof props}`)
}

export function GetObjectPropOrDefault<Y>(props: Record<string, any> | undefined | null, prop: string, defaultValue: Y): Y {
Expand Down Expand Up @@ -181,7 +181,7 @@ export function GetObjectArrayPropOrThrow<Y, X extends Y[] | null>(props: Record
return GetObjectArrayFunctionPropOrThrow<Y, X>(props, prop, (e) => e as Y)
}

export function GetObjectArrayFunctionPropOrThrow<Y, X extends Y[] | null>(props: Record<string, any> | undefined | null, prop: string, constructorFunc: ConstructorFunc<Y>): X {
export function GetObjectArrayFunctionPropOrThrow<Y, X extends Y[] | null>(props: Record<string, any> | undefined | null, prop: string, constructorFunc: ConstructorFunc<Y>, message? : string): X {
if (props) {
if (prop in props) {
let v = props[prop];
Expand All @@ -190,7 +190,7 @@ export function GetObjectArrayFunctionPropOrThrow<Y, X extends Y[] | null>(props
}
}
}
throw new Error(`${prop} not found as object in ${typeof props}`)
throw new Error(message ?? `${prop} not found as object in ${typeof props}`)
}

export function GetObjectArrayPropOrDefault<Y, X extends Y[] | null>(props: Record<string, any> | undefined | null, prop: string, defaultValue: X): X {
Expand Down

0 comments on commit 2e174d4

Please sign in to comment.