@kensio/smartass 0.0.2 → 0.0.4
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 +23 -52
- package/package.json +6 -3
package/README.md
CHANGED
|
@@ -12,67 +12,38 @@ unable to provide type information to TypeScript and IntelliSense.
|
|
|
12
12
|
This library provides assertion functions with assertion signatures so that you
|
|
13
13
|
can benefit from the extra type information.
|
|
14
14
|
|
|
15
|
-
## Installation
|
|
16
|
-
|
|
17
|
-
```bash
|
|
18
|
-
npm install @kensio/smartass
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
## Usage
|
|
22
|
-
|
|
23
|
-
### assertNonNullable
|
|
24
|
-
|
|
25
|
-
Assert that a value is neither null nor undefined, with type narrowing.
|
|
26
|
-
|
|
27
15
|
```typescript
|
|
28
16
|
import { assertNonNullable } from '@kensio/smartass';
|
|
29
17
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
expect(user.name).toBe('Alice');
|
|
36
|
-
});
|
|
18
|
+
const user: { name: string } | undefined = getUser();
|
|
19
|
+
assertNonNullable(user);
|
|
20
|
+
// TypeScript now knows user is neither null nor undefined,
|
|
21
|
+
// so no need for ? or ! operators.
|
|
22
|
+
const userName = user.name;
|
|
37
23
|
```
|
|
38
24
|
|
|
39
|
-
### assertNotEmpty
|
|
40
|
-
|
|
41
|
-
Assert that an array is non-empty, with type narrowing to a tuple type.
|
|
42
|
-
|
|
43
|
-
```typescript
|
|
44
|
-
import { assertNotEmpty } from '@kensio/smartass';
|
|
45
|
-
|
|
46
|
-
test('first item is correct', () => {
|
|
47
|
-
const items: string[] = getItems();
|
|
48
|
-
assertNotEmpty(items);
|
|
49
|
-
// TypeScript now knows items is an array of strings with at
|
|
50
|
-
// least one element, so no need for ? operator.
|
|
51
|
-
expect(items[0]).toBe('first');
|
|
52
|
-
});
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
### assertOneOf
|
|
56
|
-
|
|
57
|
-
Assert that a value is one of a set of allowed values, with type narrowing.
|
|
58
|
-
|
|
59
25
|
```typescript
|
|
60
26
|
import { assertOneOf } from '@kensio/smartass';
|
|
61
27
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
// TypeScript now knows status is 'pending' | 'active' | 'completed'
|
|
66
|
-
expect(status).toBe('active');
|
|
67
|
-
});
|
|
28
|
+
const status = getStatus();
|
|
29
|
+
assertOneOf(status, ['pending', 'active', 'completed']);
|
|
30
|
+
// TypeScript now knows status is of type 'pending' | 'active' | 'completed'
|
|
68
31
|
```
|
|
69
32
|
|
|
70
|
-
##
|
|
71
|
-
|
|
72
|
-
All assertion functions accept an optional custom error message:
|
|
33
|
+
## Installation
|
|
73
34
|
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
assertNotEmpty(items, 'Items array cannot be empty');
|
|
77
|
-
assertOneOf(status, ['open', 'closed'], 'Status must be either open or closed');
|
|
35
|
+
```bash
|
|
36
|
+
npm install @kensio/smartass
|
|
78
37
|
```
|
|
38
|
+
|
|
39
|
+
## Assertion functions
|
|
40
|
+
|
|
41
|
+
- [assertNonNullable](src/assert/non-nullable/non-nullable.assert.ts)
|
|
42
|
+
- [assertNotEmpty](src/assert/not-empty/not-empty.assert.ts)
|
|
43
|
+
- [assertOneOf](src/assert/one-of/one-of.assert.ts)
|
|
44
|
+
- [assertTypeBigInt](src/assert/type-bigint/type-bigint.assert.ts)
|
|
45
|
+
- [assertTypeBoolean](src/assert/type-boolean/type-boolean.assert.ts)
|
|
46
|
+
- [assertTypeFunction](src/assert/type-function/type-function.assert.ts)
|
|
47
|
+
- [assertTypeNumber](src/assert/type-number/type-number.assert.ts)
|
|
48
|
+
- [assertTypeObject](src/assert/type-object/type-object.assert.ts)
|
|
49
|
+
- [assertTypeString](src/assert/type-string/type-string.assert.ts)
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"author": "Kensio Software",
|
|
5
5
|
"repository": "https://github.com/KensioSoftware/smartass",
|
|
6
6
|
"license": "AGPL-3.0",
|
|
7
|
-
"version": "0.0.
|
|
7
|
+
"version": "0.0.4",
|
|
8
8
|
"type": "module",
|
|
9
9
|
"main": "./dist/index.js",
|
|
10
10
|
"types": "./dist/index.d.ts",
|
|
@@ -21,6 +21,9 @@
|
|
|
21
21
|
"access": "public"
|
|
22
22
|
},
|
|
23
23
|
"sideEffects": false,
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=24.14.0"
|
|
26
|
+
},
|
|
24
27
|
"keywords": [
|
|
25
28
|
"test",
|
|
26
29
|
"assertion",
|
|
@@ -48,8 +51,8 @@
|
|
|
48
51
|
},
|
|
49
52
|
"scripts": {
|
|
50
53
|
"build": "tsc -p tsconfig.build.json",
|
|
51
|
-
"fmt": "eslint --fix '{src,test}/**/*.
|
|
52
|
-
"lint": "eslint '{src,test}/**/*.
|
|
54
|
+
"fmt": "eslint --fix '{src,test}/**/*.ts' && prettier --write '{src,test}/**/*.{ts,json,md,css,html,yml,yaml}'",
|
|
55
|
+
"lint": "eslint '{src,test}/**/*.ts' && prettier --check '{src,test}/**/*.{ts,json,md,css,html,yml,yaml}'",
|
|
53
56
|
"test": "vitest run",
|
|
54
57
|
"test:coverage": "vitest run --coverage"
|
|
55
58
|
}
|