Skip to content

Commit a75d0db

Browse files
authored
feat: Export the checkOneOrMore and isOneOrMore utility functions. (#113)
* feat: Export the `checkOneOrMore` and `isOneOrMore` utility functions. * chore: update docs
1 parent 1a78545 commit a75d0db

8 files changed

Lines changed: 73 additions & 0 deletions

File tree

etc/types.api.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ export type Branded<T, BrandName extends string> = T extends WithBrands<infer Ba
110110
// @public
111111
export const brands: unique symbol;
112112

113+
// @public
114+
export function checkOneOrMore<T>(arr: T[]): OneOrMore<T>;
115+
113116
// @public
114117
export function createType<Impl extends BaseTypeImpl<any, any>>(impl: Impl, override?: Partial<Record<keyof BaseTypeImpl<any, any> | 'typeValidator' | 'typeParser' | 'customValidators', PropertyDescriptor>>): TypeImpl<Impl>;
115118

@@ -233,6 +236,9 @@ export class IntersectionType<Types extends OneOrMore<BaseObjectLikeTypeImpl<unk
233236
protected typeValidator(input: unknown, options: ValidationOptions): Result<IntersectionOfTypeTuple<Types>>;
234237
}
235238

239+
// @public
240+
export function isOneOrMore<T>(arr: T[]): arr is OneOrMore<T>;
241+
236242
// @public
237243
export function isType(value: unknown): value is Type<unknown>;
238244

markdown/types.checkoneormore.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [@skunkteam/types](./types.md) &gt; [checkOneOrMore](./types.checkoneormore.md)
4+
5+
## checkOneOrMore() function
6+
7+
Returns the original array if and only if it has at least one element.
8+
9+
**Signature:**
10+
11+
```typescript
12+
declare function checkOneOrMore<T>(arr: T[]): OneOrMore<T>;
13+
```
14+
15+
## Parameters
16+
17+
| Parameter | Type | Description |
18+
| --------- | ----- | ----------- |
19+
| arr | T\[\] | |
20+
21+
**Returns:**
22+
23+
[OneOrMore](./types.oneormore.md)<!-- -->&lt;T&gt;

markdown/types.isoneormore.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [@skunkteam/types](./types.md) &gt; [isOneOrMore](./types.isoneormore.md)
4+
5+
## isOneOrMore() function
6+
7+
Type guard for `OneOrMore`
8+
9+
**Signature:**
10+
11+
```typescript
12+
declare function isOneOrMore<T>(arr: T[]): arr is OneOrMore<T>;
13+
```
14+
15+
## Parameters
16+
17+
| Parameter | Type | Description |
18+
| --------- | ----- | ----------- |
19+
| arr | T\[\] | |
20+
21+
**Returns:**
22+
23+
arr is [OneOrMore](./types.oneormore.md)<!-- -->&lt;T&gt;
24+
25+
## Remarks
26+
27+
This checks if the array has at least one element.

markdown/types.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ Runtime type-validation with derived TypeScript types.
3535
| [autoCast(type)](./types.autocast.md) | Returns the same type, but with an auto-casting default parser installed. |
3636
| [autoCastAll(type)](./types.autocastall.md) | Create a recursive autocasting version of the given type. |
3737
| [booleanAutoCaster(input)](./types.booleanautocaster.md) | |
38+
| [checkOneOrMore(arr)](./types.checkoneormore.md) | Returns the original array if and only if it has at least one element. |
3839
| [createType(impl, override)](./types.createtype.md) | Create a Type from the given type-implementation. |
3940
| [intersection(args)](./types.intersection.md) | Intersect the given types. |
41+
| [isOneOrMore(arr)](./types.isoneormore.md) | Type guard for <code>OneOrMore</code> |
4042
| [isType(value)](./types.istype.md) | Type-guard that asserts that a given value is a Type. |
4143
| [keyof(args)](./types.keyof.md) | |
4244
| [literal(value)](./types.literal.md) | |

markdown/types.oneormore.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ An Array with at least one element.
1111
```typescript
1212
type OneOrMore<T> = [T, ...T[]];
1313
```
14+
15+
## Remarks
16+
17+
Note that this type does not disable the mutable methods of the array, which may still invalidate the non-emptiness of the array.

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ export * from './symbols';
77
export * from './type-guard';
88
export * from './types';
99
export { printKey, printPath, printValue } from './utils/print-utils';
10+
export { checkOneOrMore, isOneOrMore } from './utils/type-utils';
1011
export * from './validation-error';

src/interfaces.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,8 @@ export type TypeguardResult<ResultType, Input> =
362362

363363
/**
364364
* An Array with at least one element.
365+
* @remarks
366+
* Note that this type does not disable the mutable methods of the array, which may still invalidate the non-emptiness of the array.
365367
*/
366368
export type OneOrMore<T> = [T, ...T[]];
367369

src/utils/type-utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
import type { BasicType, Failure, OneOrMore, Result, ValidationResult } from '../interfaces';
22

3+
/**
4+
* Type guard for `OneOrMore`
5+
* @remarks
6+
* This checks if the array has at least one element.
7+
*/
38
export function isOneOrMore<T>(arr: T[]): arr is OneOrMore<T> {
49
return arr.length > 0;
510
}
611

12+
/**
13+
* Returns the original array if and only if it has at least one element.
14+
*/
715
export function checkOneOrMore<T>(arr: T[]): OneOrMore<T> {
816
// istanbul ignore if
917
if (!isOneOrMore(arr)) {

0 commit comments

Comments
 (0)