@augment-vir/assert 30.8.3 → 31.0.0

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/README.md CHANGED
@@ -2,10 +2,10 @@
2
2
 
3
3
  A collection of assertions for test and production code alike. These main exports are the following:
4
4
 
5
- - [`assert`](https://electrovir.github.io/augment-vir/functions/assert.html): a collection of assertion methods with type guards when possible. Example: [`assert.isDefined()`](https://electrovir.github.io/augment-vir/functions/assert.html#isDefined)
6
- - [`check`](https://electrovir.github.io/augment-vir/functions/check.html): a collection of boolean check methods with type guards when possible. Example: [`check.isBoolean()`](https://electrovir.github.io/augment-vir/functions/check.html#isBoolean)
7
- - [`assertWrap`](https://electrovir.github.io/augment-vir/functions/assertWrap.html): a collection of assertions that return the asserted value if the assertion passes. Examples [`assertWrap.isArray()`](https://electrovir.github.io/augment-vir/functions/assertWrap.html#isArray)
8
- - [`checkWrap`](https://electrovir.github.io/augment-vir/functions/checkWrap.html): a collection of checks that return the checked value if it passes or `undefined`. Example: [`checkWrap.isInfinite()`](https://electrovir.github.io/augment-vir/functions/checkWrap.html#isInfinite)
9
- - [`waitUntil`](https://electrovir.github.io/augment-vir/functions/waitUntil.html): a collection of assertion methods that try to wait until the assertion becomes true. Example: [`waitUntil.isTruthy()`](https://electrovir.github.io/augment-vir/functions/waitUntil.html#isTruthy)
5
+ - [`assert`](https://electrovir.github.io/augment-vir/functions/assert.html): a collection of assertion methods with type guards when possible. Example: [`assert.isDefined()`](https://electrovir.github.io/augment-vir/functions/assert.html#isDefined)
6
+ - [`check`](https://electrovir.github.io/augment-vir/functions/check.html): a collection of boolean check methods with type guards when possible. Example: [`check.isBoolean()`](https://electrovir.github.io/augment-vir/functions/check.html#isBoolean)
7
+ - [`assertWrap`](https://electrovir.github.io/augment-vir/functions/assertWrap.html): a collection of assertions that return the asserted value if the assertion passes. Examples [`assertWrap.isArray()`](https://electrovir.github.io/augment-vir/functions/assertWrap.html#isArray)
8
+ - [`checkWrap`](https://electrovir.github.io/augment-vir/functions/checkWrap.html): a collection of checks that return the checked value if it passes or `undefined`. Example: [`checkWrap.isInfinite()`](https://electrovir.github.io/augment-vir/functions/checkWrap.html#isInfinite)
9
+ - [`waitUntil`](https://electrovir.github.io/augment-vir/functions/waitUntil.html): a collection of assertion methods that try to wait until the assertion becomes true. Example: [`waitUntil.isTruthy()`](https://electrovir.github.io/augment-vir/functions/waitUntil.html#isTruthy)
10
10
 
11
11
  See the docs under `Assert`, or `Package: @augment-vir/assert` here: https://electrovir.github.io/augment-vir
@@ -30,7 +30,6 @@ const hasKeyAttempts = [
30
30
  ];
31
31
  /** Check if an object has the given property. */
32
32
  function hasKey(parent, key, failureMessage) {
33
- const message = `'${stringify(parent)}' does not have key '${String(key)}'.`;
34
33
  const doesHaveKey = hasKeyAttempts.some((attemptCallback) => {
35
34
  try {
36
35
  return attemptCallback(parent, key);
@@ -40,7 +39,7 @@ function hasKey(parent, key, failureMessage) {
40
39
  }
41
40
  });
42
41
  if (!doesHaveKey) {
43
- throw new AssertionError(message, failureMessage);
42
+ throw new AssertionError(`'${stringify(parent)}' does not have key '${String(key)}'.`, failureMessage);
44
43
  }
45
44
  }
46
45
  function lacksKey(parent, key, failureMessage) {
@@ -1996,61 +1996,4 @@ export declare const runtimeTypeGuards: {
1996
1996
  isNotUndefined: <const Actual>(callback: () => MaybePromise<Actual>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Exclude<Actual, undefined>>;
1997
1997
  };
1998
1998
  };
1999
- /**
2000
- * An enum representing the possible values returned by {@link getRuntimeType}. These values are
2001
- * similar to the output of the built-in
2002
- * [`typeof`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/typeof) operator
2003
- * except that this includes new types `array` and `null`, that `typeof` does not have, which are
2004
- * both distinct from `object`.
2005
- *
2006
- * @category Assert : Util
2007
- * @category Package : @augment-vir/assert
2008
- * @package [`@augment-vir/assert`](https://www.npmjs.com/package/@augment-vir/assert)
2009
- */
2010
- export declare enum RuntimeType {
2011
- String = "string",
2012
- Number = "number",
2013
- Bigint = "bigint",
2014
- Boolean = "boolean",
2015
- Symbol = "symbol",
2016
- Undefined = "undefined",
2017
- Object = "object",
2018
- Function = "function",
2019
- /**
2020
- * This is not included in {@link RuntimeType.Object}. (Compared to
2021
- * [`typeof`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/typeof)
2022
- * which _does_ include `null` in the `'object'` type.)
2023
- */
2024
- Array = "array",
2025
- /**
2026
- * This is not included in {@link RuntimeType.Object}. (Compared to
2027
- * [`typeof`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/typeof)
2028
- * which _does_ include `null` in the `'object'` type.)
2029
- */
2030
- Null = "null"
2031
- }
2032
- /**
2033
- * Determines the {@link RuntimeType} of a variable. This is similar to the built-in
2034
- * [`typeof`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/typeof) operator
2035
- * except in the following ways:
2036
- *
2037
- * - This returns an enum value ({@link RuntimeType}) rather than just a string (though the enum values
2038
- * are strings anyway).
2039
- * - This includes new types `array` and `null`, that `typeof` does not have, which are both distinct
2040
- * from `object`.
2041
- *
2042
- * @category Assert : Util
2043
- * @category Package : @augment-vir/assert
2044
- * @example
2045
- *
2046
- * ```ts
2047
- * import {getRuntimeType} from '@augment-vir/assert';
2048
- *
2049
- * getRuntimeType(['a']); // RuntimeType.Array
2050
- * getRuntimeType({a: 'a'}); // RuntimeType.Object
2051
- * ```
2052
- *
2053
- * @package [`@augment-vir/assert`](https://www.npmjs.com/package/@augment-vir/assert)
2054
- */
2055
- export declare function getRuntimeType(actual: unknown): RuntimeType;
2056
1999
  export {};
@@ -2,67 +2,104 @@ import { stringify, } from '@augment-vir/core';
2
2
  import { AssertionError } from '../augments/assertion.error.js';
3
3
  import { autoGuard, autoGuardSymbol } from '../guard-types/guard-override.js';
4
4
  function isNotArray(actual, failureMessage) {
5
- assertNotRuntimeType(actual, 'array', failureMessage);
5
+ if (Array.isArray(actual)) {
6
+ throw new AssertionError(`'${stringify(actual)}' is an array.`, failureMessage);
7
+ }
6
8
  }
7
9
  function isNotBigInt(actual, failureMessage) {
8
- assertNotRuntimeType(actual, 'bigint', failureMessage);
10
+ if (typeof actual === 'bigint') {
11
+ throw new AssertionError(`'${stringify(actual)}' is a bigint.`, failureMessage);
12
+ }
9
13
  }
10
14
  function isNotBoolean(actual, failureMessage) {
11
- assertNotRuntimeType(actual, 'boolean', failureMessage);
15
+ if (typeof actual === 'boolean') {
16
+ throw new AssertionError(`'${stringify(actual)}' is a boolean.`, failureMessage);
17
+ }
12
18
  }
13
19
  function isNotFunction(actual, failureMessage) {
14
- assertNotRuntimeType(actual, 'function', failureMessage);
20
+ if (typeof actual === 'function') {
21
+ throw new AssertionError(`'${stringify(actual)}' is a function.`, failureMessage);
22
+ }
15
23
  }
16
24
  function isNotNumber(actual, failureMessage) {
17
- assertNotRuntimeType(actual, 'number', failureMessage);
25
+ if (typeof actual === 'number') {
26
+ throw new AssertionError(`'${stringify(actual)}' is a number.`, failureMessage);
27
+ }
18
28
  }
19
29
  function isNotObject(actual, failureMessage) {
20
- assertNotRuntimeType(actual, 'object', failureMessage);
30
+ if (!Array.isArray(actual) && typeof actual === 'object' && !!actual) {
31
+ throw new AssertionError(`'${stringify(actual)}' is a non-null object.`, failureMessage);
32
+ }
21
33
  }
22
34
  function isNotString(actual, failureMessage) {
23
- assertNotRuntimeType(actual, 'string', failureMessage);
35
+ if (typeof actual === 'string') {
36
+ throw new AssertionError(`'${stringify(actual)}' is a string.`, failureMessage);
37
+ }
24
38
  }
25
39
  function isNotSymbol(actual, failureMessage) {
26
- assertNotRuntimeType(actual, 'symbol', failureMessage);
40
+ if (typeof actual === 'symbol') {
41
+ throw new AssertionError(`'${stringify(actual)}' is a symbol.`, failureMessage);
42
+ }
27
43
  }
28
44
  function isNotUndefined(actual, failureMessage) {
29
- assertNotRuntimeType(actual, 'undefined', failureMessage);
45
+ if (typeof actual === 'undefined') {
46
+ throw new AssertionError(`'${stringify(actual)}' is a undefined.`, failureMessage);
47
+ }
30
48
  }
31
49
  function isNotNull(actual, failureMessage) {
32
- assertNotRuntimeType(actual, 'null', failureMessage);
50
+ if (actual === null) {
51
+ throw new AssertionError(`'${stringify(actual)}' is a null.`, failureMessage);
52
+ }
33
53
  }
34
54
  function isArray(actual, failureMessage) {
35
- assertRuntimeType(actual, 'array', failureMessage);
55
+ if (!Array.isArray(actual)) {
56
+ throw new AssertionError(`'${stringify(actual)}' is not an array.`, failureMessage);
57
+ }
36
58
  }
37
59
  function isBigInt(actual, failureMessage) {
38
- assertRuntimeType(actual, 'bigint', failureMessage);
60
+ if (typeof actual !== 'bigint') {
61
+ throw new AssertionError(`'${stringify(actual)}' is not a bigint.`, failureMessage);
62
+ }
39
63
  }
40
64
  function isBoolean(actual, failureMessage) {
41
- assertRuntimeType(actual, 'boolean', failureMessage);
65
+ if (typeof actual !== 'boolean') {
66
+ throw new AssertionError(`'${stringify(actual)}' is not a boolean.`, failureMessage);
67
+ }
42
68
  }
43
69
  function isFunction(actual, failureMessage) {
44
- assertRuntimeType(actual, 'function', failureMessage);
70
+ if (typeof actual !== 'function') {
71
+ throw new AssertionError(`'${stringify(actual)}' is not a function.`, failureMessage);
72
+ }
45
73
  }
46
74
  export function isNumber(actual, failureMessage) {
47
- assertRuntimeType(actual, 'number', failureMessage);
48
- if (isNaN(actual)) {
49
- throw new AssertionError('Value is NaN.', failureMessage);
75
+ if (typeof actual !== 'number' || isNaN(actual)) {
76
+ throw new AssertionError(`'${stringify(actual)}' is not a number.`, failureMessage);
50
77
  }
51
78
  }
52
79
  function isObject(actual, failureMessage) {
53
- assertRuntimeType(actual, 'object', failureMessage);
80
+ if (Array.isArray(actual) || typeof actual !== 'object' || !actual) {
81
+ throw new AssertionError(`'${stringify(actual)}' is not a non-null object.`, failureMessage);
82
+ }
54
83
  }
55
84
  function isString(actual, failureMessage) {
56
- assertRuntimeType(actual, 'string', failureMessage);
85
+ if (typeof actual !== 'string') {
86
+ throw new AssertionError(`'${stringify(actual)}' is not a string.`, failureMessage);
87
+ }
57
88
  }
58
89
  function isSymbol(actual, failureMessage) {
59
- assertRuntimeType(actual, 'symbol', failureMessage);
90
+ if (typeof actual !== 'symbol') {
91
+ throw new AssertionError(`'${stringify(actual)}' is not a symbol.`, failureMessage);
92
+ }
60
93
  }
61
94
  function isUndefined(actual, failureMessage) {
62
- assertRuntimeType(actual, 'undefined', failureMessage);
95
+ if (typeof actual !== 'undefined') {
96
+ throw new AssertionError(`'${stringify(actual)}' is not a undefined.`, failureMessage);
97
+ }
63
98
  }
64
99
  function isNull(actual, failureMessage) {
65
- assertRuntimeType(actual, 'null', failureMessage);
100
+ if (actual !== null) {
101
+ throw new AssertionError(`'${stringify(actual)}' is not nul.`, failureMessage);
102
+ }
66
103
  }
67
104
  const assertions = {
68
105
  isArray,
@@ -1679,87 +1716,3 @@ export const runtimeTypeGuards = {
1679
1716
  isNotUndefined: autoGuard(),
1680
1717
  },
1681
1718
  };
1682
- /**
1683
- * An enum representing the possible values returned by {@link getRuntimeType}. These values are
1684
- * similar to the output of the built-in
1685
- * [`typeof`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/typeof) operator
1686
- * except that this includes new types `array` and `null`, that `typeof` does not have, which are
1687
- * both distinct from `object`.
1688
- *
1689
- * @category Assert : Util
1690
- * @category Package : @augment-vir/assert
1691
- * @package [`@augment-vir/assert`](https://www.npmjs.com/package/@augment-vir/assert)
1692
- */
1693
- export var RuntimeType;
1694
- (function (RuntimeType) {
1695
- RuntimeType["String"] = "string";
1696
- RuntimeType["Number"] = "number";
1697
- RuntimeType["Bigint"] = "bigint";
1698
- RuntimeType["Boolean"] = "boolean";
1699
- RuntimeType["Symbol"] = "symbol";
1700
- RuntimeType["Undefined"] = "undefined";
1701
- RuntimeType["Object"] = "object";
1702
- RuntimeType["Function"] = "function";
1703
- /**
1704
- * This is not included in {@link RuntimeType.Object}. (Compared to
1705
- * [`typeof`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/typeof)
1706
- * which _does_ include `null` in the `'object'` type.)
1707
- */
1708
- RuntimeType["Array"] = "array";
1709
- /**
1710
- * This is not included in {@link RuntimeType.Object}. (Compared to
1711
- * [`typeof`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/typeof)
1712
- * which _does_ include `null` in the `'object'` type.)
1713
- */
1714
- RuntimeType["Null"] = "null";
1715
- })(RuntimeType || (RuntimeType = {}));
1716
- /**
1717
- * Determines the {@link RuntimeType} of a variable. This is similar to the built-in
1718
- * [`typeof`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/typeof) operator
1719
- * except in the following ways:
1720
- *
1721
- * - This returns an enum value ({@link RuntimeType}) rather than just a string (though the enum values
1722
- * are strings anyway).
1723
- * - This includes new types `array` and `null`, that `typeof` does not have, which are both distinct
1724
- * from `object`.
1725
- *
1726
- * @category Assert : Util
1727
- * @category Package : @augment-vir/assert
1728
- * @example
1729
- *
1730
- * ```ts
1731
- * import {getRuntimeType} from '@augment-vir/assert';
1732
- *
1733
- * getRuntimeType(['a']); // RuntimeType.Array
1734
- * getRuntimeType({a: 'a'}); // RuntimeType.Object
1735
- * ```
1736
- *
1737
- * @package [`@augment-vir/assert`](https://www.npmjs.com/package/@augment-vir/assert)
1738
- */
1739
- export function getRuntimeType(actual) {
1740
- if (actual === null) {
1741
- return RuntimeType.Null;
1742
- }
1743
- else if (Array.isArray(actual)) {
1744
- return RuntimeType.Array;
1745
- }
1746
- else {
1747
- return typeof actual;
1748
- }
1749
- }
1750
- /**
1751
- * Asserts that the given actual matches the given test type. Note that an name for the actual must
1752
- * be provided for error messaging purposes.
1753
- */
1754
- function assertRuntimeType(actual, testType, failureMessage) {
1755
- const actualType = getRuntimeType(actual);
1756
- if (actualType !== testType) {
1757
- throw new AssertionError(`'${stringify(actual)}' is '${actualType}', not '${testType}'.`, failureMessage);
1758
- }
1759
- }
1760
- function assertNotRuntimeType(actual, testType, failureMessage) {
1761
- const actualType = getRuntimeType(actual);
1762
- if (actualType === testType) {
1763
- throw new AssertionError(`'${stringify(actual)}' is '${actualType}'.`, failureMessage);
1764
- }
1765
- }
@@ -1,5 +1,4 @@
1
1
  export type { Falsy, FalsyValue, Truthy } from '../assertions/boolean.js';
2
2
  export type { CustomOutputAsserter } from '../assertions/output.js';
3
- export { RuntimeType, getRuntimeType } from '../assertions/runtime-type.js';
4
3
  export type { ErrorMatchOptions } from '../assertions/throws.js';
5
4
  export type { CanBeEmpty, Empty } from '../assertions/values.js';
@@ -1 +1 @@
1
- export { RuntimeType, getRuntimeType } from '../assertions/runtime-type.js';
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@augment-vir/assert",
3
- "version": "30.8.3",
3
+ "version": "31.0.0",
4
4
  "description": "A collection of assertions for test and production code alike.",
5
5
  "keywords": [
6
6
  "augment",
@@ -41,11 +41,11 @@
41
41
  "test:update": "npm test"
42
42
  },
43
43
  "dependencies": {
44
- "@augment-vir/core": "^30.8.3",
44
+ "@augment-vir/core": "^31.0.0",
45
45
  "@date-vir/duration": "^7.0.1",
46
46
  "deep-eql": "^5.0.2",
47
47
  "expect-type": "^1.1.0",
48
- "type-fest": "^4.26.1"
48
+ "type-fest": "^4.29.0"
49
49
  },
50
50
  "devDependencies": {
51
51
  "@types/deep-eql": "^4.0.2",