Skip to content

Commit

Permalink
feat: Exposes isScormInteractionType guard
Browse files Browse the repository at this point in the history
  • Loading branch information
ryasmi committed Jun 20, 2020
1 parent 8772ab4 commit 28bfc0b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/constrainedStrings/scormInteractionType/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[Back to root readme.md](../../../readme.md)

This function can be used to check the type of the input is a valid SCORM interaction type as shown in the example below. It should only throw `rulr.InvalidScormInteractionTypeError`. This is a little convenience rule for everyone trying to improve the world's training using [SCORM](https://scorm.com/) or the [xAPI](https://github.com/adlnet/xAPI-Spec), this package might not exist without these people.
This function uses `rulr.isScormInteractionType` to check the input is a valid SCORM interaction type as shown in the example below. It should only throw `rulr.InvalidScormInteractionTypeError`. This is a little convenience rule for everyone trying to improve the world's training using [SCORM](https://scorm.com/) or the [xAPI](https://github.com/adlnet/xAPI-Spec), this package might not exist without these people.

```ts
import * as rulr from 'rulr'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseError } from 'make-error'
import { enumerated } from '../../valueRules/enum/enum'
import { isEnum } from '../../valueRules/enum/enum'

export class InvalidScormInteractionTypeError extends BaseError {
constructor() {
Expand All @@ -22,10 +22,13 @@ export enum ScormInteractionType {
Other = 'other',
}

export function isScormInteractionType(input: unknown): input is ScormInteractionType {
return isEnum(ScormInteractionType, input)
}

export function scormInteractionType(input: unknown) {
try {
return enumerated(ScormInteractionType)(input)
} catch (err) {
throw new InvalidScormInteractionTypeError()
if (isScormInteractionType(input)) {
return input
}
throw new InvalidScormInteractionTypeError()
}
1 change: 1 addition & 0 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export {
} from './constrainedStrings/mongoId/mongoId'
export {
scormInteractionType,
isScormInteractionType,
ScormInteractionType,
InvalidScormInteractionTypeError,
} from './constrainedStrings/scormInteractionType/scormInteractionType'
Expand Down

0 comments on commit 28bfc0b

Please sign in to comment.