@thi.ng/errors
Advanced tools
Comparing version 2.4.5 to 2.4.6
import { defError } from "./deferror.js"; | ||
export const AssertionError = defError(() => "Assertion failed"); | ||
/** | ||
* Takes a `test` result or predicate function without args and throws error | ||
* with given `msg` if test failed (i.e. is falsy). | ||
* | ||
* @remarks | ||
* The function is only enabled if `process.env.NODE_ENV != "production"` or if | ||
* the `UMBRELLA_ASSERTS` or `VITE_UMBRELLA_ASSERTS` env var is set to 1. | ||
*/ | ||
export const assert = (typeof process !== "undefined" && process.env !== undefined | ||
? process.env.NODE_ENV !== "production" || | ||
!!process.env.UMBRELLA_ASSERTS | ||
: import.meta.env | ||
? import.meta.env.MODE !== "production" || | ||
!!import.meta.env.UMBRELLA_ASSERTS || | ||
!!import.meta.env.VITE_UMBRELLA_ASSERTS | ||
: true) | ||
? (test, msg) => { | ||
if ((typeof test === "function" && !test()) || !test) { | ||
throw new AssertionError(typeof msg === "function" ? msg() : msg); | ||
} | ||
} | ||
: () => { }; | ||
const AssertionError = defError(() => "Assertion failed"); | ||
const assert = (typeof process !== "undefined" && process.env !== void 0 ? process.env.NODE_ENV !== "production" || !!process.env.UMBRELLA_ASSERTS : import.meta.env ? import.meta.env.MODE !== "production" || !!import.meta.env.UMBRELLA_ASSERTS || !!import.meta.env.VITE_UMBRELLA_ASSERTS : true) ? (test, msg) => { | ||
if (typeof test === "function" && !test() || !test) { | ||
throw new AssertionError( | ||
typeof msg === "function" ? msg() : msg | ||
); | ||
} | ||
} : () => { | ||
}; | ||
export { | ||
AssertionError, | ||
assert | ||
}; |
# Change Log | ||
- **Last updated**: 2023-12-09T19:12:03Z | ||
- **Last updated**: 2023-12-11T10:07:09Z | ||
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub) | ||
@@ -5,0 +5,0 @@ |
@@ -1,5 +0,8 @@ | ||
export const defError = (prefix, suffix = (msg) => (msg !== undefined ? ": " + msg : "")) => class extends Error { | ||
constructor(msg) { | ||
super(prefix(msg) + suffix(msg)); | ||
} | ||
const defError = (prefix, suffix = (msg) => msg !== void 0 ? ": " + msg : "") => class extends Error { | ||
constructor(msg) { | ||
super(prefix(msg) + suffix(msg)); | ||
} | ||
}; | ||
export { | ||
defError | ||
}; |
159
ensure.js
import { assert } from "./assert.js"; | ||
/** | ||
* Higher-order function to define ensurance assertions. Takes a `pred`icate | ||
* function and an `expected` (type) name, returns a new function which accepts | ||
* 2 args (an arbitrary value `x` and optional error `msg`). When called, checks | ||
* `x` for non-null and if so applies given `pred`icate. If result is false (or | ||
* `x` is nullish) and iff {@link assert} is enabled, throws a | ||
* {@link AssertionError} with given `msg` (or a constructed default msg). | ||
* Otherwise function is a no-op. | ||
* | ||
* @param pred | ||
* @param expected | ||
*/ | ||
export const defEnsure = (pred, expected) => (x, msg) => { | ||
x != null | ||
? assert(() => pred(x), msg || `expected ${expected}, got ${typeof x}`) | ||
: assert(false, `expected ${expected}, got ${x}`); | ||
return x; | ||
const defEnsure = (pred, expected) => (x, msg) => { | ||
x != null ? assert( | ||
() => pred(x), | ||
msg || `expected ${expected}, got ${typeof x}` | ||
) : assert(false, `expected ${expected}, got ${x}`); | ||
return x; | ||
}; | ||
/** | ||
* Only enabled if {@link assert} is enabled (otherwise no-op). Checks if `x` is | ||
* a JS array and if not throws {@link AssertionError}, optionally with given | ||
* `msg`. | ||
* | ||
* @remarks | ||
* See {@link defEnsure} for details. | ||
* | ||
* @param x | ||
* @param msg | ||
*/ | ||
export const ensureArray = defEnsure((x) => Array.isArray(x), `array`); | ||
/** | ||
* Only enabled if {@link assert} is enabled (otherwise no-op). Checks if `x` is | ||
* a bigint and if not throws {@link AssertionError}, optionally with given | ||
* `msg`. | ||
* | ||
* @remarks | ||
* See {@link defEnsure} for details. | ||
* | ||
* @param x | ||
* @param msg | ||
*/ | ||
export const ensureBigInt = defEnsure((x) => typeof x === "bigint", "bigint"); | ||
/** | ||
* Only enabled if {@link assert} is enabled (otherwise no-op). Checks if `x` is | ||
* a boolean and if not throws {@link AssertionError}, optionally with given | ||
* `msg`. | ||
* | ||
* @remarks | ||
* See {@link defEnsure} for details. | ||
* | ||
* @param x | ||
* @param msg | ||
*/ | ||
export const ensureBoolean = defEnsure((x) => typeof x === "boolean", "boolean"); | ||
/** | ||
* Only enabled if {@link assert} is enabled (otherwise no-op). Checks if `x` is | ||
* a function and if not throws {@link AssertionError}, optionally with given | ||
* `msg`. | ||
* | ||
* @remarks | ||
* See {@link defEnsure} for details. | ||
* | ||
* @param x | ||
* @param msg | ||
*/ | ||
export const ensureFunction = defEnsure((x) => typeof x === "function", "function"); | ||
/** | ||
* Only enabled if {@link assert} is enabled (otherwise no-op). Checks if `x` is | ||
* an ES6 iterable and if not throws {@link AssertionError}, optionally with | ||
* given `msg`. | ||
* | ||
* @remarks | ||
* See {@link defEnsure} for details. | ||
* | ||
* @param x | ||
* @param msg | ||
*/ | ||
export const ensureIterable = defEnsure((x) => typeof x[Symbol.iterator] === "function", "iterable"); | ||
/** | ||
* Only enabled if {@link assert} is enabled (otherwise no-op). Checks if `x` is | ||
* a number and if not throws {@link AssertionError}, optionally with given | ||
* `msg`. | ||
* | ||
* @remarks | ||
* See {@link defEnsure} for details. | ||
* | ||
* @param x | ||
* @param msg | ||
*/ | ||
export const ensureNumber = defEnsure((x) => typeof x === "number", "number"); | ||
/** | ||
* Only enabled if {@link assert} is enabled (otherwise no-op). Checks if `x` is | ||
* a string and if not throws {@link AssertionError}, optionally with given | ||
* `msg`. | ||
* | ||
* @remarks | ||
* See {@link defEnsure} for details. | ||
* | ||
* @param x | ||
* @param msg | ||
*/ | ||
export const ensureString = defEnsure((x) => typeof x === "string", "string"); | ||
/** | ||
* Only enabled if {@link assert} is enabled (otherwise no-op). Checks if `x` is | ||
* a symbol and if not throws {@link AssertionError}, optionally with given | ||
* `msg`. | ||
* | ||
* @remarks | ||
* See {@link defEnsure} for details. | ||
* | ||
* @param x | ||
* @param msg | ||
*/ | ||
export const ensureSymbol = defEnsure((x) => typeof x === "symbol", "symbol"); | ||
const ensureArray = defEnsure((x) => Array.isArray(x), `array`); | ||
const ensureBigInt = defEnsure( | ||
(x) => typeof x === "bigint", | ||
"bigint" | ||
); | ||
const ensureBoolean = defEnsure( | ||
(x) => typeof x === "boolean", | ||
"boolean" | ||
); | ||
const ensureFunction = defEnsure( | ||
(x) => typeof x === "function", | ||
"function" | ||
); | ||
const ensureIterable = defEnsure( | ||
(x) => typeof x[Symbol.iterator] === "function", | ||
"iterable" | ||
); | ||
const ensureNumber = defEnsure( | ||
(x) => typeof x === "number", | ||
"number" | ||
); | ||
const ensureString = defEnsure( | ||
(x) => typeof x === "string", | ||
"string" | ||
); | ||
const ensureSymbol = defEnsure( | ||
(x) => typeof x === "symbol", | ||
"symbol" | ||
); | ||
export { | ||
defEnsure, | ||
ensureArray, | ||
ensureBigInt, | ||
ensureBoolean, | ||
ensureFunction, | ||
ensureIterable, | ||
ensureNumber, | ||
ensureString, | ||
ensureSymbol | ||
}; |
import { defError } from "./deferror.js"; | ||
export const IllegalArgumentError = defError(() => "illegal argument(s)"); | ||
export const illegalArgs = (msg) => { | ||
throw new IllegalArgumentError(msg); | ||
const IllegalArgumentError = defError(() => "illegal argument(s)"); | ||
const illegalArgs = (msg) => { | ||
throw new IllegalArgumentError(msg); | ||
}; | ||
export { | ||
IllegalArgumentError, | ||
illegalArgs | ||
}; |
import { defError } from "./deferror.js"; | ||
export const IllegalArityError = defError(() => "illegal arity"); | ||
export const illegalArity = (n) => { | ||
throw new IllegalArityError(n); | ||
const IllegalArityError = defError(() => "illegal arity"); | ||
const illegalArity = (n) => { | ||
throw new IllegalArityError(n); | ||
}; | ||
export { | ||
IllegalArityError, | ||
illegalArity | ||
}; |
import { defError } from "./deferror.js"; | ||
export const IllegalStateError = defError(() => "illegal state"); | ||
export const illegalState = (msg) => { | ||
throw new IllegalStateError(msg); | ||
const IllegalStateError = defError(() => "illegal state"); | ||
const illegalState = (msg) => { | ||
throw new IllegalStateError(msg); | ||
}; | ||
export { | ||
IllegalStateError, | ||
illegalState | ||
}; |
18
io.js
import { defError } from "./deferror.js"; | ||
export const IOError = defError(() => "IO error"); | ||
export const ioerror = (msg) => { | ||
throw new IOError(msg); | ||
const IOError = defError(() => "IO error"); | ||
const ioerror = (msg) => { | ||
throw new IOError(msg); | ||
}; | ||
export const FileNotFoundError = defError(() => "File not found"); | ||
export const fileNotFound = (path) => { | ||
throw new FileNotFoundError(path); | ||
const FileNotFoundError = defError(() => "File not found"); | ||
const fileNotFound = (path) => { | ||
throw new FileNotFoundError(path); | ||
}; | ||
export { | ||
FileNotFoundError, | ||
IOError, | ||
fileNotFound, | ||
ioerror | ||
}; |
import { defError } from "./deferror.js"; | ||
export const OutOfBoundsError = defError(() => "index out of bounds"); | ||
export const outOfBounds = (index) => { | ||
throw new OutOfBoundsError(index); | ||
const OutOfBoundsError = defError(() => "index out of bounds"); | ||
const outOfBounds = (index) => { | ||
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, min, max) => (index < min || index >= max) && outOfBounds(index); | ||
/** | ||
* Throws an {@link OutOfBoundsError} if either `x` or `y` is outside their | ||
* respective `[0..max)` range. | ||
* | ||
* @param x - | ||
* @param y - | ||
* @param maxX - | ||
* @param maxY - | ||
*/ | ||
export const ensureIndex2 = (x, y, maxX, maxY) => (x < 0 || x >= maxX || y < 0 || y >= maxY) && outOfBounds([x, y]); | ||
const ensureIndex = (index, min, max) => (index < min || index >= max) && outOfBounds(index); | ||
const ensureIndex2 = (x, y, maxX, maxY) => (x < 0 || x >= maxX || y < 0 || y >= maxY) && outOfBounds([x, y]); | ||
export { | ||
OutOfBoundsError, | ||
ensureIndex, | ||
ensureIndex2, | ||
outOfBounds | ||
}; |
{ | ||
"name": "@thi.ng/errors", | ||
"version": "2.4.5", | ||
"version": "2.4.6", | ||
"description": "Custom error types and error factory functions", | ||
@@ -27,3 +27,5 @@ "type": "module", | ||
"scripts": { | ||
"build": "yarn clean && tsc --declaration", | ||
"build": "yarn build:esbuild && yarn build:decl", | ||
"build:decl": "tsc --declaration --emitDeclarationOnly", | ||
"build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts", | ||
"clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc", | ||
@@ -39,2 +41,3 @@ "doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts", | ||
"@types/node": "^20.10.2", | ||
"esbuild": "^0.19.8", | ||
"rimraf": "^5.0.5", | ||
@@ -99,3 +102,3 @@ "tools": "^0.0.1", | ||
}, | ||
"gitHead": "25f2ac8ff795a432a930119661b364d4d93b59a0\n" | ||
"gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n" | ||
} |
@@ -64,3 +64,3 @@ <!-- This file is generated - DO NOT EDIT! --> | ||
Package sizes (brotli'd, pre-treeshake): ESM: 749 bytes | ||
Package sizes (brotli'd, pre-treeshake): ESM: 720 bytes | ||
@@ -67,0 +67,0 @@ ## Dependencies |
import { defError } from "./deferror.js"; | ||
export const UnsupportedOperationError = defError(() => "unsupported operation"); | ||
export const unsupported = (msg) => { | ||
throw new UnsupportedOperationError(msg); | ||
const UnsupportedOperationError = defError( | ||
() => "unsupported operation" | ||
); | ||
const unsupported = (msg) => { | ||
throw new UnsupportedOperationError(msg); | ||
}; | ||
export { | ||
UnsupportedOperationError, | ||
unsupported | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38000
7
324447
405