Skip to content

Commit

Permalink
feat(errors): add outOfBounds(), ensureIndex()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Mar 13, 2021
1 parent 24c8afb commit fb5ca0a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/errors/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export * from "./deferror";
export * from "./illegal-arguments";
export * from "./illegal-arity";
export * from "./illegal-state";
export * from "./out-of-bounds";
export * from "./unsupported";
17 changes: 17 additions & 0 deletions packages/errors/src/out-of-bounds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { defError } from "./deferror";

export const OutOfBoundsError = defError<any>(() => "index out of bounds");

export const outOfBounds = (index: number): never => {
throw new OutOfBoundsError(index);
};

/**
* Throws an {@link OutOfBoundsError} if `index` outside the `[min..max)` range.
*
* @param index
* @param min
* @param max
*/
export const ensureIndex = (index: number, min: number, max: number) =>
(index < min || index >= max) && outOfBounds(index);

0 comments on commit fb5ca0a

Please sign in to comment.