Last active
September 15, 2021 08:18
-
-
Save ericzakariasson/4ce6c4a48050f8a8589502c6db0043e7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class InternalError extends Error {} | |
type Predicate<T> = (item: T) => boolean; | |
function single<T>(array: T[], predicate?: Predicate<T>) { | |
const items = predicate ? array.filter(predicate) : array; | |
if (items.length !== 1) { | |
throw new InternalError("Array does not contain exactly one item"); | |
} | |
return items[0]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment