From 2e174d4334622eba664fdd9bb22bc1258503f821 Mon Sep 17 00:00:00 2001 From: Arran Ubels Date: Fri, 30 Jun 2023 19:37:46 +1000 Subject: [PATCH] Specify an error message. --- README.md | 14 +++++++------- src/index.ts | 20 ++++++++++---------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 696676e..7d9ff45 100644 --- a/README.md +++ b/README.md @@ -39,29 +39,29 @@ By using the `props` component you both have created: ```typescript export function GetDateArrayPropOrDefaultFunction(props: Record | undefined | null, prop: string, defaultFunction: () => R): Date[] | R; export function GetDateArrayPropOrDefault(props: Record | undefined | null, prop: string, defaultValue: R): Date[] | R; -export function GetDateArrayPropOrThrow(props: Record | undefined | null, prop: string): Date[]; +export function GetDateArrayPropOrThrow(props: Record | undefined | null, prop: string, errorMessage?: string): Date[]; export function GetDatePropOrDefaultFunction(props: Record | undefined | null, prop: string, defaultFunction: () => R): R | Date; export function GetDatePropOrDefault(props: Record | undefined | null, prop: string, defaultValue: R): R | Date; -export function GetDatePropOrThrow(props: Record | undefined | null, prop: string): Date; +export function GetDatePropOrThrow(props: Record | undefined | null, prop: string, errorMessage?: string): Date; export function GetNumberPropOrDefaultFunction(props: Record | undefined | null, prop: string, defaultFunction: () => R): R; export function GetNumberPropOrDefault(props: Record | undefined | null, prop: string, defaultValue: R): R; -export function GetNumberPropOrThrow(props: Record | undefined | null, prop: string): R; +export function GetNumberPropOrThrow(props: Record | undefined | null, prop: string, errorMessage?: string): R; export function GetObjectArrayFunctionPropOrDefault(props: Record | undefined | null, prop: string, constructorFunc: ConstructorFunc, defaultValue: X): X; export function GetObjectArrayFunctionPropOrThrow(props: Record | undefined | null, prop: string, constructorFunc: ConstructorFunc): X; export function GetObjectArrayPropOrDefaultFunction(props: Record | undefined | null, prop: string, constructorFunc: ConstructorFunc, defaultValue: () => X): X; export function GetObjectArrayPropOrDefault(props: Record | undefined | null, prop: string, defaultValue: X): X; -export function GetObjectArrayPropOrThrow(props: Record | undefined | null, prop: string): X; +export function GetObjectArrayPropOrThrow(props: Record | undefined | null, prop: string, errorMessage?: string): X; export function GetObjectFunctionPropOrDefault(props: Record | undefined | null, prop: string, constructorFunc: ConstructorFunc, defaultValue: Y): Y; export function GetObjectFunctionPropOrThrow(props: Record | undefined | null, prop: string, constructorFunc: ConstructorFunc): Y; export function GetObjectPropOrDefaultFunction(props: Record | undefined | null, prop: string, constructorFunc: ConstructorFunc, defaultValue: () => Y): Y; export function GetObjectPropOrDefault(props: Record | undefined | null, prop: string, defaultValue: Y): Y; -export function GetObjectPropOrThrow(props: Record | undefined | null, prop: string): Y; +export function GetObjectPropOrThrow(props: Record | undefined | null, prop: string, errorMessage?: string): Y; export function GetStringArrayPropOrDefaultFunction(props: Record | undefined | null, prop: string, defaultFunction: () => R): string[] | R; export function GetStringArrayPropOrDefault(props: Record | undefined | null, prop: string, defaultValue: R): string[] | R; -export function GetStringArrayPropOrThrow(props: Record | undefined | null, prop: string): string[]; +export function GetStringArrayPropOrThrow(props: Record | undefined | null, prop: string, errorMessage?: string): string[]; export function GetStringPropOrDefaultFunction(props: Record | undefined | null, prop: string, defaultFunction: () => R): R; export function GetStringPropOrDefault(props: Record | undefined | null, prop: string, defaultValue: R): R; -export function GetStringPropOrThrow(props: Record | undefined | null, prop: string): R; +export function GetStringPropOrThrow(props: Record | undefined | null, prop: string, errorMessage?: string): R; export type ConstructorFunc = (params: Partial>) => Y; ``` diff --git a/src/index.ts b/src/index.ts index a2f219e..32f32b4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,7 +10,7 @@ export function GetStringPropOrDefault(props: Record(props, prop, () => defaultValue); } -export function GetStringPropOrThrow(props: Record | undefined | null, prop: string): R { +export function GetStringPropOrThrow(props: Record | undefined | null, prop: string, message? : string): R { if (props) { if (prop in props) { let v = props[prop]; @@ -19,7 +19,7 @@ export function GetStringPropOrThrow(props: Record(props: Record | undefined | null, prop: string, defaultFunction: () => R): R { @@ -34,7 +34,7 @@ export function GetNumberPropOrDefault(props: Record(props, prop, () => defaultValue); } -export function GetNumberPropOrThrow(props: Record | undefined | null, prop: string): R { +export function GetNumberPropOrThrow(props: Record | undefined | null, prop: string, message? : string): R { if (props) { if (prop in props) { let v = props[prop]; @@ -46,7 +46,7 @@ export function GetNumberPropOrThrow(props: Record(props: Record | undefined | null, prop: string, defaultFunction: () => R): R | Date { @@ -89,7 +89,7 @@ export function GetStringArrayPropOrDefault(props: Record | unde return GetStringArrayPropOrDefaultFunction(props, prop, () => defaultValue); } -export function GetStringArrayPropOrThrow(props: Record | undefined | null, prop: string): string[] { +export function GetStringArrayPropOrThrow(props: Record | undefined | null, prop: string, message? : string): string[] { if (props) { if (prop in props) { let v = props[prop]; @@ -98,7 +98,7 @@ export function GetStringArrayPropOrThrow(props: Record | 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(props: Record | undefined | null, prop: string, defaultFunction: () => R): Date[] | R { @@ -141,7 +141,7 @@ export function GetObjectPropOrThrow(props: Record | undefined | return GetObjectFunctionPropOrThrow(props, prop, (e) => e as Y) } -export function GetObjectFunctionPropOrThrow(props: Record | undefined | null, prop: string, constructorFunc: ConstructorFunc): Y { +export function GetObjectFunctionPropOrThrow(props: Record | undefined | null, prop: string, constructorFunc: ConstructorFunc, message? : string): Y { if (props) { if (prop in props) { let v = props[prop]; @@ -150,7 +150,7 @@ export function GetObjectFunctionPropOrThrow(props: Record | 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(props: Record | undefined | null, prop: string, defaultValue: Y): Y { @@ -181,7 +181,7 @@ export function GetObjectArrayPropOrThrow(props: Record return GetObjectArrayFunctionPropOrThrow(props, prop, (e) => e as Y) } -export function GetObjectArrayFunctionPropOrThrow(props: Record | undefined | null, prop: string, constructorFunc: ConstructorFunc): X { +export function GetObjectArrayFunctionPropOrThrow(props: Record | undefined | null, prop: string, constructorFunc: ConstructorFunc, message? : string): X { if (props) { if (prop in props) { let v = props[prop]; @@ -190,7 +190,7 @@ export function GetObjectArrayFunctionPropOrThrow(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(props: Record | undefined | null, prop: string, defaultValue: X): X {