Skip to content

Commit

Permalink
feat: Add methods to simplify allowing other method arguments to be s…
Browse files Browse the repository at this point in the history
…upplied as class instances or raw strings.
  • Loading branch information
adam-coster committed Aug 24, 2021
1 parent 3d08c52 commit a14ee8e
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/lib/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,50 @@ export function createIsMatchFilter<R extends Record<string, any>>(
return isMatch(stringOrRecord, pattern);
};
}

type ObjectWithStringField<
StringField extends string,
StringFieldValue extends string,
> = Record<StringField, StringFieldValue> & Record<string, any>;
/**
* If the first parameter is a string, return it. If it
* is an object, return the value of the `fieldName` property
* (expected to be a string).
*
* This is useful for functions that accept either a string or
* an object that has a field containing that same string.
*/
export function stringOrObjectToString<S extends string>(
string: S,
fieldIfObject: string,
): S;
export function stringOrObjectToString<K extends string, V extends string>(
object: ObjectWithStringField<K, V>,
fieldIfObject: K,
): V;
export function stringOrObjectToString<K extends string, V extends string>(
stringOrObject: V | ObjectWithStringField<K, V>,
fieldIfObject: K,
): V {
if (typeof stringOrObject == 'string') {
return stringOrObject;
} else {
const value = stringOrObject[fieldIfObject];
assertBravoClaim(
typeof value == 'string',
`Field ${fieldIfObject} not found in object`,
);
return stringOrObject[fieldIfObject];
}
}

export function stringsOrObjectsToStrings<K extends string, V extends string>(
stringsOrObjects: (V | ObjectWithStringField<K, V>)[],
fieldIfObject: K,
): V[] {
// @ts-expect-error
return stringsOrObjects.map((stringOrObject) =>
// @ts-expect-error
stringOrObjectToString(stringOrObject, fieldIfObject),
);
}

0 comments on commit a14ee8e

Please sign in to comment.