@dloizides/utils 1.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/CHANGELOG.md +24 -0
- package/LICENSE +21 -0
- package/README.md +95 -0
- package/dist/assertions/index.d.mts +1 -0
- package/dist/assertions/index.d.ts +1 -0
- package/dist/assertions/index.js +12 -0
- package/dist/assertions/index.js.map +1 -0
- package/dist/assertions/index.mjs +10 -0
- package/dist/assertions/index.mjs.map +1 -0
- package/dist/guards/index.d.mts +1 -0
- package/dist/guards/index.d.ts +1 -0
- package/dist/guards/index.js +40 -0
- package/dist/guards/index.js.map +1 -0
- package/dist/guards/index.mjs +33 -0
- package/dist/guards/index.mjs.map +1 -0
- package/dist/index-Dge_3DNw.d.mts +115 -0
- package/dist/index-Dge_3DNw.d.ts +115 -0
- package/dist/index-DmagV9KM.d.mts +24 -0
- package/dist/index-DmagV9KM.d.ts +24 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +73 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +63 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +93 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [1.0.0] - 2024-01-27
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- Initial release
|
|
13
|
+
- Type guards:
|
|
14
|
+
- `isValueDefined` - Check if value is not null or undefined
|
|
15
|
+
- `isNotEmptyArray` - Check if value is a non-empty array
|
|
16
|
+
- `isNotEmptyString` - Check if value is a non-empty string (after trim)
|
|
17
|
+
- `isNullOrUndefined` - Check if value is null or undefined
|
|
18
|
+
- `isEmptyArray` - Check if value is an empty array
|
|
19
|
+
- `isEmptyString` - Check if value is empty or whitespace only
|
|
20
|
+
- Assertions:
|
|
21
|
+
- `assertDefined` - Assert that value is defined (throws if null/undefined)
|
|
22
|
+
- Full TypeScript support with type narrowing
|
|
23
|
+
- ESM and CommonJS module support
|
|
24
|
+
- 100% test coverage
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 dloizides
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# @dloizides/utils
|
|
2
|
+
|
|
3
|
+
> Type-safe utility functions for TypeScript projects
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@dloizides/utils)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @dloizides/utils
|
|
12
|
+
# or
|
|
13
|
+
yarn add @dloizides/utils
|
|
14
|
+
# or
|
|
15
|
+
pnpm add @dloizides/utils
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Features
|
|
19
|
+
|
|
20
|
+
- **Type Guards**: `isValueDefined`, `isNotEmptyArray`, `isNotEmptyString`, `isNullOrUndefined`, `isEmptyArray`, `isEmptyString`
|
|
21
|
+
- **Assertions**: `assertDefined`
|
|
22
|
+
- **Zero Dependencies**: No runtime dependencies
|
|
23
|
+
- **Tree-shakeable**: Only import what you need
|
|
24
|
+
- **TypeScript First**: Full type inference and narrowing
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
### Type Guards
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { isValueDefined, isNotEmptyArray, isNotEmptyString } from '@dloizides/utils';
|
|
32
|
+
|
|
33
|
+
// isValueDefined - Check if value is not null or undefined
|
|
34
|
+
const user = getUserOrNull();
|
|
35
|
+
if (isValueDefined(user)) {
|
|
36
|
+
console.log(user.name); // TypeScript knows user is defined
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// isNotEmptyArray - Check if array has elements
|
|
40
|
+
const items = getItems();
|
|
41
|
+
if (isNotEmptyArray(items)) {
|
|
42
|
+
console.log(items[0]); // Safe to access first element
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// isNotEmptyString - Check if string has content
|
|
46
|
+
const input = formData.get('name');
|
|
47
|
+
if (isNotEmptyString(input)) {
|
|
48
|
+
saveUser(input); // String is guaranteed to have content
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Assertions
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
import { assertDefined } from '@dloizides/utils';
|
|
56
|
+
|
|
57
|
+
function processUser(user: User | null) {
|
|
58
|
+
assertDefined(user, 'User is required');
|
|
59
|
+
// user is now narrowed to User
|
|
60
|
+
return user.name.toUpperCase();
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Subpath Imports
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
// Import only guards
|
|
68
|
+
import { isValueDefined } from '@dloizides/utils/guards';
|
|
69
|
+
|
|
70
|
+
// Import only assertions
|
|
71
|
+
import { assertDefined } from '@dloizides/utils/assertions';
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## API Reference
|
|
75
|
+
|
|
76
|
+
### Type Guards
|
|
77
|
+
|
|
78
|
+
| Function | Description |
|
|
79
|
+
|----------|-------------|
|
|
80
|
+
| `isValueDefined<T>(value)` | Returns `true` if value is not `null` or `undefined` |
|
|
81
|
+
| `isNotEmptyArray<T>(value)` | Returns `true` if value is an array with length > 0 |
|
|
82
|
+
| `isNotEmptyString(value)` | Returns `true` if value is a non-empty string (after trim) |
|
|
83
|
+
| `isNullOrUndefined(value)` | Returns `true` if value is `null` or `undefined` |
|
|
84
|
+
| `isEmptyArray(value)` | Returns `true` if value is an empty array |
|
|
85
|
+
| `isEmptyString(value)` | Returns `true` if value is empty or whitespace only |
|
|
86
|
+
|
|
87
|
+
### Assertions
|
|
88
|
+
|
|
89
|
+
| Function | Description |
|
|
90
|
+
|----------|-------------|
|
|
91
|
+
| `assertDefined<T>(value, message?)` | Throws if value is `null` or `undefined` |
|
|
92
|
+
|
|
93
|
+
## License
|
|
94
|
+
|
|
95
|
+
MIT
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { a as assertDefined } from '../index-DmagV9KM.mjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { a as assertDefined } from '../index-DmagV9KM.js';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/assertions/assertDefined.ts
|
|
4
|
+
function assertDefined(value, message) {
|
|
5
|
+
if (value === void 0 || value === null) {
|
|
6
|
+
throw new Error(message ?? "Value must be defined");
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
exports.assertDefined = assertDefined;
|
|
11
|
+
//# sourceMappingURL=index.js.map
|
|
12
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/assertions/assertDefined.ts"],"names":[],"mappings":";;;AAgBO,SAAS,aAAA,CACd,OACA,OAAA,EACoB;AACpB,EAAA,IAAI,KAAA,KAAU,MAAA,IAAa,KAAA,KAAU,IAAA,EAAM;AACzC,IAAA,MAAM,IAAI,KAAA,CAAM,OAAA,IAAW,uBAAuB,CAAA;AAAA,EACpD;AACF","file":"index.js","sourcesContent":["/**\r\n * Asserts that a value is defined (not null or undefined).\r\n * Throws an error if the value is null or undefined.\r\n *\r\n * @param value - The value to assert\r\n * @param message - Optional error message\r\n * @throws Error if value is null or undefined\r\n *\r\n * @example\r\n * ```typescript\r\n * const user = getUser(); // User | null\r\n * assertDefined(user, 'User must exist');\r\n * // user is now narrowed to User\r\n * console.log(user.name);\r\n * ```\r\n */\r\nexport function assertDefined<T>(\r\n value: T | null | undefined,\r\n message?: string\r\n): asserts value is T {\r\n if (value === undefined || value === null) {\r\n throw new Error(message ?? 'Value must be defined');\r\n }\r\n}\r\n"]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// src/assertions/assertDefined.ts
|
|
2
|
+
function assertDefined(value, message) {
|
|
3
|
+
if (value === void 0 || value === null) {
|
|
4
|
+
throw new Error(message ?? "Value must be defined");
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export { assertDefined };
|
|
9
|
+
//# sourceMappingURL=index.mjs.map
|
|
10
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/assertions/assertDefined.ts"],"names":[],"mappings":";AAgBO,SAAS,aAAA,CACd,OACA,OAAA,EACoB;AACpB,EAAA,IAAI,KAAA,KAAU,MAAA,IAAa,KAAA,KAAU,IAAA,EAAM;AACzC,IAAA,MAAM,IAAI,KAAA,CAAM,OAAA,IAAW,uBAAuB,CAAA;AAAA,EACpD;AACF","file":"index.mjs","sourcesContent":["/**\r\n * Asserts that a value is defined (not null or undefined).\r\n * Throws an error if the value is null or undefined.\r\n *\r\n * @param value - The value to assert\r\n * @param message - Optional error message\r\n * @throws Error if value is null or undefined\r\n *\r\n * @example\r\n * ```typescript\r\n * const user = getUser(); // User | null\r\n * assertDefined(user, 'User must exist');\r\n * // user is now narrowed to User\r\n * console.log(user.name);\r\n * ```\r\n */\r\nexport function assertDefined<T>(\r\n value: T | null | undefined,\r\n message?: string\r\n): asserts value is T {\r\n if (value === undefined || value === null) {\r\n throw new Error(message ?? 'Value must be defined');\r\n }\r\n}\r\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { a as isEmptyArray, b as isEmptyString, c as isNotEmptyArray, d as isNotEmptyString, e as isNullOrUndefined, f as isValueDefined } from '../index-Dge_3DNw.mjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { a as isEmptyArray, b as isEmptyString, c as isNotEmptyArray, d as isNotEmptyString, e as isNullOrUndefined, f as isValueDefined } from '../index-Dge_3DNw.js';
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/guards/isValueDefined.ts
|
|
4
|
+
function isValueDefined(value) {
|
|
5
|
+
return value !== void 0 && value !== null;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// src/guards/isNotEmptyArray.ts
|
|
9
|
+
function isNotEmptyArray(value) {
|
|
10
|
+
return Array.isArray(value) && value.length > 0;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// src/guards/isNotEmptyString.ts
|
|
14
|
+
function isNotEmptyString(value) {
|
|
15
|
+
return typeof value === "string" && value.trim().length > 0;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// src/guards/isNullOrUndefined.ts
|
|
19
|
+
function isNullOrUndefined(value) {
|
|
20
|
+
return value === void 0 || value === null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// src/guards/isEmptyArray.ts
|
|
24
|
+
function isEmptyArray(value) {
|
|
25
|
+
return Array.isArray(value) && value.length === 0;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// src/guards/isEmptyString.ts
|
|
29
|
+
function isEmptyString(value) {
|
|
30
|
+
return typeof value === "string" && value.trim().length === 0;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
exports.isEmptyArray = isEmptyArray;
|
|
34
|
+
exports.isEmptyString = isEmptyString;
|
|
35
|
+
exports.isNotEmptyArray = isNotEmptyArray;
|
|
36
|
+
exports.isNotEmptyString = isNotEmptyString;
|
|
37
|
+
exports.isNullOrUndefined = isNullOrUndefined;
|
|
38
|
+
exports.isValueDefined = isValueDefined;
|
|
39
|
+
//# sourceMappingURL=index.js.map
|
|
40
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/guards/isValueDefined.ts","../../src/guards/isNotEmptyArray.ts","../../src/guards/isNotEmptyString.ts","../../src/guards/isNullOrUndefined.ts","../../src/guards/isEmptyArray.ts","../../src/guards/isEmptyString.ts"],"names":[],"mappings":";;;AAeO,SAAS,eAAkB,KAAA,EAAyC;AACzE,EAAA,OAAO,KAAA,KAAU,UAAa,KAAA,KAAU,IAAA;AAC1C;;;ACFO,SAAS,gBACd,KAAA,EACqD;AACrD,EAAA,OAAO,KAAA,CAAM,OAAA,CAAQ,KAAK,CAAA,IAAK,MAAM,MAAA,GAAS,CAAA;AAChD;;;ACLO,SAAS,iBAAiB,KAAA,EAAmD;AAClF,EAAA,OAAO,OAAO,KAAA,KAAU,QAAA,IAAY,KAAA,CAAM,IAAA,GAAO,MAAA,GAAS,CAAA;AAC5D;;;ACDO,SAAS,kBAAkB,KAAA,EAA2C;AAC3E,EAAA,OAAO,KAAA,KAAU,UAAa,KAAA,KAAU,IAAA;AAC1C;;;ACHO,SAAS,aACd,KAAA,EACgD;AAChD,EAAA,OAAO,KAAA,CAAM,OAAA,CAAQ,KAAK,CAAA,IAAK,MAAM,MAAA,KAAW,CAAA;AAClD;;;ACHO,SAAS,cAAc,KAAA,EAA2C;AACvE,EAAA,OAAO,OAAO,KAAA,KAAU,QAAA,IAAY,KAAA,CAAM,IAAA,GAAO,MAAA,KAAW,CAAA;AAC9D","file":"index.js","sourcesContent":["/**\r\n * Type guard that checks if a value is neither null nor undefined.\r\n *\r\n * @param value - The value to check\r\n * @returns True if the value is defined (not null and not undefined)\r\n *\r\n * @example\r\n * ```typescript\r\n * const value: string | null | undefined = getValue();\r\n * if (isValueDefined(value)) {\r\n * // value is narrowed to string\r\n * console.log(value.length);\r\n * }\r\n * ```\r\n */\r\nexport function isValueDefined<T>(value: T | null | undefined): value is T {\r\n return value !== undefined && value !== null;\r\n}\r\n","/**\r\n * Type guard that checks if a value is a non-empty array.\r\n *\r\n * @param value - The value to check\r\n * @returns True if the value is an array with at least one element\r\n *\r\n * @example\r\n * ```typescript\r\n * const items: string[] | undefined = getItems();\r\n * if (isNotEmptyArray(items)) {\r\n * // items is narrowed to string[] with at least one element\r\n * console.log(items[0]); // Safe to access\r\n * }\r\n * ```\r\n */\r\nexport function isNotEmptyArray<T>(\r\n value: readonly T[] | null | undefined\r\n): value is readonly T[] & { readonly length: number } {\r\n return Array.isArray(value) && value.length > 0;\r\n}\r\n","/**\r\n * Checks if a value is a non-empty string (after trimming whitespace).\r\n *\r\n * @param value - The value to check\r\n * @returns True if the value is a string with non-whitespace content\r\n *\r\n * @example\r\n * ```typescript\r\n * isNotEmptyString('hello'); // true\r\n * isNotEmptyString(' '); // false\r\n * isNotEmptyString(''); // false\r\n * isNotEmptyString(undefined); // false\r\n * ```\r\n */\r\nexport function isNotEmptyString(value: string | null | undefined): value is string {\r\n return typeof value === 'string' && value.trim().length > 0;\r\n}\r\n","/**\r\n * Type guard that checks if a value is null or undefined.\r\n *\r\n * @param value - The value to check\r\n * @returns True if the value is null or undefined\r\n *\r\n * @example\r\n * ```typescript\r\n * const value: string | null | undefined = getValue();\r\n * if (isNullOrUndefined(value)) {\r\n * // value is narrowed to null | undefined\r\n * console.log('Value is not set');\r\n * }\r\n * ```\r\n */\r\nexport function isNullOrUndefined(value: unknown): value is null | undefined {\r\n return value === undefined || value === null;\r\n}\r\n","/**\r\n * Type guard that checks if a value is an empty array.\r\n *\r\n * @param value - The value to check\r\n * @returns True if the value is an array with zero elements\r\n *\r\n * @example\r\n * ```typescript\r\n * const items: string[] = getItems();\r\n * if (isEmptyArray(items)) {\r\n * console.log('No items found');\r\n * }\r\n * ```\r\n */\r\nexport function isEmptyArray<T>(\r\n value: readonly T[] | null | undefined\r\n): value is readonly T[] & { readonly length: 0 } {\r\n return Array.isArray(value) && value.length === 0;\r\n}\r\n","/**\r\n * Checks if a value is an empty string or contains only whitespace.\r\n *\r\n * @param value - The value to check\r\n * @returns True if the value is an empty string or whitespace-only\r\n *\r\n * @example\r\n * ```typescript\r\n * isEmptyString(''); // true\r\n * isEmptyString(' '); // true\r\n * isEmptyString('hello'); // false\r\n * isEmptyString(undefined); // false (not a string)\r\n * isEmptyString(null); // false (not a string)\r\n * ```\r\n */\r\nexport function isEmptyString(value: string | null | undefined): boolean {\r\n return typeof value === 'string' && value.trim().length === 0;\r\n}\r\n"]}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// src/guards/isValueDefined.ts
|
|
2
|
+
function isValueDefined(value) {
|
|
3
|
+
return value !== void 0 && value !== null;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
// src/guards/isNotEmptyArray.ts
|
|
7
|
+
function isNotEmptyArray(value) {
|
|
8
|
+
return Array.isArray(value) && value.length > 0;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// src/guards/isNotEmptyString.ts
|
|
12
|
+
function isNotEmptyString(value) {
|
|
13
|
+
return typeof value === "string" && value.trim().length > 0;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// src/guards/isNullOrUndefined.ts
|
|
17
|
+
function isNullOrUndefined(value) {
|
|
18
|
+
return value === void 0 || value === null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// src/guards/isEmptyArray.ts
|
|
22
|
+
function isEmptyArray(value) {
|
|
23
|
+
return Array.isArray(value) && value.length === 0;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// src/guards/isEmptyString.ts
|
|
27
|
+
function isEmptyString(value) {
|
|
28
|
+
return typeof value === "string" && value.trim().length === 0;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { isEmptyArray, isEmptyString, isNotEmptyArray, isNotEmptyString, isNullOrUndefined, isValueDefined };
|
|
32
|
+
//# sourceMappingURL=index.mjs.map
|
|
33
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/guards/isValueDefined.ts","../../src/guards/isNotEmptyArray.ts","../../src/guards/isNotEmptyString.ts","../../src/guards/isNullOrUndefined.ts","../../src/guards/isEmptyArray.ts","../../src/guards/isEmptyString.ts"],"names":[],"mappings":";AAeO,SAAS,eAAkB,KAAA,EAAyC;AACzE,EAAA,OAAO,KAAA,KAAU,UAAa,KAAA,KAAU,IAAA;AAC1C;;;ACFO,SAAS,gBACd,KAAA,EACqD;AACrD,EAAA,OAAO,KAAA,CAAM,OAAA,CAAQ,KAAK,CAAA,IAAK,MAAM,MAAA,GAAS,CAAA;AAChD;;;ACLO,SAAS,iBAAiB,KAAA,EAAmD;AAClF,EAAA,OAAO,OAAO,KAAA,KAAU,QAAA,IAAY,KAAA,CAAM,IAAA,GAAO,MAAA,GAAS,CAAA;AAC5D;;;ACDO,SAAS,kBAAkB,KAAA,EAA2C;AAC3E,EAAA,OAAO,KAAA,KAAU,UAAa,KAAA,KAAU,IAAA;AAC1C;;;ACHO,SAAS,aACd,KAAA,EACgD;AAChD,EAAA,OAAO,KAAA,CAAM,OAAA,CAAQ,KAAK,CAAA,IAAK,MAAM,MAAA,KAAW,CAAA;AAClD;;;ACHO,SAAS,cAAc,KAAA,EAA2C;AACvE,EAAA,OAAO,OAAO,KAAA,KAAU,QAAA,IAAY,KAAA,CAAM,IAAA,GAAO,MAAA,KAAW,CAAA;AAC9D","file":"index.mjs","sourcesContent":["/**\r\n * Type guard that checks if a value is neither null nor undefined.\r\n *\r\n * @param value - The value to check\r\n * @returns True if the value is defined (not null and not undefined)\r\n *\r\n * @example\r\n * ```typescript\r\n * const value: string | null | undefined = getValue();\r\n * if (isValueDefined(value)) {\r\n * // value is narrowed to string\r\n * console.log(value.length);\r\n * }\r\n * ```\r\n */\r\nexport function isValueDefined<T>(value: T | null | undefined): value is T {\r\n return value !== undefined && value !== null;\r\n}\r\n","/**\r\n * Type guard that checks if a value is a non-empty array.\r\n *\r\n * @param value - The value to check\r\n * @returns True if the value is an array with at least one element\r\n *\r\n * @example\r\n * ```typescript\r\n * const items: string[] | undefined = getItems();\r\n * if (isNotEmptyArray(items)) {\r\n * // items is narrowed to string[] with at least one element\r\n * console.log(items[0]); // Safe to access\r\n * }\r\n * ```\r\n */\r\nexport function isNotEmptyArray<T>(\r\n value: readonly T[] | null | undefined\r\n): value is readonly T[] & { readonly length: number } {\r\n return Array.isArray(value) && value.length > 0;\r\n}\r\n","/**\r\n * Checks if a value is a non-empty string (after trimming whitespace).\r\n *\r\n * @param value - The value to check\r\n * @returns True if the value is a string with non-whitespace content\r\n *\r\n * @example\r\n * ```typescript\r\n * isNotEmptyString('hello'); // true\r\n * isNotEmptyString(' '); // false\r\n * isNotEmptyString(''); // false\r\n * isNotEmptyString(undefined); // false\r\n * ```\r\n */\r\nexport function isNotEmptyString(value: string | null | undefined): value is string {\r\n return typeof value === 'string' && value.trim().length > 0;\r\n}\r\n","/**\r\n * Type guard that checks if a value is null or undefined.\r\n *\r\n * @param value - The value to check\r\n * @returns True if the value is null or undefined\r\n *\r\n * @example\r\n * ```typescript\r\n * const value: string | null | undefined = getValue();\r\n * if (isNullOrUndefined(value)) {\r\n * // value is narrowed to null | undefined\r\n * console.log('Value is not set');\r\n * }\r\n * ```\r\n */\r\nexport function isNullOrUndefined(value: unknown): value is null | undefined {\r\n return value === undefined || value === null;\r\n}\r\n","/**\r\n * Type guard that checks if a value is an empty array.\r\n *\r\n * @param value - The value to check\r\n * @returns True if the value is an array with zero elements\r\n *\r\n * @example\r\n * ```typescript\r\n * const items: string[] = getItems();\r\n * if (isEmptyArray(items)) {\r\n * console.log('No items found');\r\n * }\r\n * ```\r\n */\r\nexport function isEmptyArray<T>(\r\n value: readonly T[] | null | undefined\r\n): value is readonly T[] & { readonly length: 0 } {\r\n return Array.isArray(value) && value.length === 0;\r\n}\r\n","/**\r\n * Checks if a value is an empty string or contains only whitespace.\r\n *\r\n * @param value - The value to check\r\n * @returns True if the value is an empty string or whitespace-only\r\n *\r\n * @example\r\n * ```typescript\r\n * isEmptyString(''); // true\r\n * isEmptyString(' '); // true\r\n * isEmptyString('hello'); // false\r\n * isEmptyString(undefined); // false (not a string)\r\n * isEmptyString(null); // false (not a string)\r\n * ```\r\n */\r\nexport function isEmptyString(value: string | null | undefined): boolean {\r\n return typeof value === 'string' && value.trim().length === 0;\r\n}\r\n"]}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type guard that checks if a value is neither null nor undefined.
|
|
3
|
+
*
|
|
4
|
+
* @param value - The value to check
|
|
5
|
+
* @returns True if the value is defined (not null and not undefined)
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* const value: string | null | undefined = getValue();
|
|
10
|
+
* if (isValueDefined(value)) {
|
|
11
|
+
* // value is narrowed to string
|
|
12
|
+
* console.log(value.length);
|
|
13
|
+
* }
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
declare function isValueDefined<T>(value: T | null | undefined): value is T;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Type guard that checks if a value is a non-empty array.
|
|
20
|
+
*
|
|
21
|
+
* @param value - The value to check
|
|
22
|
+
* @returns True if the value is an array with at least one element
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* const items: string[] | undefined = getItems();
|
|
27
|
+
* if (isNotEmptyArray(items)) {
|
|
28
|
+
* // items is narrowed to string[] with at least one element
|
|
29
|
+
* console.log(items[0]); // Safe to access
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
declare function isNotEmptyArray<T>(value: readonly T[] | null | undefined): value is readonly T[] & {
|
|
34
|
+
readonly length: number;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Checks if a value is a non-empty string (after trimming whitespace).
|
|
39
|
+
*
|
|
40
|
+
* @param value - The value to check
|
|
41
|
+
* @returns True if the value is a string with non-whitespace content
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* isNotEmptyString('hello'); // true
|
|
46
|
+
* isNotEmptyString(' '); // false
|
|
47
|
+
* isNotEmptyString(''); // false
|
|
48
|
+
* isNotEmptyString(undefined); // false
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
declare function isNotEmptyString(value: string | null | undefined): value is string;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Type guard that checks if a value is null or undefined.
|
|
55
|
+
*
|
|
56
|
+
* @param value - The value to check
|
|
57
|
+
* @returns True if the value is null or undefined
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* const value: string | null | undefined = getValue();
|
|
62
|
+
* if (isNullOrUndefined(value)) {
|
|
63
|
+
* // value is narrowed to null | undefined
|
|
64
|
+
* console.log('Value is not set');
|
|
65
|
+
* }
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
declare function isNullOrUndefined(value: unknown): value is null | undefined;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Type guard that checks if a value is an empty array.
|
|
72
|
+
*
|
|
73
|
+
* @param value - The value to check
|
|
74
|
+
* @returns True if the value is an array with zero elements
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* const items: string[] = getItems();
|
|
79
|
+
* if (isEmptyArray(items)) {
|
|
80
|
+
* console.log('No items found');
|
|
81
|
+
* }
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
declare function isEmptyArray<T>(value: readonly T[] | null | undefined): value is readonly T[] & {
|
|
85
|
+
readonly length: 0;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Checks if a value is an empty string or contains only whitespace.
|
|
90
|
+
*
|
|
91
|
+
* @param value - The value to check
|
|
92
|
+
* @returns True if the value is an empty string or whitespace-only
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```typescript
|
|
96
|
+
* isEmptyString(''); // true
|
|
97
|
+
* isEmptyString(' '); // true
|
|
98
|
+
* isEmptyString('hello'); // false
|
|
99
|
+
* isEmptyString(undefined); // false (not a string)
|
|
100
|
+
* isEmptyString(null); // false (not a string)
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
declare function isEmptyString(value: string | null | undefined): boolean;
|
|
104
|
+
|
|
105
|
+
declare const index_isEmptyArray: typeof isEmptyArray;
|
|
106
|
+
declare const index_isEmptyString: typeof isEmptyString;
|
|
107
|
+
declare const index_isNotEmptyArray: typeof isNotEmptyArray;
|
|
108
|
+
declare const index_isNotEmptyString: typeof isNotEmptyString;
|
|
109
|
+
declare const index_isNullOrUndefined: typeof isNullOrUndefined;
|
|
110
|
+
declare const index_isValueDefined: typeof isValueDefined;
|
|
111
|
+
declare namespace index {
|
|
112
|
+
export { index_isEmptyArray as isEmptyArray, index_isEmptyString as isEmptyString, index_isNotEmptyArray as isNotEmptyArray, index_isNotEmptyString as isNotEmptyString, index_isNullOrUndefined as isNullOrUndefined, index_isValueDefined as isValueDefined };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export { isEmptyArray as a, isEmptyString as b, isNotEmptyArray as c, isNotEmptyString as d, isNullOrUndefined as e, isValueDefined as f, index as i };
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type guard that checks if a value is neither null nor undefined.
|
|
3
|
+
*
|
|
4
|
+
* @param value - The value to check
|
|
5
|
+
* @returns True if the value is defined (not null and not undefined)
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* const value: string | null | undefined = getValue();
|
|
10
|
+
* if (isValueDefined(value)) {
|
|
11
|
+
* // value is narrowed to string
|
|
12
|
+
* console.log(value.length);
|
|
13
|
+
* }
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
declare function isValueDefined<T>(value: T | null | undefined): value is T;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Type guard that checks if a value is a non-empty array.
|
|
20
|
+
*
|
|
21
|
+
* @param value - The value to check
|
|
22
|
+
* @returns True if the value is an array with at least one element
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* const items: string[] | undefined = getItems();
|
|
27
|
+
* if (isNotEmptyArray(items)) {
|
|
28
|
+
* // items is narrowed to string[] with at least one element
|
|
29
|
+
* console.log(items[0]); // Safe to access
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
declare function isNotEmptyArray<T>(value: readonly T[] | null | undefined): value is readonly T[] & {
|
|
34
|
+
readonly length: number;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Checks if a value is a non-empty string (after trimming whitespace).
|
|
39
|
+
*
|
|
40
|
+
* @param value - The value to check
|
|
41
|
+
* @returns True if the value is a string with non-whitespace content
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* isNotEmptyString('hello'); // true
|
|
46
|
+
* isNotEmptyString(' '); // false
|
|
47
|
+
* isNotEmptyString(''); // false
|
|
48
|
+
* isNotEmptyString(undefined); // false
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
declare function isNotEmptyString(value: string | null | undefined): value is string;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Type guard that checks if a value is null or undefined.
|
|
55
|
+
*
|
|
56
|
+
* @param value - The value to check
|
|
57
|
+
* @returns True if the value is null or undefined
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* const value: string | null | undefined = getValue();
|
|
62
|
+
* if (isNullOrUndefined(value)) {
|
|
63
|
+
* // value is narrowed to null | undefined
|
|
64
|
+
* console.log('Value is not set');
|
|
65
|
+
* }
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
declare function isNullOrUndefined(value: unknown): value is null | undefined;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Type guard that checks if a value is an empty array.
|
|
72
|
+
*
|
|
73
|
+
* @param value - The value to check
|
|
74
|
+
* @returns True if the value is an array with zero elements
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* const items: string[] = getItems();
|
|
79
|
+
* if (isEmptyArray(items)) {
|
|
80
|
+
* console.log('No items found');
|
|
81
|
+
* }
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
declare function isEmptyArray<T>(value: readonly T[] | null | undefined): value is readonly T[] & {
|
|
85
|
+
readonly length: 0;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Checks if a value is an empty string or contains only whitespace.
|
|
90
|
+
*
|
|
91
|
+
* @param value - The value to check
|
|
92
|
+
* @returns True if the value is an empty string or whitespace-only
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```typescript
|
|
96
|
+
* isEmptyString(''); // true
|
|
97
|
+
* isEmptyString(' '); // true
|
|
98
|
+
* isEmptyString('hello'); // false
|
|
99
|
+
* isEmptyString(undefined); // false (not a string)
|
|
100
|
+
* isEmptyString(null); // false (not a string)
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
declare function isEmptyString(value: string | null | undefined): boolean;
|
|
104
|
+
|
|
105
|
+
declare const index_isEmptyArray: typeof isEmptyArray;
|
|
106
|
+
declare const index_isEmptyString: typeof isEmptyString;
|
|
107
|
+
declare const index_isNotEmptyArray: typeof isNotEmptyArray;
|
|
108
|
+
declare const index_isNotEmptyString: typeof isNotEmptyString;
|
|
109
|
+
declare const index_isNullOrUndefined: typeof isNullOrUndefined;
|
|
110
|
+
declare const index_isValueDefined: typeof isValueDefined;
|
|
111
|
+
declare namespace index {
|
|
112
|
+
export { index_isEmptyArray as isEmptyArray, index_isEmptyString as isEmptyString, index_isNotEmptyArray as isNotEmptyArray, index_isNotEmptyString as isNotEmptyString, index_isNullOrUndefined as isNullOrUndefined, index_isValueDefined as isValueDefined };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export { isEmptyArray as a, isEmptyString as b, isNotEmptyArray as c, isNotEmptyString as d, isNullOrUndefined as e, isValueDefined as f, index as i };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Asserts that a value is defined (not null or undefined).
|
|
3
|
+
* Throws an error if the value is null or undefined.
|
|
4
|
+
*
|
|
5
|
+
* @param value - The value to assert
|
|
6
|
+
* @param message - Optional error message
|
|
7
|
+
* @throws Error if value is null or undefined
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* const user = getUser(); // User | null
|
|
12
|
+
* assertDefined(user, 'User must exist');
|
|
13
|
+
* // user is now narrowed to User
|
|
14
|
+
* console.log(user.name);
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
declare function assertDefined<T>(value: T | null | undefined, message?: string): asserts value is T;
|
|
18
|
+
|
|
19
|
+
declare const index_assertDefined: typeof assertDefined;
|
|
20
|
+
declare namespace index {
|
|
21
|
+
export { index_assertDefined as assertDefined };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { assertDefined as a, index as i };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Asserts that a value is defined (not null or undefined).
|
|
3
|
+
* Throws an error if the value is null or undefined.
|
|
4
|
+
*
|
|
5
|
+
* @param value - The value to assert
|
|
6
|
+
* @param message - Optional error message
|
|
7
|
+
* @throws Error if value is null or undefined
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* const user = getUser(); // User | null
|
|
12
|
+
* assertDefined(user, 'User must exist');
|
|
13
|
+
* // user is now narrowed to User
|
|
14
|
+
* console.log(user.name);
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
declare function assertDefined<T>(value: T | null | undefined, message?: string): asserts value is T;
|
|
18
|
+
|
|
19
|
+
declare const index_assertDefined: typeof assertDefined;
|
|
20
|
+
declare namespace index {
|
|
21
|
+
export { index_assertDefined as assertDefined };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { assertDefined as a, index as i };
|
package/dist/index.d.mts
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __export = (target, all) => {
|
|
5
|
+
for (var name in all)
|
|
6
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
// src/guards/isValueDefined.ts
|
|
10
|
+
function isValueDefined(value) {
|
|
11
|
+
return value !== void 0 && value !== null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// src/guards/isNotEmptyArray.ts
|
|
15
|
+
function isNotEmptyArray(value) {
|
|
16
|
+
return Array.isArray(value) && value.length > 0;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// src/guards/isNotEmptyString.ts
|
|
20
|
+
function isNotEmptyString(value) {
|
|
21
|
+
return typeof value === "string" && value.trim().length > 0;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// src/guards/isNullOrUndefined.ts
|
|
25
|
+
function isNullOrUndefined(value) {
|
|
26
|
+
return value === void 0 || value === null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// src/guards/isEmptyArray.ts
|
|
30
|
+
function isEmptyArray(value) {
|
|
31
|
+
return Array.isArray(value) && value.length === 0;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// src/guards/isEmptyString.ts
|
|
35
|
+
function isEmptyString(value) {
|
|
36
|
+
return typeof value === "string" && value.trim().length === 0;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// src/assertions/assertDefined.ts
|
|
40
|
+
function assertDefined(value, message) {
|
|
41
|
+
if (value === void 0 || value === null) {
|
|
42
|
+
throw new Error(message ?? "Value must be defined");
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// src/guards/index.ts
|
|
47
|
+
var guards_exports = {};
|
|
48
|
+
__export(guards_exports, {
|
|
49
|
+
isEmptyArray: () => isEmptyArray,
|
|
50
|
+
isEmptyString: () => isEmptyString,
|
|
51
|
+
isNotEmptyArray: () => isNotEmptyArray,
|
|
52
|
+
isNotEmptyString: () => isNotEmptyString,
|
|
53
|
+
isNullOrUndefined: () => isNullOrUndefined,
|
|
54
|
+
isValueDefined: () => isValueDefined
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// src/assertions/index.ts
|
|
58
|
+
var assertions_exports = {};
|
|
59
|
+
__export(assertions_exports, {
|
|
60
|
+
assertDefined: () => assertDefined
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
exports.assertDefined = assertDefined;
|
|
64
|
+
exports.assertions = assertions_exports;
|
|
65
|
+
exports.guards = guards_exports;
|
|
66
|
+
exports.isEmptyArray = isEmptyArray;
|
|
67
|
+
exports.isEmptyString = isEmptyString;
|
|
68
|
+
exports.isNotEmptyArray = isNotEmptyArray;
|
|
69
|
+
exports.isNotEmptyString = isNotEmptyString;
|
|
70
|
+
exports.isNullOrUndefined = isNullOrUndefined;
|
|
71
|
+
exports.isValueDefined = isValueDefined;
|
|
72
|
+
//# sourceMappingURL=index.js.map
|
|
73
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/guards/isValueDefined.ts","../src/guards/isNotEmptyArray.ts","../src/guards/isNotEmptyString.ts","../src/guards/isNullOrUndefined.ts","../src/guards/isEmptyArray.ts","../src/guards/isEmptyString.ts","../src/assertions/assertDefined.ts","../src/guards/index.ts","../src/assertions/index.ts"],"names":[],"mappings":";;;;;;;;;AAeO,SAAS,eAAkB,KAAA,EAAyC;AACzE,EAAA,OAAO,KAAA,KAAU,UAAa,KAAA,KAAU,IAAA;AAC1C;;;ACFO,SAAS,gBACd,KAAA,EACqD;AACrD,EAAA,OAAO,KAAA,CAAM,OAAA,CAAQ,KAAK,CAAA,IAAK,MAAM,MAAA,GAAS,CAAA;AAChD;;;ACLO,SAAS,iBAAiB,KAAA,EAAmD;AAClF,EAAA,OAAO,OAAO,KAAA,KAAU,QAAA,IAAY,KAAA,CAAM,IAAA,GAAO,MAAA,GAAS,CAAA;AAC5D;;;ACDO,SAAS,kBAAkB,KAAA,EAA2C;AAC3E,EAAA,OAAO,KAAA,KAAU,UAAa,KAAA,KAAU,IAAA;AAC1C;;;ACHO,SAAS,aACd,KAAA,EACgD;AAChD,EAAA,OAAO,KAAA,CAAM,OAAA,CAAQ,KAAK,CAAA,IAAK,MAAM,MAAA,KAAW,CAAA;AAClD;;;ACHO,SAAS,cAAc,KAAA,EAA2C;AACvE,EAAA,OAAO,OAAO,KAAA,KAAU,QAAA,IAAY,KAAA,CAAM,IAAA,GAAO,MAAA,KAAW,CAAA;AAC9D;;;ACDO,SAAS,aAAA,CACd,OACA,OAAA,EACoB;AACpB,EAAA,IAAI,KAAA,KAAU,MAAA,IAAa,KAAA,KAAU,IAAA,EAAM;AACzC,IAAA,MAAM,IAAI,KAAA,CAAM,OAAA,IAAW,uBAAuB,CAAA;AAAA,EACpD;AACF;;;ACvBA,IAAA,cAAA,GAAA;AAAA,QAAA,CAAA,cAAA,EAAA;AAAA,EAAA,YAAA,EAAA,MAAA,YAAA;AAAA,EAAA,aAAA,EAAA,MAAA,aAAA;AAAA,EAAA,eAAA,EAAA,MAAA,eAAA;AAAA,EAAA,gBAAA,EAAA,MAAA,gBAAA;AAAA,EAAA,iBAAA,EAAA,MAAA,iBAAA;AAAA,EAAA,cAAA,EAAA,MAAA;AAAA,CAAA,CAAA;;;ACAA,IAAA,kBAAA,GAAA;AAAA,QAAA,CAAA,kBAAA,EAAA;AAAA,EAAA,aAAA,EAAA,MAAA;AAAA,CAAA,CAAA","file":"index.js","sourcesContent":["/**\r\n * Type guard that checks if a value is neither null nor undefined.\r\n *\r\n * @param value - The value to check\r\n * @returns True if the value is defined (not null and not undefined)\r\n *\r\n * @example\r\n * ```typescript\r\n * const value: string | null | undefined = getValue();\r\n * if (isValueDefined(value)) {\r\n * // value is narrowed to string\r\n * console.log(value.length);\r\n * }\r\n * ```\r\n */\r\nexport function isValueDefined<T>(value: T | null | undefined): value is T {\r\n return value !== undefined && value !== null;\r\n}\r\n","/**\r\n * Type guard that checks if a value is a non-empty array.\r\n *\r\n * @param value - The value to check\r\n * @returns True if the value is an array with at least one element\r\n *\r\n * @example\r\n * ```typescript\r\n * const items: string[] | undefined = getItems();\r\n * if (isNotEmptyArray(items)) {\r\n * // items is narrowed to string[] with at least one element\r\n * console.log(items[0]); // Safe to access\r\n * }\r\n * ```\r\n */\r\nexport function isNotEmptyArray<T>(\r\n value: readonly T[] | null | undefined\r\n): value is readonly T[] & { readonly length: number } {\r\n return Array.isArray(value) && value.length > 0;\r\n}\r\n","/**\r\n * Checks if a value is a non-empty string (after trimming whitespace).\r\n *\r\n * @param value - The value to check\r\n * @returns True if the value is a string with non-whitespace content\r\n *\r\n * @example\r\n * ```typescript\r\n * isNotEmptyString('hello'); // true\r\n * isNotEmptyString(' '); // false\r\n * isNotEmptyString(''); // false\r\n * isNotEmptyString(undefined); // false\r\n * ```\r\n */\r\nexport function isNotEmptyString(value: string | null | undefined): value is string {\r\n return typeof value === 'string' && value.trim().length > 0;\r\n}\r\n","/**\r\n * Type guard that checks if a value is null or undefined.\r\n *\r\n * @param value - The value to check\r\n * @returns True if the value is null or undefined\r\n *\r\n * @example\r\n * ```typescript\r\n * const value: string | null | undefined = getValue();\r\n * if (isNullOrUndefined(value)) {\r\n * // value is narrowed to null | undefined\r\n * console.log('Value is not set');\r\n * }\r\n * ```\r\n */\r\nexport function isNullOrUndefined(value: unknown): value is null | undefined {\r\n return value === undefined || value === null;\r\n}\r\n","/**\r\n * Type guard that checks if a value is an empty array.\r\n *\r\n * @param value - The value to check\r\n * @returns True if the value is an array with zero elements\r\n *\r\n * @example\r\n * ```typescript\r\n * const items: string[] = getItems();\r\n * if (isEmptyArray(items)) {\r\n * console.log('No items found');\r\n * }\r\n * ```\r\n */\r\nexport function isEmptyArray<T>(\r\n value: readonly T[] | null | undefined\r\n): value is readonly T[] & { readonly length: 0 } {\r\n return Array.isArray(value) && value.length === 0;\r\n}\r\n","/**\r\n * Checks if a value is an empty string or contains only whitespace.\r\n *\r\n * @param value - The value to check\r\n * @returns True if the value is an empty string or whitespace-only\r\n *\r\n * @example\r\n * ```typescript\r\n * isEmptyString(''); // true\r\n * isEmptyString(' '); // true\r\n * isEmptyString('hello'); // false\r\n * isEmptyString(undefined); // false (not a string)\r\n * isEmptyString(null); // false (not a string)\r\n * ```\r\n */\r\nexport function isEmptyString(value: string | null | undefined): boolean {\r\n return typeof value === 'string' && value.trim().length === 0;\r\n}\r\n","/**\r\n * Asserts that a value is defined (not null or undefined).\r\n * Throws an error if the value is null or undefined.\r\n *\r\n * @param value - The value to assert\r\n * @param message - Optional error message\r\n * @throws Error if value is null or undefined\r\n *\r\n * @example\r\n * ```typescript\r\n * const user = getUser(); // User | null\r\n * assertDefined(user, 'User must exist');\r\n * // user is now narrowed to User\r\n * console.log(user.name);\r\n * ```\r\n */\r\nexport function assertDefined<T>(\r\n value: T | null | undefined,\r\n message?: string\r\n): asserts value is T {\r\n if (value === undefined || value === null) {\r\n throw new Error(message ?? 'Value must be defined');\r\n }\r\n}\r\n","export { isValueDefined } from './isValueDefined';\r\nexport { isNotEmptyArray } from './isNotEmptyArray';\r\nexport { isNotEmptyString } from './isNotEmptyString';\r\nexport { isNullOrUndefined } from './isNullOrUndefined';\r\nexport { isEmptyArray } from './isEmptyArray';\r\nexport { isEmptyString } from './isEmptyString';\r\n","export { assertDefined } from './assertDefined';\r\n"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __export = (target, all) => {
|
|
3
|
+
for (var name in all)
|
|
4
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
// src/guards/isValueDefined.ts
|
|
8
|
+
function isValueDefined(value) {
|
|
9
|
+
return value !== void 0 && value !== null;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// src/guards/isNotEmptyArray.ts
|
|
13
|
+
function isNotEmptyArray(value) {
|
|
14
|
+
return Array.isArray(value) && value.length > 0;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// src/guards/isNotEmptyString.ts
|
|
18
|
+
function isNotEmptyString(value) {
|
|
19
|
+
return typeof value === "string" && value.trim().length > 0;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// src/guards/isNullOrUndefined.ts
|
|
23
|
+
function isNullOrUndefined(value) {
|
|
24
|
+
return value === void 0 || value === null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// src/guards/isEmptyArray.ts
|
|
28
|
+
function isEmptyArray(value) {
|
|
29
|
+
return Array.isArray(value) && value.length === 0;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// src/guards/isEmptyString.ts
|
|
33
|
+
function isEmptyString(value) {
|
|
34
|
+
return typeof value === "string" && value.trim().length === 0;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// src/assertions/assertDefined.ts
|
|
38
|
+
function assertDefined(value, message) {
|
|
39
|
+
if (value === void 0 || value === null) {
|
|
40
|
+
throw new Error(message ?? "Value must be defined");
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// src/guards/index.ts
|
|
45
|
+
var guards_exports = {};
|
|
46
|
+
__export(guards_exports, {
|
|
47
|
+
isEmptyArray: () => isEmptyArray,
|
|
48
|
+
isEmptyString: () => isEmptyString,
|
|
49
|
+
isNotEmptyArray: () => isNotEmptyArray,
|
|
50
|
+
isNotEmptyString: () => isNotEmptyString,
|
|
51
|
+
isNullOrUndefined: () => isNullOrUndefined,
|
|
52
|
+
isValueDefined: () => isValueDefined
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// src/assertions/index.ts
|
|
56
|
+
var assertions_exports = {};
|
|
57
|
+
__export(assertions_exports, {
|
|
58
|
+
assertDefined: () => assertDefined
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
export { assertDefined, assertions_exports as assertions, guards_exports as guards, isEmptyArray, isEmptyString, isNotEmptyArray, isNotEmptyString, isNullOrUndefined, isValueDefined };
|
|
62
|
+
//# sourceMappingURL=index.mjs.map
|
|
63
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/guards/isValueDefined.ts","../src/guards/isNotEmptyArray.ts","../src/guards/isNotEmptyString.ts","../src/guards/isNullOrUndefined.ts","../src/guards/isEmptyArray.ts","../src/guards/isEmptyString.ts","../src/assertions/assertDefined.ts","../src/guards/index.ts","../src/assertions/index.ts"],"names":[],"mappings":";;;;;;;AAeO,SAAS,eAAkB,KAAA,EAAyC;AACzE,EAAA,OAAO,KAAA,KAAU,UAAa,KAAA,KAAU,IAAA;AAC1C;;;ACFO,SAAS,gBACd,KAAA,EACqD;AACrD,EAAA,OAAO,KAAA,CAAM,OAAA,CAAQ,KAAK,CAAA,IAAK,MAAM,MAAA,GAAS,CAAA;AAChD;;;ACLO,SAAS,iBAAiB,KAAA,EAAmD;AAClF,EAAA,OAAO,OAAO,KAAA,KAAU,QAAA,IAAY,KAAA,CAAM,IAAA,GAAO,MAAA,GAAS,CAAA;AAC5D;;;ACDO,SAAS,kBAAkB,KAAA,EAA2C;AAC3E,EAAA,OAAO,KAAA,KAAU,UAAa,KAAA,KAAU,IAAA;AAC1C;;;ACHO,SAAS,aACd,KAAA,EACgD;AAChD,EAAA,OAAO,KAAA,CAAM,OAAA,CAAQ,KAAK,CAAA,IAAK,MAAM,MAAA,KAAW,CAAA;AAClD;;;ACHO,SAAS,cAAc,KAAA,EAA2C;AACvE,EAAA,OAAO,OAAO,KAAA,KAAU,QAAA,IAAY,KAAA,CAAM,IAAA,GAAO,MAAA,KAAW,CAAA;AAC9D;;;ACDO,SAAS,aAAA,CACd,OACA,OAAA,EACoB;AACpB,EAAA,IAAI,KAAA,KAAU,MAAA,IAAa,KAAA,KAAU,IAAA,EAAM;AACzC,IAAA,MAAM,IAAI,KAAA,CAAM,OAAA,IAAW,uBAAuB,CAAA;AAAA,EACpD;AACF;;;ACvBA,IAAA,cAAA,GAAA;AAAA,QAAA,CAAA,cAAA,EAAA;AAAA,EAAA,YAAA,EAAA,MAAA,YAAA;AAAA,EAAA,aAAA,EAAA,MAAA,aAAA;AAAA,EAAA,eAAA,EAAA,MAAA,eAAA;AAAA,EAAA,gBAAA,EAAA,MAAA,gBAAA;AAAA,EAAA,iBAAA,EAAA,MAAA,iBAAA;AAAA,EAAA,cAAA,EAAA,MAAA;AAAA,CAAA,CAAA;;;ACAA,IAAA,kBAAA,GAAA;AAAA,QAAA,CAAA,kBAAA,EAAA;AAAA,EAAA,aAAA,EAAA,MAAA;AAAA,CAAA,CAAA","file":"index.mjs","sourcesContent":["/**\r\n * Type guard that checks if a value is neither null nor undefined.\r\n *\r\n * @param value - The value to check\r\n * @returns True if the value is defined (not null and not undefined)\r\n *\r\n * @example\r\n * ```typescript\r\n * const value: string | null | undefined = getValue();\r\n * if (isValueDefined(value)) {\r\n * // value is narrowed to string\r\n * console.log(value.length);\r\n * }\r\n * ```\r\n */\r\nexport function isValueDefined<T>(value: T | null | undefined): value is T {\r\n return value !== undefined && value !== null;\r\n}\r\n","/**\r\n * Type guard that checks if a value is a non-empty array.\r\n *\r\n * @param value - The value to check\r\n * @returns True if the value is an array with at least one element\r\n *\r\n * @example\r\n * ```typescript\r\n * const items: string[] | undefined = getItems();\r\n * if (isNotEmptyArray(items)) {\r\n * // items is narrowed to string[] with at least one element\r\n * console.log(items[0]); // Safe to access\r\n * }\r\n * ```\r\n */\r\nexport function isNotEmptyArray<T>(\r\n value: readonly T[] | null | undefined\r\n): value is readonly T[] & { readonly length: number } {\r\n return Array.isArray(value) && value.length > 0;\r\n}\r\n","/**\r\n * Checks if a value is a non-empty string (after trimming whitespace).\r\n *\r\n * @param value - The value to check\r\n * @returns True if the value is a string with non-whitespace content\r\n *\r\n * @example\r\n * ```typescript\r\n * isNotEmptyString('hello'); // true\r\n * isNotEmptyString(' '); // false\r\n * isNotEmptyString(''); // false\r\n * isNotEmptyString(undefined); // false\r\n * ```\r\n */\r\nexport function isNotEmptyString(value: string | null | undefined): value is string {\r\n return typeof value === 'string' && value.trim().length > 0;\r\n}\r\n","/**\r\n * Type guard that checks if a value is null or undefined.\r\n *\r\n * @param value - The value to check\r\n * @returns True if the value is null or undefined\r\n *\r\n * @example\r\n * ```typescript\r\n * const value: string | null | undefined = getValue();\r\n * if (isNullOrUndefined(value)) {\r\n * // value is narrowed to null | undefined\r\n * console.log('Value is not set');\r\n * }\r\n * ```\r\n */\r\nexport function isNullOrUndefined(value: unknown): value is null | undefined {\r\n return value === undefined || value === null;\r\n}\r\n","/**\r\n * Type guard that checks if a value is an empty array.\r\n *\r\n * @param value - The value to check\r\n * @returns True if the value is an array with zero elements\r\n *\r\n * @example\r\n * ```typescript\r\n * const items: string[] = getItems();\r\n * if (isEmptyArray(items)) {\r\n * console.log('No items found');\r\n * }\r\n * ```\r\n */\r\nexport function isEmptyArray<T>(\r\n value: readonly T[] | null | undefined\r\n): value is readonly T[] & { readonly length: 0 } {\r\n return Array.isArray(value) && value.length === 0;\r\n}\r\n","/**\r\n * Checks if a value is an empty string or contains only whitespace.\r\n *\r\n * @param value - The value to check\r\n * @returns True if the value is an empty string or whitespace-only\r\n *\r\n * @example\r\n * ```typescript\r\n * isEmptyString(''); // true\r\n * isEmptyString(' '); // true\r\n * isEmptyString('hello'); // false\r\n * isEmptyString(undefined); // false (not a string)\r\n * isEmptyString(null); // false (not a string)\r\n * ```\r\n */\r\nexport function isEmptyString(value: string | null | undefined): boolean {\r\n return typeof value === 'string' && value.trim().length === 0;\r\n}\r\n","/**\r\n * Asserts that a value is defined (not null or undefined).\r\n * Throws an error if the value is null or undefined.\r\n *\r\n * @param value - The value to assert\r\n * @param message - Optional error message\r\n * @throws Error if value is null or undefined\r\n *\r\n * @example\r\n * ```typescript\r\n * const user = getUser(); // User | null\r\n * assertDefined(user, 'User must exist');\r\n * // user is now narrowed to User\r\n * console.log(user.name);\r\n * ```\r\n */\r\nexport function assertDefined<T>(\r\n value: T | null | undefined,\r\n message?: string\r\n): asserts value is T {\r\n if (value === undefined || value === null) {\r\n throw new Error(message ?? 'Value must be defined');\r\n }\r\n}\r\n","export { isValueDefined } from './isValueDefined';\r\nexport { isNotEmptyArray } from './isNotEmptyArray';\r\nexport { isNotEmptyString } from './isNotEmptyString';\r\nexport { isNullOrUndefined } from './isNullOrUndefined';\r\nexport { isEmptyArray } from './isEmptyArray';\r\nexport { isEmptyString } from './isEmptyString';\r\n","export { assertDefined } from './assertDefined';\r\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dloizides/utils",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Type-safe utility functions for TypeScript projects",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"typescript",
|
|
7
|
+
"utilities",
|
|
8
|
+
"type-guards",
|
|
9
|
+
"validation"
|
|
10
|
+
],
|
|
11
|
+
"author": "dloizides",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/openmindednewby/utils.git"
|
|
16
|
+
},
|
|
17
|
+
"homepage": "https://github.com/openmindednewby/utils#readme",
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/openmindednewby/utils/issues"
|
|
20
|
+
},
|
|
21
|
+
"main": "./dist/index.js",
|
|
22
|
+
"module": "./dist/index.mjs",
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"require": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"default": "./dist/index.js"
|
|
29
|
+
},
|
|
30
|
+
"import": {
|
|
31
|
+
"types": "./dist/index.d.mts",
|
|
32
|
+
"default": "./dist/index.mjs"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"./guards": {
|
|
36
|
+
"require": {
|
|
37
|
+
"types": "./dist/guards/index.d.ts",
|
|
38
|
+
"default": "./dist/guards/index.js"
|
|
39
|
+
},
|
|
40
|
+
"import": {
|
|
41
|
+
"types": "./dist/guards/index.d.mts",
|
|
42
|
+
"default": "./dist/guards/index.mjs"
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"./assertions": {
|
|
46
|
+
"require": {
|
|
47
|
+
"types": "./dist/assertions/index.d.ts",
|
|
48
|
+
"default": "./dist/assertions/index.js"
|
|
49
|
+
},
|
|
50
|
+
"import": {
|
|
51
|
+
"types": "./dist/assertions/index.d.mts",
|
|
52
|
+
"default": "./dist/assertions/index.mjs"
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
"files": [
|
|
57
|
+
"dist",
|
|
58
|
+
"README.md",
|
|
59
|
+
"CHANGELOG.md"
|
|
60
|
+
],
|
|
61
|
+
"sideEffects": false,
|
|
62
|
+
"engines": {
|
|
63
|
+
"node": ">=18.0.0"
|
|
64
|
+
},
|
|
65
|
+
"scripts": {
|
|
66
|
+
"build": "tsup",
|
|
67
|
+
"build:watch": "tsup --watch",
|
|
68
|
+
"test": "jest",
|
|
69
|
+
"test:watch": "jest --watch",
|
|
70
|
+
"test:coverage": "jest --coverage",
|
|
71
|
+
"lint": "eslint src --ext .ts",
|
|
72
|
+
"lint:fix": "eslint src --ext .ts --fix",
|
|
73
|
+
"typecheck": "tsc --noEmit",
|
|
74
|
+
"clean": "rimraf dist",
|
|
75
|
+
"prepublishOnly": "npm run clean && npm run build && npm run test",
|
|
76
|
+
"version": "npm run build",
|
|
77
|
+
"postversion": "git push && git push --tags"
|
|
78
|
+
},
|
|
79
|
+
"devDependencies": {
|
|
80
|
+
"@types/jest": "^29.5.0",
|
|
81
|
+
"@types/node": "^20.0.0",
|
|
82
|
+
"@typescript-eslint/eslint-plugin": "^7.0.0",
|
|
83
|
+
"@typescript-eslint/parser": "^7.0.0",
|
|
84
|
+
"eslint": "^8.57.0",
|
|
85
|
+
"jest": "^29.7.0",
|
|
86
|
+
"rimraf": "^5.0.0",
|
|
87
|
+
"ts-jest": "^29.1.0",
|
|
88
|
+
"tsup": "^8.0.0",
|
|
89
|
+
"typescript": "^5.4.0"
|
|
90
|
+
},
|
|
91
|
+
"peerDependencies": {},
|
|
92
|
+
"dependencies": {}
|
|
93
|
+
}
|