@thi.ng/errors 2.4.5 → 2.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2023-12-09T19:12:03Z
3
+ - **Last updated**: 2023-12-11T10:07:09Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
package/README.md CHANGED
@@ -61,7 +61,7 @@ For Node.js REPL:
61
61
  const errors = await import("@thi.ng/errors");
62
62
  ```
63
63
 
64
- Package sizes (brotli'd, pre-treeshake): ESM: 749 bytes
64
+ Package sizes (brotli'd, pre-treeshake): ESM: 720 bytes
65
65
 
66
66
  ## Dependencies
67
67
 
package/assert.js CHANGED
@@ -1,24 +1,14 @@
1
1
  import { defError } from "./deferror.js";
2
- export const AssertionError = defError(() => "Assertion failed");
3
- /**
4
- * Takes a `test` result or predicate function without args and throws error
5
- * with given `msg` if test failed (i.e. is falsy).
6
- *
7
- * @remarks
8
- * The function is only enabled if `process.env.NODE_ENV != "production"` or if
9
- * the `UMBRELLA_ASSERTS` or `VITE_UMBRELLA_ASSERTS` env var is set to 1.
10
- */
11
- export const assert = (typeof process !== "undefined" && process.env !== undefined
12
- ? process.env.NODE_ENV !== "production" ||
13
- !!process.env.UMBRELLA_ASSERTS
14
- : import.meta.env
15
- ? import.meta.env.MODE !== "production" ||
16
- !!import.meta.env.UMBRELLA_ASSERTS ||
17
- !!import.meta.env.VITE_UMBRELLA_ASSERTS
18
- : true)
19
- ? (test, msg) => {
20
- if ((typeof test === "function" && !test()) || !test) {
21
- throw new AssertionError(typeof msg === "function" ? msg() : msg);
22
- }
23
- }
24
- : () => { };
2
+ const AssertionError = defError(() => "Assertion failed");
3
+ 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) => {
4
+ if (typeof test === "function" && !test() || !test) {
5
+ throw new AssertionError(
6
+ typeof msg === "function" ? msg() : msg
7
+ );
8
+ }
9
+ } : () => {
10
+ };
11
+ export {
12
+ AssertionError,
13
+ assert
14
+ };
package/deferror.js CHANGED
@@ -1,5 +1,8 @@
1
- export const defError = (prefix, suffix = (msg) => (msg !== undefined ? ": " + msg : "")) => class extends Error {
2
- constructor(msg) {
3
- super(prefix(msg) + suffix(msg));
4
- }
1
+ const defError = (prefix, suffix = (msg) => msg !== void 0 ? ": " + msg : "") => class extends Error {
2
+ constructor(msg) {
3
+ super(prefix(msg) + suffix(msg));
4
+ }
5
+ };
6
+ export {
7
+ defError
5
8
  };
package/ensure.js CHANGED
@@ -1,115 +1,48 @@
1
1
  import { assert } from "./assert.js";
2
- /**
3
- * Higher-order function to define ensurance assertions. Takes a `pred`icate
4
- * function and an `expected` (type) name, returns a new function which accepts
5
- * 2 args (an arbitrary value `x` and optional error `msg`). When called, checks
6
- * `x` for non-null and if so applies given `pred`icate. If result is false (or
7
- * `x` is nullish) and iff {@link assert} is enabled, throws a
8
- * {@link AssertionError} with given `msg` (or a constructed default msg).
9
- * Otherwise function is a no-op.
10
- *
11
- * @param pred
12
- * @param expected
13
- */
14
- export const defEnsure = (pred, expected) => (x, msg) => {
15
- x != null
16
- ? assert(() => pred(x), msg || `expected ${expected}, got ${typeof x}`)
17
- : assert(false, `expected ${expected}, got ${x}`);
18
- return x;
2
+ const defEnsure = (pred, expected) => (x, msg) => {
3
+ x != null ? assert(
4
+ () => pred(x),
5
+ msg || `expected ${expected}, got ${typeof x}`
6
+ ) : assert(false, `expected ${expected}, got ${x}`);
7
+ return x;
8
+ };
9
+ const ensureArray = defEnsure((x) => Array.isArray(x), `array`);
10
+ const ensureBigInt = defEnsure(
11
+ (x) => typeof x === "bigint",
12
+ "bigint"
13
+ );
14
+ const ensureBoolean = defEnsure(
15
+ (x) => typeof x === "boolean",
16
+ "boolean"
17
+ );
18
+ const ensureFunction = defEnsure(
19
+ (x) => typeof x === "function",
20
+ "function"
21
+ );
22
+ const ensureIterable = defEnsure(
23
+ (x) => typeof x[Symbol.iterator] === "function",
24
+ "iterable"
25
+ );
26
+ const ensureNumber = defEnsure(
27
+ (x) => typeof x === "number",
28
+ "number"
29
+ );
30
+ const ensureString = defEnsure(
31
+ (x) => typeof x === "string",
32
+ "string"
33
+ );
34
+ const ensureSymbol = defEnsure(
35
+ (x) => typeof x === "symbol",
36
+ "symbol"
37
+ );
38
+ export {
39
+ defEnsure,
40
+ ensureArray,
41
+ ensureBigInt,
42
+ ensureBoolean,
43
+ ensureFunction,
44
+ ensureIterable,
45
+ ensureNumber,
46
+ ensureString,
47
+ ensureSymbol
19
48
  };
20
- /**
21
- * Only enabled if {@link assert} is enabled (otherwise no-op). Checks if `x` is
22
- * a JS array and if not throws {@link AssertionError}, optionally with given
23
- * `msg`.
24
- *
25
- * @remarks
26
- * See {@link defEnsure} for details.
27
- *
28
- * @param x
29
- * @param msg
30
- */
31
- export const ensureArray = defEnsure((x) => Array.isArray(x), `array`);
32
- /**
33
- * Only enabled if {@link assert} is enabled (otherwise no-op). Checks if `x` is
34
- * a bigint and if not throws {@link AssertionError}, optionally with given
35
- * `msg`.
36
- *
37
- * @remarks
38
- * See {@link defEnsure} for details.
39
- *
40
- * @param x
41
- * @param msg
42
- */
43
- export const ensureBigInt = defEnsure((x) => typeof x === "bigint", "bigint");
44
- /**
45
- * Only enabled if {@link assert} is enabled (otherwise no-op). Checks if `x` is
46
- * a boolean and if not throws {@link AssertionError}, optionally with given
47
- * `msg`.
48
- *
49
- * @remarks
50
- * See {@link defEnsure} for details.
51
- *
52
- * @param x
53
- * @param msg
54
- */
55
- export const ensureBoolean = defEnsure((x) => typeof x === "boolean", "boolean");
56
- /**
57
- * Only enabled if {@link assert} is enabled (otherwise no-op). Checks if `x` is
58
- * a function and if not throws {@link AssertionError}, optionally with given
59
- * `msg`.
60
- *
61
- * @remarks
62
- * See {@link defEnsure} for details.
63
- *
64
- * @param x
65
- * @param msg
66
- */
67
- export const ensureFunction = defEnsure((x) => typeof x === "function", "function");
68
- /**
69
- * Only enabled if {@link assert} is enabled (otherwise no-op). Checks if `x` is
70
- * an ES6 iterable and if not throws {@link AssertionError}, optionally with
71
- * given `msg`.
72
- *
73
- * @remarks
74
- * See {@link defEnsure} for details.
75
- *
76
- * @param x
77
- * @param msg
78
- */
79
- export const ensureIterable = defEnsure((x) => typeof x[Symbol.iterator] === "function", "iterable");
80
- /**
81
- * Only enabled if {@link assert} is enabled (otherwise no-op). Checks if `x` is
82
- * a number and if not throws {@link AssertionError}, optionally with given
83
- * `msg`.
84
- *
85
- * @remarks
86
- * See {@link defEnsure} for details.
87
- *
88
- * @param x
89
- * @param msg
90
- */
91
- export const ensureNumber = defEnsure((x) => typeof x === "number", "number");
92
- /**
93
- * Only enabled if {@link assert} is enabled (otherwise no-op). Checks if `x` is
94
- * a string and if not throws {@link AssertionError}, optionally with given
95
- * `msg`.
96
- *
97
- * @remarks
98
- * See {@link defEnsure} for details.
99
- *
100
- * @param x
101
- * @param msg
102
- */
103
- export const ensureString = defEnsure((x) => typeof x === "string", "string");
104
- /**
105
- * Only enabled if {@link assert} is enabled (otherwise no-op). Checks if `x` is
106
- * a symbol and if not throws {@link AssertionError}, optionally with given
107
- * `msg`.
108
- *
109
- * @remarks
110
- * See {@link defEnsure} for details.
111
- *
112
- * @param x
113
- * @param msg
114
- */
115
- export const ensureSymbol = defEnsure((x) => typeof x === "symbol", "symbol");
@@ -1,5 +1,9 @@
1
1
  import { defError } from "./deferror.js";
2
- export const IllegalArgumentError = defError(() => "illegal argument(s)");
3
- export const illegalArgs = (msg) => {
4
- throw new IllegalArgumentError(msg);
2
+ const IllegalArgumentError = defError(() => "illegal argument(s)");
3
+ const illegalArgs = (msg) => {
4
+ throw new IllegalArgumentError(msg);
5
+ };
6
+ export {
7
+ IllegalArgumentError,
8
+ illegalArgs
5
9
  };
package/illegal-arity.js CHANGED
@@ -1,5 +1,9 @@
1
1
  import { defError } from "./deferror.js";
2
- export const IllegalArityError = defError(() => "illegal arity");
3
- export const illegalArity = (n) => {
4
- throw new IllegalArityError(n);
2
+ const IllegalArityError = defError(() => "illegal arity");
3
+ const illegalArity = (n) => {
4
+ throw new IllegalArityError(n);
5
+ };
6
+ export {
7
+ IllegalArityError,
8
+ illegalArity
5
9
  };
package/illegal-state.js CHANGED
@@ -1,5 +1,9 @@
1
1
  import { defError } from "./deferror.js";
2
- export const IllegalStateError = defError(() => "illegal state");
3
- export const illegalState = (msg) => {
4
- throw new IllegalStateError(msg);
2
+ const IllegalStateError = defError(() => "illegal state");
3
+ const illegalState = (msg) => {
4
+ throw new IllegalStateError(msg);
5
+ };
6
+ export {
7
+ IllegalStateError,
8
+ illegalState
5
9
  };
package/io.js CHANGED
@@ -1,9 +1,15 @@
1
1
  import { defError } from "./deferror.js";
2
- export const IOError = defError(() => "IO error");
3
- export const ioerror = (msg) => {
4
- throw new IOError(msg);
2
+ const IOError = defError(() => "IO error");
3
+ const ioerror = (msg) => {
4
+ throw new IOError(msg);
5
5
  };
6
- export const FileNotFoundError = defError(() => "File not found");
7
- export const fileNotFound = (path) => {
8
- throw new FileNotFoundError(path);
6
+ const FileNotFoundError = defError(() => "File not found");
7
+ const fileNotFound = (path) => {
8
+ throw new FileNotFoundError(path);
9
+ };
10
+ export {
11
+ FileNotFoundError,
12
+ IOError,
13
+ fileNotFound,
14
+ ioerror
9
15
  };
package/out-of-bounds.js CHANGED
@@ -1,23 +1,13 @@
1
1
  import { defError } from "./deferror.js";
2
- export const OutOfBoundsError = defError(() => "index out of bounds");
3
- export const outOfBounds = (index) => {
4
- throw new OutOfBoundsError(index);
2
+ const OutOfBoundsError = defError(() => "index out of bounds");
3
+ const outOfBounds = (index) => {
4
+ throw new OutOfBoundsError(index);
5
+ };
6
+ const ensureIndex = (index, min, max) => (index < min || index >= max) && outOfBounds(index);
7
+ const ensureIndex2 = (x, y, maxX, maxY) => (x < 0 || x >= maxX || y < 0 || y >= maxY) && outOfBounds([x, y]);
8
+ export {
9
+ OutOfBoundsError,
10
+ ensureIndex,
11
+ ensureIndex2,
12
+ outOfBounds
5
13
  };
6
- /**
7
- * Throws an {@link OutOfBoundsError} if `index` outside the `[min..max)` range.
8
- *
9
- * @param index -
10
- * @param min -
11
- * @param max -
12
- */
13
- export const ensureIndex = (index, min, max) => (index < min || index >= max) && outOfBounds(index);
14
- /**
15
- * Throws an {@link OutOfBoundsError} if either `x` or `y` is outside their
16
- * respective `[0..max)` range.
17
- *
18
- * @param x -
19
- * @param y -
20
- * @param maxX -
21
- * @param maxY -
22
- */
23
- export const ensureIndex2 = (x, y, maxX, maxY) => (x < 0 || x >= maxX || y < 0 || y >= maxY) && outOfBounds([x, y]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/errors",
3
- "version": "2.4.5",
3
+ "version": "2.4.6",
4
4
  "description": "Custom error types and error factory functions",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -24,7 +24,9 @@
24
24
  "author": "Karsten Schmidt (https://thi.ng)",
25
25
  "license": "Apache-2.0",
26
26
  "scripts": {
27
- "build": "yarn clean && tsc --declaration",
27
+ "build": "yarn build:esbuild && yarn build:decl",
28
+ "build:decl": "tsc --declaration --emitDeclarationOnly",
29
+ "build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
28
30
  "clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc",
29
31
  "doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
30
32
  "doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
@@ -35,6 +37,7 @@
35
37
  "devDependencies": {
36
38
  "@microsoft/api-extractor": "^7.38.3",
37
39
  "@types/node": "^20.10.2",
40
+ "esbuild": "^0.19.8",
38
41
  "rimraf": "^5.0.5",
39
42
  "tools": "^0.0.1",
40
43
  "typedoc": "^0.25.4",
@@ -94,5 +97,5 @@
94
97
  "thi.ng": {
95
98
  "year": 2018
96
99
  },
97
- "gitHead": "25f2ac8ff795a432a930119661b364d4d93b59a0\n"
100
+ "gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n"
98
101
  }
package/unsupported.js CHANGED
@@ -1,5 +1,11 @@
1
1
  import { defError } from "./deferror.js";
2
- export const UnsupportedOperationError = defError(() => "unsupported operation");
3
- export const unsupported = (msg) => {
4
- throw new UnsupportedOperationError(msg);
2
+ const UnsupportedOperationError = defError(
3
+ () => "unsupported operation"
4
+ );
5
+ const unsupported = (msg) => {
6
+ throw new UnsupportedOperationError(msg);
7
+ };
8
+ export {
9
+ UnsupportedOperationError,
10
+ unsupported
5
11
  };