@augment-vir/core 30.0.0 → 30.0.1
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 +7 -0
- package/dist/augments/array/array.d.ts +25 -1
- package/dist/augments/array/tuple.d.ts +48 -6
- package/dist/augments/enum/enum-type.d.ts +7 -0
- package/dist/augments/enum/enum-values.d.ts +7 -0
- package/dist/augments/enum/enum-values.js +7 -0
- package/dist/augments/error/ensure-error.d.ts +15 -0
- package/dist/augments/error/ensure-error.js +15 -0
- package/dist/augments/error/error-message.d.ts +8 -0
- package/dist/augments/error/error-message.js +15 -0
- package/dist/augments/function/generic-function-type.d.ts +16 -2
- package/dist/augments/function/typed-function-type.d.ts +4 -0
- package/dist/augments/http/http-status.d.ts +26 -0
- package/dist/augments/http/http-status.js +19 -0
- package/dist/augments/narrow-type.d.ts +20 -0
- package/dist/augments/object/generic-object-type.d.ts +14 -0
- package/dist/augments/object/object-keys.d.ts +43 -0
- package/dist/augments/object/object-keys.js +9 -0
- package/dist/augments/object/object-value-types.d.ts +14 -0
- package/dist/augments/object/required-keys.d.ts +22 -1
- package/dist/augments/object/stringify.d.ts +9 -0
- package/dist/augments/object/stringify.js +9 -0
- package/dist/augments/overwrite-type.d.ts +7 -1
- package/dist/augments/partial-type.d.ts +16 -0
- package/dist/augments/promise/deferred-promise.d.ts +28 -1
- package/dist/augments/promise/deferred-promise.js +27 -0
- package/dist/augments/promise/maybe-promise.d.ts +7 -0
- package/dist/augments/promise/wait.d.ts +18 -0
- package/dist/augments/promise/wait.js +18 -0
- package/dist/augments/runtime-env.d.ts +41 -3
- package/dist/augments/runtime-env.js +41 -3
- package/dist/augments/string/ansi.d.ts +17 -0
- package/dist/augments/string/ansi.js +17 -0
- package/dist/augments/string/punctuation.d.ts +37 -0
- package/dist/augments/string/punctuation.js +30 -0
- package/dist/augments/string/uuid.d.ts +17 -2
- package/dist/augments/string/uuid.js +9 -1
- package/package.json +2 -10
package/README.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# @augment-vir/core
|
|
2
|
+
|
|
3
|
+
A collection of helpers that is used within multiple `@augment-vir/*` packages.
|
|
4
|
+
|
|
5
|
+
This should be imported through [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common) instead of directly installing this package.
|
|
6
|
+
|
|
7
|
+
See all `@augment-vir` docs here: https://electrovir.github.io/augment-vir
|
|
@@ -1,4 +1,28 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* Extract the type of the elements in an array.
|
|
3
|
+
*
|
|
4
|
+
* @category Array
|
|
5
|
+
* @category Package : @augment-vir/common
|
|
6
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
7
|
+
*/
|
|
2
8
|
export type ArrayElement<ArrayType extends ReadonlyArray<any>> = ArrayType[number];
|
|
9
|
+
/**
|
|
10
|
+
* Either an array of `T` or just `T` itself.
|
|
11
|
+
*
|
|
12
|
+
* @category Array
|
|
13
|
+
* @category Package : @augment-vir/common
|
|
14
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
15
|
+
* @see
|
|
16
|
+
* - {@link MaybeReadonlyArray}: the readonly array version.
|
|
17
|
+
*/
|
|
3
18
|
export type MaybeArray<T> = T | T[];
|
|
19
|
+
/**
|
|
20
|
+
* Either a readonly array of `T` or just `T` itself.
|
|
21
|
+
*
|
|
22
|
+
* @category Array
|
|
23
|
+
* @category Package : @augment-vir/common
|
|
24
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
25
|
+
* @see
|
|
26
|
+
* - {@link MaybeArray}: the non-readonly array version.
|
|
27
|
+
*/
|
|
4
28
|
export type MaybeReadonlyArray<T> = T | ReadonlyArray<T>;
|
|
@@ -1,13 +1,55 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Creates a tuple with length of `OriginalTuple` with values of `NewValueType`.
|
|
3
|
+
*
|
|
4
|
+
* @category Array
|
|
5
|
+
* @category Package : @augment-vir/common
|
|
6
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
7
|
+
*/
|
|
8
|
+
export type MappedTuple<OriginalTuple extends ReadonlyArray<any>, NewValueType> = {
|
|
9
|
+
[I in keyof OriginalTuple]: NewValueType;
|
|
3
10
|
};
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Creates a tuple that is _at least_ of length `Length`.
|
|
13
|
+
*
|
|
14
|
+
* @category Array
|
|
15
|
+
* @category Package : @augment-vir/common
|
|
16
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
17
|
+
*/
|
|
18
|
+
export type AtLeastTuple<Element, Length extends number> = readonly [
|
|
19
|
+
...Tuple<Element, Length>,
|
|
20
|
+
...Element[]
|
|
7
21
|
];
|
|
22
|
+
/**
|
|
23
|
+
* Either a tuple of `T` with length at least 1 or just `T` itself.
|
|
24
|
+
*
|
|
25
|
+
* @category Array
|
|
26
|
+
* @category Package : @augment-vir/common
|
|
27
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
28
|
+
*/
|
|
8
29
|
export type MaybeTuple<T> = T | AtLeastTuple<T, 1>;
|
|
30
|
+
/**
|
|
31
|
+
* Remove the last entry in a tuple.
|
|
32
|
+
*
|
|
33
|
+
* @category Array
|
|
34
|
+
* @category Package : @augment-vir/common
|
|
35
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
36
|
+
*/
|
|
9
37
|
export type RemoveLastTupleEntry<T extends any[]> = T extends [...infer Head, any?] ? Head : any[];
|
|
38
|
+
/**
|
|
39
|
+
* Remove the first entry in a tuple.
|
|
40
|
+
*
|
|
41
|
+
* @category Array
|
|
42
|
+
* @category Package : @augment-vir/common
|
|
43
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
44
|
+
*/
|
|
10
45
|
export type RemoveFirstTupleEntry<T extends any[]> = T extends [any?, ...infer Tail] ? Tail : any[];
|
|
11
|
-
|
|
46
|
+
/**
|
|
47
|
+
* A tuple with entries of type `Element` and length of `Length`.
|
|
48
|
+
*
|
|
49
|
+
* @category Array
|
|
50
|
+
* @category Package : @augment-vir/common
|
|
51
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
52
|
+
*/
|
|
53
|
+
export type Tuple<Element, Length extends number> = Length extends Length ? number extends Length ? Element[] : _TupleOf<Element, Length, []> : never;
|
|
12
54
|
type _TupleOf<ArrayElementGeneric, LengthGeneric extends number, FullArrayGeneric extends unknown[]> = FullArrayGeneric['length'] extends LengthGeneric ? FullArrayGeneric : _TupleOf<ArrayElementGeneric, LengthGeneric, [ArrayElementGeneric, ...FullArrayGeneric]>;
|
|
13
55
|
export {};
|
|
@@ -1 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A base enum type, useful for enum type parameter baselines.
|
|
3
|
+
*
|
|
4
|
+
* @category Enum
|
|
5
|
+
* @category Package : @augment-vir/common
|
|
6
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
7
|
+
*/
|
|
1
8
|
export type EnumBaseType = Record<string, number | string>;
|
|
@@ -1,2 +1,9 @@
|
|
|
1
1
|
import type { EnumBaseType } from './enum-type.js';
|
|
2
|
+
/**
|
|
3
|
+
* Gets all values within an enum as an array.
|
|
4
|
+
*
|
|
5
|
+
* @category Enum
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
8
|
+
*/
|
|
2
9
|
export declare function getEnumValues<T extends EnumBaseType>(input: T): T[keyof T][];
|
|
@@ -4,6 +4,13 @@ function getEnumKeys(input) {
|
|
|
4
4
|
// enum keys are always strings
|
|
5
5
|
return getObjectTypedKeys(input).filter((key) => isNaN(Number(key)));
|
|
6
6
|
}
|
|
7
|
+
/**
|
|
8
|
+
* Gets all values within an enum as an array.
|
|
9
|
+
*
|
|
10
|
+
* @category Enum
|
|
11
|
+
* @category Package : @augment-vir/common
|
|
12
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
13
|
+
*/
|
|
7
14
|
export function getEnumValues(input) {
|
|
8
15
|
const keys = getEnumKeys(input);
|
|
9
16
|
return keys.map((key) => input[key]);
|
|
@@ -1,2 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Either returns the input if it's already an Error instance or converts it into an Error instance.
|
|
3
|
+
*
|
|
4
|
+
* @category Error
|
|
5
|
+
* @category Package : @augment-vir/common
|
|
6
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
7
|
+
*/
|
|
1
8
|
export declare function ensureError(maybeError: unknown): Error;
|
|
9
|
+
/**
|
|
10
|
+
* Ensures that the given input is an error and prepends the given message to the ensured Error
|
|
11
|
+
* instance's message.
|
|
12
|
+
*
|
|
13
|
+
* @category Error
|
|
14
|
+
* @category Package : @augment-vir/common
|
|
15
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
16
|
+
*/
|
|
2
17
|
export declare function ensureErrorAndPrependMessage(maybeError: unknown, prependMessage: string): Error;
|
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import { combineErrorMessages, extractErrorMessage } from './error-message.js';
|
|
2
|
+
/**
|
|
3
|
+
* Either returns the input if it's already an Error instance or converts it into an Error instance.
|
|
4
|
+
*
|
|
5
|
+
* @category Error
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
8
|
+
*/
|
|
2
9
|
export function ensureError(maybeError) {
|
|
3
10
|
if (maybeError instanceof Error) {
|
|
4
11
|
return maybeError;
|
|
@@ -7,6 +14,14 @@ export function ensureError(maybeError) {
|
|
|
7
14
|
return new Error(extractErrorMessage(maybeError));
|
|
8
15
|
}
|
|
9
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Ensures that the given input is an error and prepends the given message to the ensured Error
|
|
19
|
+
* instance's message.
|
|
20
|
+
*
|
|
21
|
+
* @category Error
|
|
22
|
+
* @category Package : @augment-vir/common
|
|
23
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
24
|
+
*/
|
|
10
25
|
export function ensureErrorAndPrependMessage(maybeError, prependMessage) {
|
|
11
26
|
const error = ensureError(maybeError);
|
|
12
27
|
error.message = combineErrorMessages(prependMessage, error.message);
|
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tries its hardest to extract an error message from the input, which may be anything (not even an
|
|
3
|
+
* Error instance).
|
|
4
|
+
*
|
|
5
|
+
* @category Error
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
8
|
+
*/
|
|
1
9
|
export declare function extractErrorMessage(maybeError: unknown): string;
|
|
2
10
|
export declare function combineErrorMessages(...messages: ReadonlyArray<string | undefined>): string;
|
|
3
11
|
export declare function combineErrorMessages(messages: ReadonlyArray<string | undefined>): string;
|
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import { stringify } from '../object/stringify.js';
|
|
2
2
|
import { removeEndingPunctuation } from '../string/punctuation.js';
|
|
3
|
+
/**
|
|
4
|
+
* Tries its hardest to extract an error message from the input, which may be anything (not even an
|
|
5
|
+
* Error instance).
|
|
6
|
+
*
|
|
7
|
+
* @category Error
|
|
8
|
+
* @category Package : @augment-vir/common
|
|
9
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
10
|
+
*/
|
|
3
11
|
export function extractErrorMessage(maybeError) {
|
|
4
12
|
if (!maybeError) {
|
|
5
13
|
return '';
|
|
@@ -17,6 +25,13 @@ export function extractErrorMessage(maybeError) {
|
|
|
17
25
|
return stringify(maybeError);
|
|
18
26
|
}
|
|
19
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Combines multiple error messages into a single error message.
|
|
30
|
+
*
|
|
31
|
+
* @category Error
|
|
32
|
+
* @category Package : @augment-vir/common
|
|
33
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
34
|
+
*/
|
|
20
35
|
export function combineErrorMessages(...rawMessages) {
|
|
21
36
|
const messages = (Array.isArray(rawMessages[0]) ? rawMessages[0] : rawMessages).filter((message) => {
|
|
22
37
|
return message && removeEndingPunctuation(message);
|
|
@@ -1,2 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* A Function with no inputs and a return type of `Return` (which defaults to `any`).
|
|
3
|
+
*
|
|
4
|
+
* @category Function
|
|
5
|
+
* @category Package : @augment-vir/common
|
|
6
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
7
|
+
*/
|
|
8
|
+
export type NoInputsFunction<Return = any> = () => Return;
|
|
9
|
+
/**
|
|
10
|
+
* A Function with any inputs and a return type of `Return` (which defaults to `any`).
|
|
11
|
+
*
|
|
12
|
+
* @category Function
|
|
13
|
+
* @category Package : @augment-vir/common
|
|
14
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
15
|
+
*/
|
|
16
|
+
export type AnyFunction<Return = any> = (...args: any[]) => Return;
|
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
* instead of a rest parameter, put it inside of a tuple. If no arguments should be possible, pass
|
|
5
5
|
* void to "Arguments". If you need an optional argument, pass it inside of a tuple.
|
|
6
6
|
*
|
|
7
|
+
* @category Function
|
|
8
|
+
* @category Package : @augment-vir/common
|
|
7
9
|
* @example
|
|
8
10
|
*
|
|
9
11
|
* ```ts
|
|
@@ -13,5 +15,7 @@
|
|
|
13
15
|
* TypedFunction<[string, number], number>; // (input1: string, input2: number) => number
|
|
14
16
|
* TypedFunction<[string | undefined], number>; // (input1: string|undefined) => number
|
|
15
17
|
* ```
|
|
18
|
+
*
|
|
19
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
16
20
|
*/
|
|
17
21
|
export type TypedFunction<Arguments, Return> = Arguments extends readonly any[] ? number extends Arguments['length'] ? (...args: Arguments[number][]) => Return : (...args: Arguments) => Return : void extends Arguments ? () => Return : (arg: Arguments) => Return;
|
|
@@ -4,6 +4,10 @@ import type { ArrayElement } from '../array/array.js';
|
|
|
4
4
|
*
|
|
5
5
|
* These values are automatically parsed from https://developer.mozilla.org/docs/Web/HTTP/Status via
|
|
6
6
|
* https://github.com/electrovir/augment-vir/blob/dev/packages/scripts/src/scripts/generate-http-status.script.ts
|
|
7
|
+
*
|
|
8
|
+
* @category HTTP
|
|
9
|
+
* @category Package : @augment-vir/common
|
|
10
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
7
11
|
*/
|
|
8
12
|
export declare enum HttpStatus {
|
|
9
13
|
/** 100 level codes (information) */
|
|
@@ -479,6 +483,14 @@ export declare enum HttpStatus {
|
|
|
479
483
|
*/
|
|
480
484
|
NetworkAuthenticationRequired = 511
|
|
481
485
|
}
|
|
486
|
+
/**
|
|
487
|
+
* All standardized HTTP status code categories. These are determined by the first number in the
|
|
488
|
+
* HTTP status code.
|
|
489
|
+
*
|
|
490
|
+
* @category HTTP
|
|
491
|
+
* @category Package : @augment-vir/common
|
|
492
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
493
|
+
*/
|
|
482
494
|
export declare enum HttpStatusCategory {
|
|
483
495
|
Information = "information",
|
|
484
496
|
Success = "success",
|
|
@@ -486,6 +498,13 @@ export declare enum HttpStatusCategory {
|
|
|
486
498
|
ClientError = "clientError",
|
|
487
499
|
ServerError = "serverError"
|
|
488
500
|
}
|
|
501
|
+
/**
|
|
502
|
+
* All standardized HTTP status codes grouped into their respective categories.
|
|
503
|
+
*
|
|
504
|
+
* @category HTTP
|
|
505
|
+
* @category Package : @augment-vir/common
|
|
506
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
507
|
+
*/
|
|
489
508
|
export declare const httpStatusByCategory: {
|
|
490
509
|
readonly information: readonly [HttpStatus.Continue, HttpStatus.SwitchingProtocols, HttpStatus.Processing, HttpStatus.EarlyHints];
|
|
491
510
|
readonly success: readonly [HttpStatus.Ok, HttpStatus.Created, HttpStatus.Accepted, HttpStatus.NonAuthoritativeInformation, HttpStatus.NoContent, HttpStatus.ResetContent, HttpStatus.PartialContent, HttpStatus.MultiStatus, HttpStatus.AlreadyReported, HttpStatus.ImUsed];
|
|
@@ -493,4 +512,11 @@ export declare const httpStatusByCategory: {
|
|
|
493
512
|
readonly clientError: readonly [HttpStatus.BadRequest, HttpStatus.Unauthorized, HttpStatus.PaymentRequired, HttpStatus.Forbidden, HttpStatus.NotFound, HttpStatus.MethodNotAllowed, HttpStatus.NotAcceptable, HttpStatus.ProxyAuthenticationRequired, HttpStatus.RequestTimeout, HttpStatus.Conflict, HttpStatus.Gone, HttpStatus.LengthRequired, HttpStatus.PreconditionFailed, HttpStatus.PayloadTooLarge, HttpStatus.UriTooLong, HttpStatus.UnsupportedMediaType, HttpStatus.RangeNotSatisfiable, HttpStatus.ExpectationFailed, HttpStatus.ImATeapot, HttpStatus.MisdirectedRequest, HttpStatus.UnprocessableContent, HttpStatus.Locked, HttpStatus.FailedDependency, HttpStatus.TooEarly, HttpStatus.UpgradeRequired, HttpStatus.PreconditionRequired, HttpStatus.TooManyRequests, HttpStatus.RequestHeaderFieldsTooLarge, HttpStatus.UnavailableForLegalReasons];
|
|
494
513
|
readonly serverError: readonly [HttpStatus.InternalServerError, HttpStatus.NotImplemented, HttpStatus.BadGateway, HttpStatus.ServiceUnavailable, HttpStatus.GatewayTimeout, HttpStatus.HttpVersionNotSupported, HttpStatus.VariantAlsoNegotiates, HttpStatus.InsufficientStorage, HttpStatus.LoopDetected, HttpStatus.NotExtended, HttpStatus.NetworkAuthenticationRequired];
|
|
495
514
|
};
|
|
515
|
+
/**
|
|
516
|
+
* All possible HTTP status codes for the given {@link HttpStatusCategory}.
|
|
517
|
+
*
|
|
518
|
+
* @category HTTP
|
|
519
|
+
* @category Package : @augment-vir/common
|
|
520
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
521
|
+
*/
|
|
496
522
|
export type HttpStatusByCategory<Category extends HttpStatusCategory> = ArrayElement<(typeof httpStatusByCategory)[Category]>;
|
|
@@ -3,6 +3,10 @@
|
|
|
3
3
|
*
|
|
4
4
|
* These values are automatically parsed from https://developer.mozilla.org/docs/Web/HTTP/Status via
|
|
5
5
|
* https://github.com/electrovir/augment-vir/blob/dev/packages/scripts/src/scripts/generate-http-status.script.ts
|
|
6
|
+
*
|
|
7
|
+
* @category HTTP
|
|
8
|
+
* @category Package : @augment-vir/common
|
|
9
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
6
10
|
*/
|
|
7
11
|
export var HttpStatus;
|
|
8
12
|
(function (HttpStatus) {
|
|
@@ -479,6 +483,14 @@ export var HttpStatus;
|
|
|
479
483
|
*/
|
|
480
484
|
HttpStatus[HttpStatus["NetworkAuthenticationRequired"] = 511] = "NetworkAuthenticationRequired";
|
|
481
485
|
})(HttpStatus || (HttpStatus = {}));
|
|
486
|
+
/**
|
|
487
|
+
* All standardized HTTP status code categories. These are determined by the first number in the
|
|
488
|
+
* HTTP status code.
|
|
489
|
+
*
|
|
490
|
+
* @category HTTP
|
|
491
|
+
* @category Package : @augment-vir/common
|
|
492
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
493
|
+
*/
|
|
482
494
|
export var HttpStatusCategory;
|
|
483
495
|
(function (HttpStatusCategory) {
|
|
484
496
|
HttpStatusCategory["Information"] = "information";
|
|
@@ -487,6 +499,13 @@ export var HttpStatusCategory;
|
|
|
487
499
|
HttpStatusCategory["ClientError"] = "clientError";
|
|
488
500
|
HttpStatusCategory["ServerError"] = "serverError";
|
|
489
501
|
})(HttpStatusCategory || (HttpStatusCategory = {}));
|
|
502
|
+
/**
|
|
503
|
+
* All standardized HTTP status codes grouped into their respective categories.
|
|
504
|
+
*
|
|
505
|
+
* @category HTTP
|
|
506
|
+
* @category Package : @augment-vir/common
|
|
507
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
508
|
+
*/
|
|
490
509
|
export const httpStatusByCategory = {
|
|
491
510
|
[HttpStatusCategory.Information]: [
|
|
492
511
|
HttpStatus.Continue,
|
|
@@ -1,2 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Narrows the given `Actual` type to the given `Expected` type as much as possible, or falls back
|
|
3
|
+
* to just `Expected` itself.
|
|
4
|
+
*
|
|
5
|
+
* @category Type
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
8
|
+
* @see
|
|
9
|
+
* - {@link NarrowToActual}: narrowing in the other direction.
|
|
10
|
+
*/
|
|
1
11
|
export type NarrowToExpected<Actual, Expected> = Extract<Expected, Actual> extends never ? Extract<Actual, Expected> extends never ? Expected extends Actual ? Expected : never : Extract<Actual, Expected> : Extract<Expected, Actual>;
|
|
12
|
+
/**
|
|
13
|
+
* Narrows the given `Expected` type to the given `Actual` type as much as possible, or falls back
|
|
14
|
+
* to just `Expected` itself.
|
|
15
|
+
*
|
|
16
|
+
* @category Type
|
|
17
|
+
* @category Package : @augment-vir/common
|
|
18
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
19
|
+
* @see
|
|
20
|
+
* - {@link NarrowToExpected}: narrowing in the other direction.
|
|
21
|
+
*/
|
|
2
22
|
export type NarrowToActual<Actual, Expected> = Extract<Actual, Expected> extends never ? Extract<Expected, Actual> extends never ? Expected extends Actual ? Expected : never : Extract<Expected, Actual> : Extract<Actual, Expected>;
|
|
@@ -1,2 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Any object with any allowed key and `any` values.
|
|
3
|
+
*
|
|
4
|
+
* @category Object
|
|
5
|
+
* @category Package : @augment-vir/common
|
|
6
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
7
|
+
*/
|
|
1
8
|
export type AnyObject = Record<PropertyKey, any>;
|
|
9
|
+
/**
|
|
10
|
+
* Any object with any allowed key and `unknown` value.
|
|
11
|
+
*
|
|
12
|
+
* @category Object
|
|
13
|
+
* @category Package : @augment-vir/common
|
|
14
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
15
|
+
*/
|
|
2
16
|
export type UnknownObject = Record<PropertyKey, unknown>;
|
|
@@ -1,7 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gets all keys of an object. This is similar to
|
|
3
|
+
* [`Object.keys`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/keys)
|
|
4
|
+
* except that it also grabs symbol keys and has better TypeScript typing.
|
|
5
|
+
*
|
|
6
|
+
* @category Object
|
|
7
|
+
* @category Package : @augment-vir/common
|
|
8
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
9
|
+
*/
|
|
1
10
|
export declare function getObjectTypedKeys<const ObjectGeneric>(input: ObjectGeneric): Array<keyof ObjectGeneric>;
|
|
11
|
+
/**
|
|
12
|
+
* Performs `keyof` on all keys within the `OriginalObject` that have values matching the given
|
|
13
|
+
* `Matcher`.
|
|
14
|
+
*
|
|
15
|
+
* @category Object
|
|
16
|
+
* @category Package : @augment-vir/common
|
|
17
|
+
* @example
|
|
18
|
+
*
|
|
19
|
+
* ```ts
|
|
20
|
+
* import {ExtractKeysWithMatchingValues} from '@augment-vir/common';
|
|
21
|
+
*
|
|
22
|
+
* type ExtractedKeys = ExtractKeysWithMatchingValues<{a: RegExp; b: string}, string>;
|
|
23
|
+
* // `ExtractedKeys` is `'b'`
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
27
|
+
*/
|
|
2
28
|
export type ExtractKeysWithMatchingValues<OriginalObject extends object, Matcher> = keyof {
|
|
3
29
|
[Prop in keyof OriginalObject as OriginalObject[Prop] extends Matcher ? Prop : never]: Prop;
|
|
4
30
|
};
|
|
31
|
+
/**
|
|
32
|
+
* Performs `keyof` on all keys within the `OriginalObject` that have values _not_ matching the
|
|
33
|
+
* given `Matcher`.
|
|
34
|
+
*
|
|
35
|
+
* @category Object
|
|
36
|
+
* @category Package : @augment-vir/common
|
|
37
|
+
* @example
|
|
38
|
+
*
|
|
39
|
+
* ```ts
|
|
40
|
+
* import {ExcludeKeysWithMatchingValues} from '@augment-vir/common';
|
|
41
|
+
*
|
|
42
|
+
* type ExcludedKeys = ExcludeKeysWithMatchingValues<{a: RegExp; b: string}, string>;
|
|
43
|
+
* // `ExcludedKeys` is `'a'`
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
47
|
+
*/
|
|
5
48
|
export type ExcludeKeysWithMatchingValues<OriginalObject extends object, Matcher> = keyof {
|
|
6
49
|
[Prop in keyof OriginalObject as OriginalObject[Prop] extends Matcher ? never : Prop]: Prop;
|
|
7
50
|
};
|
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gets all keys of an object. This is similar to
|
|
3
|
+
* [`Object.keys`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/keys)
|
|
4
|
+
* except that it also grabs symbol keys and has better TypeScript typing.
|
|
5
|
+
*
|
|
6
|
+
* @category Object
|
|
7
|
+
* @category Package : @augment-vir/common
|
|
8
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
9
|
+
*/
|
|
1
10
|
export function getObjectTypedKeys(input) {
|
|
2
11
|
let reflectKeys;
|
|
3
12
|
try {
|
|
@@ -1,3 +1,17 @@
|
|
|
1
1
|
import type { CompleteRequire } from './required-keys.js';
|
|
2
|
+
/**
|
|
3
|
+
* Gets all values of an object.
|
|
4
|
+
*
|
|
5
|
+
* @category Object
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
8
|
+
*/
|
|
2
9
|
export type Values<T> = CompleteRequire<T>[keyof T];
|
|
10
|
+
/**
|
|
11
|
+
* Gets the value within an object when all its keys are required.
|
|
12
|
+
*
|
|
13
|
+
* @category Object
|
|
14
|
+
* @category Package : @augment-vir/common
|
|
15
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
16
|
+
*/
|
|
3
17
|
export type ValueAtRequiredKey<Parent, Key extends keyof Parent> = CompleteRequire<Parent>[Key];
|
|
@@ -3,6 +3,10 @@ export type { SetRequired } from 'type-fest';
|
|
|
3
3
|
/**
|
|
4
4
|
* Same as the Required<> built-in type helper but this requires that each property be present and
|
|
5
5
|
* be not null.
|
|
6
|
+
*
|
|
7
|
+
* @category Object
|
|
8
|
+
* @category Package : @augment-vir/common
|
|
9
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
6
10
|
*/
|
|
7
11
|
export type RequiredAndNotNull<T> = {
|
|
8
12
|
[P in keyof CompleteRequire<T>]-?: NonNullable<T[P]>;
|
|
@@ -10,16 +14,31 @@ export type RequiredAndNotNull<T> = {
|
|
|
10
14
|
/**
|
|
11
15
|
* Require only a subset of object properties and require that they be not null. This is
|
|
12
16
|
* particularly useful in conjunction with the "exactOptionalPropertyTypes" tsconfig flag.
|
|
17
|
+
*
|
|
18
|
+
* @category Object
|
|
19
|
+
* @category Package : @augment-vir/common
|
|
20
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
13
21
|
*/
|
|
14
22
|
export type SetRequiredAndNotNull<T, K extends keyof T> = Omit<T, K> & CompleteRequire<{
|
|
15
23
|
[PropertyName in K]: NonNullable<T[PropertyName]>;
|
|
16
24
|
}>;
|
|
25
|
+
/**
|
|
26
|
+
* Sets a key as optional but also nullable.
|
|
27
|
+
*
|
|
28
|
+
* @category Object
|
|
29
|
+
* @category Package : @augment-vir/common
|
|
30
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
31
|
+
*/
|
|
17
32
|
export type SetOptionalAndNullable<OriginalObjectGeneric, OptionalKeysGeneric extends keyof OriginalObjectGeneric> = Simplify<Except<OriginalObjectGeneric, OptionalKeysGeneric> & {
|
|
18
33
|
[PropKey in OptionalKeysGeneric]?: OriginalObjectGeneric[PropKey] | null | undefined;
|
|
19
34
|
}>;
|
|
20
35
|
/**
|
|
21
36
|
* Modified version of `RequiredKeys` from `type-fest` that does not require `BaseType` to extends
|
|
22
37
|
* `object`.
|
|
38
|
+
*
|
|
39
|
+
* @category Object
|
|
40
|
+
* @category Package : @augment-vir/common
|
|
41
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
23
42
|
*/
|
|
24
43
|
export type RequiredKeysOf<BaseType> = Exclude<{
|
|
25
44
|
[Key in keyof BaseType]: BaseType extends Record<Key, BaseType[Key]> ? Key : never;
|
|
@@ -29,7 +48,9 @@ export type RequiredKeysOf<BaseType> = Exclude<{
|
|
|
29
48
|
* `Required<Partial<T>>` doesn't fully remove `| undefined` from indexed keys when the
|
|
30
49
|
* `noUncheckedIndexedAccess` TSConfig compiler option is enabled.
|
|
31
50
|
*
|
|
32
|
-
* @category Object
|
|
51
|
+
* @category Object
|
|
52
|
+
* @category Package : @augment-vir/common
|
|
53
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
33
54
|
*/
|
|
34
55
|
export type CompleteRequire<Parent> = {
|
|
35
56
|
[Prop in keyof Parent]-?: Parent extends Partial<Record<Prop, infer V>> ? V : never;
|
|
@@ -1 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts the input into a string. Tries first with JSON5 and, if that fails, falls back to a
|
|
3
|
+
* regular `.toString()` conversion.
|
|
4
|
+
*
|
|
5
|
+
* @category Object
|
|
6
|
+
* @category String
|
|
7
|
+
* @category Package : @augment-vir/common
|
|
8
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
9
|
+
*/
|
|
1
10
|
export declare function stringify(input: unknown): string;
|
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
import JSON5 from 'json5';
|
|
2
|
+
/**
|
|
3
|
+
* Converts the input into a string. Tries first with JSON5 and, if that fails, falls back to a
|
|
4
|
+
* regular `.toString()` conversion.
|
|
5
|
+
*
|
|
6
|
+
* @category Object
|
|
7
|
+
* @category String
|
|
8
|
+
* @category Package : @augment-vir/common
|
|
9
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
10
|
+
*/
|
|
2
11
|
export function stringify(input) {
|
|
3
12
|
try {
|
|
4
13
|
return JSON5.stringify(input);
|
|
@@ -1,2 +1,8 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* Replace properties in T with properties in U.
|
|
3
|
+
*
|
|
4
|
+
* @category Type
|
|
5
|
+
* @category Package : @augment-vir/common
|
|
6
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
7
|
+
*/
|
|
2
8
|
export type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
|
|
@@ -1,7 +1,23 @@
|
|
|
1
1
|
import { AnyObject } from './object/generic-object-type.js';
|
|
2
|
+
/**
|
|
3
|
+
* Allow `T` to be partial or have `null` or `undefined` as the value for any of its keys.
|
|
4
|
+
*
|
|
5
|
+
* @category Type
|
|
6
|
+
* @category Object
|
|
7
|
+
* @category Package : @augment-vir/common
|
|
8
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
9
|
+
*/
|
|
2
10
|
export type PartialWithNullable<T extends AnyObject> = {
|
|
3
11
|
[Prop in keyof T]?: T[Prop] | null | undefined;
|
|
4
12
|
};
|
|
13
|
+
/**
|
|
14
|
+
* Allow `T` to be partial or have `undefined` as the value for any of its keys.
|
|
15
|
+
*
|
|
16
|
+
* @category Type
|
|
17
|
+
* @category Object
|
|
18
|
+
* @category Package : @augment-vir/common
|
|
19
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
20
|
+
*/
|
|
5
21
|
export type PartialWithUndefined<T extends AnyObject> = {
|
|
6
22
|
[Prop in keyof T]?: T[Prop] | undefined;
|
|
7
23
|
};
|
|
@@ -1,7 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a promise that can be resolved or rejected at any later time. It also includes indication
|
|
3
|
+
* on whether its been settled yet or not.
|
|
4
|
+
*
|
|
5
|
+
* @category Promise
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @example
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import {DeferredPromise} from '@augment-vir/common';
|
|
11
|
+
*
|
|
12
|
+
* function waitForInput() {
|
|
13
|
+
* const deferred = new DeferredPromise<string>();
|
|
14
|
+
*
|
|
15
|
+
* window.addEventListener('keydown', (event) => {
|
|
16
|
+
* deferred.resolve(event.code);
|
|
17
|
+
* });
|
|
18
|
+
* return deferred.promise;
|
|
19
|
+
* }
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
23
|
+
*/
|
|
1
24
|
export declare class DeferredPromise<T = void> {
|
|
25
|
+
/** The deferred promise which can be awaited. */
|
|
2
26
|
promise: Promise<T>;
|
|
27
|
+
/** Call this to resolve the deferred promise with the given value. */
|
|
3
28
|
resolve: (value: T | PromiseLike<T>) => void;
|
|
29
|
+
/** Call this to reject the deferred promise with the given reason. */
|
|
4
30
|
reject: (reason?: any) => void;
|
|
5
|
-
|
|
31
|
+
/** Indicates whether the promise has been settled (resolved or rejected) yet. */
|
|
32
|
+
readonly isSettled: boolean;
|
|
6
33
|
constructor();
|
|
7
34
|
}
|
|
@@ -1,8 +1,35 @@
|
|
|
1
1
|
import { ensureError } from '../error/ensure-error.js';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a promise that can be resolved or rejected at any later time. It also includes indication
|
|
4
|
+
* on whether its been settled yet or not.
|
|
5
|
+
*
|
|
6
|
+
* @category Promise
|
|
7
|
+
* @category Package : @augment-vir/common
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import {DeferredPromise} from '@augment-vir/common';
|
|
12
|
+
*
|
|
13
|
+
* function waitForInput() {
|
|
14
|
+
* const deferred = new DeferredPromise<string>();
|
|
15
|
+
*
|
|
16
|
+
* window.addEventListener('keydown', (event) => {
|
|
17
|
+
* deferred.resolve(event.code);
|
|
18
|
+
* });
|
|
19
|
+
* return deferred.promise;
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
24
|
+
*/
|
|
2
25
|
export class DeferredPromise {
|
|
26
|
+
/** The deferred promise which can be awaited. */
|
|
3
27
|
promise;
|
|
28
|
+
/** Call this to resolve the deferred promise with the given value. */
|
|
4
29
|
resolve;
|
|
30
|
+
/** Call this to reject the deferred promise with the given reason. */
|
|
5
31
|
reject;
|
|
32
|
+
/** Indicates whether the promise has been settled (resolved or rejected) yet. */
|
|
6
33
|
isSettled = false;
|
|
7
34
|
constructor() {
|
|
8
35
|
this.promise = new Promise((resolve, reject) => {
|
|
@@ -1,3 +1,21 @@
|
|
|
1
1
|
import { AnyDuration } from '@date-vir/duration';
|
|
2
|
+
/**
|
|
3
|
+
* An async pause for the given duration.
|
|
4
|
+
*
|
|
5
|
+
* @category Promise
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
8
|
+
* @see
|
|
9
|
+
* - {@link waitValue} : return a value after the wait finishes.
|
|
10
|
+
*/
|
|
2
11
|
export declare function wait(duration: Readonly<AnyDuration>): Promise<void>;
|
|
12
|
+
/**
|
|
13
|
+
* An async pause for the given duration that then returns the given `value`.
|
|
14
|
+
*
|
|
15
|
+
* @category Promise
|
|
16
|
+
* @category Package : @augment-vir/common
|
|
17
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
18
|
+
* @see
|
|
19
|
+
* - {@link wait} : plain wait.
|
|
20
|
+
*/
|
|
3
21
|
export declare function waitValue<Value>(duration: Readonly<AnyDuration>, value: Value): Promise<Value>;
|
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import { convertDuration, DurationUnit } from '@date-vir/duration';
|
|
2
2
|
import { DeferredPromise } from './deferred-promise.js';
|
|
3
|
+
/**
|
|
4
|
+
* An async pause for the given duration.
|
|
5
|
+
*
|
|
6
|
+
* @category Promise
|
|
7
|
+
* @category Package : @augment-vir/common
|
|
8
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
9
|
+
* @see
|
|
10
|
+
* - {@link waitValue} : return a value after the wait finishes.
|
|
11
|
+
*/
|
|
3
12
|
export function wait(duration) {
|
|
4
13
|
const deferredPromise = new DeferredPromise();
|
|
5
14
|
const milliseconds = convertDuration(duration, DurationUnit.Milliseconds).milliseconds;
|
|
@@ -10,6 +19,15 @@ export function wait(duration) {
|
|
|
10
19
|
}
|
|
11
20
|
return deferredPromise.promise;
|
|
12
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* An async pause for the given duration that then returns the given `value`.
|
|
24
|
+
*
|
|
25
|
+
* @category Promise
|
|
26
|
+
* @category Package : @augment-vir/common
|
|
27
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
28
|
+
* @see
|
|
29
|
+
* - {@link wait} : plain wait.
|
|
30
|
+
*/
|
|
13
31
|
export async function waitValue(duration, value) {
|
|
14
32
|
await wait(duration);
|
|
15
33
|
return value;
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* JavaScript
|
|
2
|
+
* JavaScript runtime env.
|
|
3
3
|
*
|
|
4
4
|
* @category Env
|
|
5
|
+
* @category Package : @augment-vir/common
|
|
6
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
7
|
+
* @see
|
|
8
|
+
* - {@link currentRuntimeEnv}
|
|
9
|
+
* - {@link isRuntimeEnv}
|
|
10
|
+
* - {@link perEnv}
|
|
5
11
|
*/
|
|
6
12
|
export declare enum RuntimeEnv {
|
|
7
13
|
Node = "node",
|
|
@@ -12,22 +18,54 @@ export declare enum RuntimeEnv {
|
|
|
12
18
|
* simply import {@link currentRuntimeEnv} directly.
|
|
13
19
|
*
|
|
14
20
|
* @category Env
|
|
21
|
+
* @category Package : @augment-vir/common
|
|
22
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
15
23
|
*/
|
|
16
24
|
export declare function determineRuntimeEnv(): RuntimeEnv;
|
|
17
25
|
/**
|
|
18
|
-
* The current {@link RuntimeEnv}
|
|
26
|
+
* The current {@link RuntimeEnv}.
|
|
19
27
|
*
|
|
20
28
|
* @category Env
|
|
29
|
+
* @category Package : @augment-vir/common
|
|
30
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
31
|
+
* @see
|
|
32
|
+
* - {@link RuntimeEnv}
|
|
33
|
+
* - {@link isRuntimeEnv}
|
|
34
|
+
* - {@link perEnv}
|
|
21
35
|
*/
|
|
22
36
|
export declare const currentRuntimeEnv: RuntimeEnv;
|
|
23
37
|
/**
|
|
24
38
|
* Checks if the given {@link RuntimeEnv} value is the current {@link RuntimeEnv} value.
|
|
25
39
|
*
|
|
26
40
|
* @category Env
|
|
41
|
+
* @category Package : @augment-vir/common
|
|
27
42
|
* @returns `true` if the given {@link RuntimeEnv} is the current {@link RuntimeEnv}.
|
|
43
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
44
|
+
* - {@link RuntimeEnv}
|
|
45
|
+
* - {@link currentRuntimeEnv}
|
|
46
|
+
* - {@link perEnv}
|
|
28
47
|
*/
|
|
29
48
|
export declare function isRuntimeEnv(itItThisEnv: RuntimeEnv): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Throw this Error to indicate that something was attempted that cannot be done in the current
|
|
51
|
+
* runtime.
|
|
52
|
+
*
|
|
53
|
+
* @category Env
|
|
54
|
+
* @category Package : @augment-vir/common
|
|
55
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
56
|
+
*/
|
|
30
57
|
export declare class RuntimeEnvError extends Error {
|
|
31
58
|
readonly name = "RuntimeEnvError";
|
|
32
59
|
}
|
|
33
|
-
|
|
60
|
+
/**
|
|
61
|
+
* Requires defining an object of functions for all possible {@link RuntimeEnv} values and then only
|
|
62
|
+
* calls the function for the current runtime.
|
|
63
|
+
*
|
|
64
|
+
* @category Env
|
|
65
|
+
* @category Package : @augment-vir/common
|
|
66
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
67
|
+
* - {@link RuntimeEnv}
|
|
68
|
+
* - {@link currentRuntimeEnv}
|
|
69
|
+
* - {@link isRuntimeEnv}
|
|
70
|
+
*/
|
|
71
|
+
export declare function perEnv<T>(perEnv: Record<RuntimeEnv, () => T>): T;
|
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import { isNode } from 'browser-or-node';
|
|
2
2
|
/**
|
|
3
|
-
* JavaScript
|
|
3
|
+
* JavaScript runtime env.
|
|
4
4
|
*
|
|
5
5
|
* @category Env
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
8
|
+
* @see
|
|
9
|
+
* - {@link currentRuntimeEnv}
|
|
10
|
+
* - {@link isRuntimeEnv}
|
|
11
|
+
* - {@link perEnv}
|
|
6
12
|
*/
|
|
7
13
|
export var RuntimeEnv;
|
|
8
14
|
(function (RuntimeEnv) {
|
|
@@ -14,6 +20,8 @@ export var RuntimeEnv;
|
|
|
14
20
|
* simply import {@link currentRuntimeEnv} directly.
|
|
15
21
|
*
|
|
16
22
|
* @category Env
|
|
23
|
+
* @category Package : @augment-vir/common
|
|
24
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
17
25
|
*/
|
|
18
26
|
export function determineRuntimeEnv() {
|
|
19
27
|
/** Coverage in this package is only run in Node. */
|
|
@@ -21,23 +29,53 @@ export function determineRuntimeEnv() {
|
|
|
21
29
|
return isNode ? RuntimeEnv.Node : RuntimeEnv.Web;
|
|
22
30
|
}
|
|
23
31
|
/**
|
|
24
|
-
* The current {@link RuntimeEnv}
|
|
32
|
+
* The current {@link RuntimeEnv}.
|
|
25
33
|
*
|
|
26
34
|
* @category Env
|
|
35
|
+
* @category Package : @augment-vir/common
|
|
36
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
37
|
+
* @see
|
|
38
|
+
* - {@link RuntimeEnv}
|
|
39
|
+
* - {@link isRuntimeEnv}
|
|
40
|
+
* - {@link perEnv}
|
|
27
41
|
*/
|
|
28
42
|
export const currentRuntimeEnv = determineRuntimeEnv();
|
|
29
43
|
/**
|
|
30
44
|
* Checks if the given {@link RuntimeEnv} value is the current {@link RuntimeEnv} value.
|
|
31
45
|
*
|
|
32
46
|
* @category Env
|
|
47
|
+
* @category Package : @augment-vir/common
|
|
33
48
|
* @returns `true` if the given {@link RuntimeEnv} is the current {@link RuntimeEnv}.
|
|
49
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
50
|
+
* - {@link RuntimeEnv}
|
|
51
|
+
* - {@link currentRuntimeEnv}
|
|
52
|
+
* - {@link perEnv}
|
|
34
53
|
*/
|
|
35
54
|
export function isRuntimeEnv(itItThisEnv) {
|
|
36
55
|
return currentRuntimeEnv === itItThisEnv;
|
|
37
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Throw this Error to indicate that something was attempted that cannot be done in the current
|
|
59
|
+
* runtime.
|
|
60
|
+
*
|
|
61
|
+
* @category Env
|
|
62
|
+
* @category Package : @augment-vir/common
|
|
63
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
64
|
+
*/
|
|
38
65
|
export class RuntimeEnvError extends Error {
|
|
39
66
|
name = 'RuntimeEnvError';
|
|
40
67
|
}
|
|
41
|
-
|
|
68
|
+
/**
|
|
69
|
+
* Requires defining an object of functions for all possible {@link RuntimeEnv} values and then only
|
|
70
|
+
* calls the function for the current runtime.
|
|
71
|
+
*
|
|
72
|
+
* @category Env
|
|
73
|
+
* @category Package : @augment-vir/common
|
|
74
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
75
|
+
* - {@link RuntimeEnv}
|
|
76
|
+
* - {@link currentRuntimeEnv}
|
|
77
|
+
* - {@link isRuntimeEnv}
|
|
78
|
+
*/
|
|
79
|
+
export function perEnv(perEnv) {
|
|
42
80
|
return perEnv[currentRuntimeEnv]();
|
|
43
81
|
}
|
|
@@ -1,3 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removes ansi escape codes (such as terminal colors) within the given string.
|
|
3
|
+
*
|
|
4
|
+
* @category String
|
|
5
|
+
* @category Package : @augment-vir/common
|
|
6
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
7
|
+
*/
|
|
1
8
|
export declare function removeAnsiEscapeCodes(input: string): string;
|
|
9
|
+
/** {@inheritDoc removeAnsiEscapeCodes} */
|
|
2
10
|
export declare const removeColor: typeof removeAnsiEscapeCodes;
|
|
11
|
+
/**
|
|
12
|
+
* A RegExp that will match all ansi escape codes (such as terminal colors). Used in
|
|
13
|
+
* {@link removeAnsiEscapeCodes}.
|
|
14
|
+
*
|
|
15
|
+
* @category String
|
|
16
|
+
* @category RegExp
|
|
17
|
+
* @category Package : @augment-vir/common
|
|
18
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
19
|
+
*/
|
|
3
20
|
export declare const ansiRegExp: RegExp;
|
|
@@ -1,6 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removes ansi escape codes (such as terminal colors) within the given string.
|
|
3
|
+
*
|
|
4
|
+
* @category String
|
|
5
|
+
* @category Package : @augment-vir/common
|
|
6
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
7
|
+
*/
|
|
1
8
|
export function removeAnsiEscapeCodes(input) {
|
|
2
9
|
return input.replace(ansiRegExp, '');
|
|
3
10
|
}
|
|
11
|
+
/** {@inheritDoc removeAnsiEscapeCodes} */
|
|
4
12
|
export const removeColor = removeAnsiEscapeCodes;
|
|
5
13
|
// cspell:disable
|
|
6
14
|
/**
|
|
@@ -33,4 +41,13 @@ const patterns = [
|
|
|
33
41
|
String.raw `[\u001B\u009B][[\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\d\/#&.:=?%@~_]+)*|[a-zA-Z\d]+(?:;[-a-zA-Z\d\/#&.:=?%@~_]*)*)?\u0007)`,
|
|
34
42
|
String.raw `(?:(?:\d{1,4}(?:;\d{0,4})*)?[\dA-PR-TZcf-nq-uy=><~]))`,
|
|
35
43
|
];
|
|
44
|
+
/**
|
|
45
|
+
* A RegExp that will match all ansi escape codes (such as terminal colors). Used in
|
|
46
|
+
* {@link removeAnsiEscapeCodes}.
|
|
47
|
+
*
|
|
48
|
+
* @category String
|
|
49
|
+
* @category RegExp
|
|
50
|
+
* @category Package : @augment-vir/common
|
|
51
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
52
|
+
*/
|
|
36
53
|
export const ansiRegExp = new RegExp(patterns.join('|'), 'g');
|
|
@@ -1,6 +1,43 @@
|
|
|
1
1
|
import type { ArrayElement } from '../array/array.js';
|
|
2
|
+
/**
|
|
3
|
+
* All characters that are considered punctuation.
|
|
4
|
+
*
|
|
5
|
+
* @category String
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
8
|
+
*/
|
|
2
9
|
export declare const punctuationLetters: readonly [".", ":", ";", ",", "?", "!"];
|
|
10
|
+
/**
|
|
11
|
+
* A RegExp matching all letters that are considered punctuation.
|
|
12
|
+
*
|
|
13
|
+
* @category String
|
|
14
|
+
* @category RegExp
|
|
15
|
+
* @category Package : @augment-vir/common
|
|
16
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
17
|
+
*/
|
|
3
18
|
export declare const punctuationRegExp: RegExp;
|
|
19
|
+
/**
|
|
20
|
+
* A RegExp matching that matches any punctuation at the end of a string.
|
|
21
|
+
*
|
|
22
|
+
* @category String
|
|
23
|
+
* @category RegExp
|
|
24
|
+
* @category Package : @augment-vir/common
|
|
25
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
26
|
+
*/
|
|
4
27
|
export declare const endsWithPunctuationRegExp: RegExp;
|
|
28
|
+
/**
|
|
29
|
+
* All characters that are considered punctuation.
|
|
30
|
+
*
|
|
31
|
+
* @category String
|
|
32
|
+
* @category Package : @augment-vir/common
|
|
33
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
34
|
+
*/
|
|
5
35
|
export type PunctuationLetter = ArrayElement<typeof punctuationLetters>;
|
|
36
|
+
/**
|
|
37
|
+
* Removes any punctuation at the end of the given string.
|
|
38
|
+
*
|
|
39
|
+
* @category String
|
|
40
|
+
* @category Package : @augment-vir/common
|
|
41
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
42
|
+
*/
|
|
6
43
|
export declare function removeEndingPunctuation(value: string): string;
|
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* All characters that are considered punctuation.
|
|
3
|
+
*
|
|
4
|
+
* @category String
|
|
5
|
+
* @category Package : @augment-vir/common
|
|
6
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
7
|
+
*/
|
|
1
8
|
export const punctuationLetters = [
|
|
2
9
|
'.',
|
|
3
10
|
':',
|
|
@@ -6,8 +13,31 @@ export const punctuationLetters = [
|
|
|
6
13
|
'?',
|
|
7
14
|
'!',
|
|
8
15
|
];
|
|
16
|
+
/**
|
|
17
|
+
* A RegExp matching all letters that are considered punctuation.
|
|
18
|
+
*
|
|
19
|
+
* @category String
|
|
20
|
+
* @category RegExp
|
|
21
|
+
* @category Package : @augment-vir/common
|
|
22
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
23
|
+
*/
|
|
9
24
|
export const punctuationRegExp = new RegExp(`[${punctuationLetters.join('')}]+`);
|
|
25
|
+
/**
|
|
26
|
+
* A RegExp matching that matches any punctuation at the end of a string.
|
|
27
|
+
*
|
|
28
|
+
* @category String
|
|
29
|
+
* @category RegExp
|
|
30
|
+
* @category Package : @augment-vir/common
|
|
31
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
32
|
+
*/
|
|
10
33
|
export const endsWithPunctuationRegExp = new RegExp(`[${punctuationLetters.join('')}]+$`);
|
|
34
|
+
/**
|
|
35
|
+
* Removes any punctuation at the end of the given string.
|
|
36
|
+
*
|
|
37
|
+
* @category String
|
|
38
|
+
* @category Package : @augment-vir/common
|
|
39
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
40
|
+
*/
|
|
11
41
|
export function removeEndingPunctuation(value) {
|
|
12
42
|
return value.replace(endsWithPunctuationRegExp, '');
|
|
13
43
|
}
|
|
@@ -1,4 +1,19 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* Any of the UUID versions.
|
|
3
|
+
*
|
|
4
|
+
* @category String
|
|
5
|
+
* @category UUID
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
8
|
+
*/
|
|
2
9
|
export type Uuid = `${string}-${string}-${string}-${string}-${string}`;
|
|
3
|
-
/**
|
|
10
|
+
/**
|
|
11
|
+
* Creates a cryptographically secure random v4 UUID using
|
|
12
|
+
* [`crypto.randomUUID`](https://developer.mozilla.org/docs/Web/API/Crypto/randomUUID).
|
|
13
|
+
*
|
|
14
|
+
* @category String
|
|
15
|
+
* @category UUID
|
|
16
|
+
* @category Package : @augment-vir/common
|
|
17
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
18
|
+
*/
|
|
4
19
|
export declare function createUuidV4(): `${string}-${string}-${string}-${string}-${string}`;
|
|
@@ -1,4 +1,12 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* Creates a cryptographically secure random v4 UUID using
|
|
3
|
+
* [`crypto.randomUUID`](https://developer.mozilla.org/docs/Web/API/Crypto/randomUUID).
|
|
4
|
+
*
|
|
5
|
+
* @category String
|
|
6
|
+
* @category UUID
|
|
7
|
+
* @category Package : @augment-vir/common
|
|
8
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
9
|
+
*/
|
|
2
10
|
export function createUuidV4() {
|
|
3
11
|
return crypto.randomUUID();
|
|
4
12
|
}
|
package/package.json
CHANGED
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@augment-vir/core",
|
|
3
|
-
"version": "30.0.
|
|
4
|
-
"description": "Core augment-vir augments
|
|
5
|
-
"keywords": [
|
|
6
|
-
"augment-vir",
|
|
7
|
-
"vir",
|
|
8
|
-
"core",
|
|
9
|
-
"env"
|
|
10
|
-
],
|
|
3
|
+
"version": "30.0.1",
|
|
4
|
+
"description": "Core augment-vir augments. Use @augment-vir/common instead.",
|
|
11
5
|
"homepage": "https://github.com/electrovir/augment-vir",
|
|
12
6
|
"bugs": {
|
|
13
7
|
"url": "https://github.com/electrovir/augment-vir/issues"
|
|
@@ -27,11 +21,9 @@
|
|
|
27
21
|
"types": "dist/index.d.ts",
|
|
28
22
|
"scripts": {
|
|
29
23
|
"compile": "virmator compile",
|
|
30
|
-
"docs": "virmator docs",
|
|
31
24
|
"start": "tsx src/index.ts",
|
|
32
25
|
"test": "virmator test node",
|
|
33
26
|
"test:coverage": "npm run test coverage",
|
|
34
|
-
"test:docs": "virmator docs check",
|
|
35
27
|
"test:update": "npm test"
|
|
36
28
|
},
|
|
37
29
|
"dependencies": {
|