@monstermann/fn
Version:
A utility library for TypeScript.
25 lines (23 loc) • 645 B
JavaScript
import { FnError } from "../function/FnError.js";
import { dfdlT } from "@monstermann/dfdl";
//#region src/array/atOrThrow.ts
/**
* `atOrThrow(array, offset)`
*
* Returns the value at the specified `offset`, throws an exception if the `offset` was out of range, or the retrieved value was nullable.
*
* ```ts
* atOrThrow([1, null], -1); // Error
* ```
*
* ```ts
* pipe([1, null], atOrThrow(-1)); // Error
* ```
*/
const atOrThrow = dfdlT((target, offset) => {
const value = target.at(offset);
if (value != null) return value;
throw new FnError("Array.atOrThrow: No value found.", [target, offset]);
}, 2);
//#endregion
export { atOrThrow };