botbuilder-stdlib
Advanced tools
Comparing version 4.15.0-dev.internal.ffde26a to 4.15.0-internal
@@ -1,4 +0,4 @@ | ||
import * as assertExt_1 from './assertExt'; | ||
export { assertExt_1 as assertExt }; | ||
export * from './types'; | ||
import * as stringExt_1 from './stringExt'; | ||
export { stringExt_1 as stringExt }; | ||
export { delay } from './delay'; | ||
@@ -5,0 +5,0 @@ export { maybeCast } from './maybeCast'; |
@@ -1,2 +0,1 @@ | ||
import { NewableError } from './assertExt'; | ||
export declare type Nil = null | undefined; | ||
@@ -8,476 +7,2 @@ export declare type Maybe<T> = T | Nil; | ||
}; | ||
export declare type Dictionary<V = unknown, K extends string | number = string | number> = Record<K, V>; | ||
export declare type Test<T> = (val: unknown) => val is T; | ||
export declare type Assertion<T> = (val: unknown, path: string[]) => void; | ||
/** | ||
* An error that indicates that the source of the error was an undefined value | ||
*/ | ||
export declare class UndefinedError extends Error { | ||
} | ||
/** | ||
* Asserts `cond` to the typescript compiler | ||
* | ||
* @param {any} cond a condition to assert | ||
* @param {string[]} path the accumulated path for the assertion | ||
* @param {string} message an error message to use | ||
* @param {NewableError} errorCtor an optional error constructor | ||
*/ | ||
declare function condition(cond: unknown, path: string[], message: string, errorCtor?: NewableError): void; | ||
/** | ||
* Construct an assertion function | ||
* | ||
* @template T the type to assert | ||
* @param {string} typeName the name of type `T` | ||
* @param {Test<T>} test a method to test if an unknown value is of type `T` | ||
* @param {boolean} acceptNil true if null or undefined values are acceptable | ||
* @returns {Assertion<T>} an assertion that asserts an unknown value is of type `T` | ||
*/ | ||
declare function makeAssertion<T>(typeName: string, test: Test<T>, acceptNil?: boolean): Assertion<T>; | ||
/** | ||
* Constructs a type assertion that is enforced if the value is not null or undefined | ||
* | ||
* @template T the type to assert | ||
* @param {Assertion<T>} assertion the assertion | ||
* @returns {Assertion<Maybe<T>>} an assertion that asserts an unknown value is of type `T` or `Nil` | ||
*/ | ||
declare function makeMaybeAssertion<T>(assertion: Assertion<T>): Assertion<Maybe<T>>; | ||
/** | ||
* Takes an assertion for type `T` and returns an assertion for type `Partial<T>`. The implementation | ||
* expects that the assertion throws `UndefinedError` if an expected value is undefined. All the assertions | ||
* exported by this package satisfy that requirement. | ||
* | ||
* @template T a type extending from `Dictionary` | ||
* @param {Assertion<T>} assertion an assertion that asserts an unknown value is of type `T` | ||
* @returns {Assertion<Partial<T>>} an assertion that asserts an unknown value is of type `Partial<T>` | ||
*/ | ||
declare function makePartialAssertion<T extends Dictionary>(assertion: Assertion<T>): Assertion<Partial<T>>; | ||
/** | ||
* Test if `val` is of type `any`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `any` | ||
*/ | ||
declare function isAny(val: unknown): val is any; | ||
/** | ||
* Assert that `val` is of type `any`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function any(val: unknown, path: string[]): void; | ||
/** | ||
* Assert that `val` is of type `any`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function maybeAny(val: unknown, path: string[]): void; | ||
/** | ||
* Test if `val` is of type `array`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `array` | ||
*/ | ||
declare function isArray(val: unknown): val is unknown[]; | ||
/** | ||
* Assert that `val` is of type `array`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function array(val: unknown, path: string[]): void; | ||
/** | ||
* Assert that `val` is of type `array`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function maybeArray(val: unknown, path: string[]): void; | ||
/** | ||
* Test if `val` is of type `boolean`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `boolean` | ||
*/ | ||
declare function isBoolean(val: unknown): val is boolean; | ||
/** | ||
* Assert that `val` is of type `boolean`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function boolean(val: unknown, path: string[]): void; | ||
/** | ||
* Assert that `val` is of type `boolean`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function maybeBoolean(val: unknown, path: string[]): void; | ||
/** | ||
* Test if `val` is of type `Date`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `Date` | ||
*/ | ||
declare function isDate(val: unknown): val is Date; | ||
/** | ||
* Assert that `val` is of type `Date`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function date(val: unknown, path: string[]): void; | ||
/** | ||
* Assert that `val` is of type `Date`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function maybeDate(val: unknown, path: string[]): void; | ||
/** | ||
* Test if `val` is of type `Dictionary`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `Dictionary` | ||
*/ | ||
declare function isDictionary(val: unknown): val is Dictionary; | ||
/** | ||
* Assert that `val` is of type `Dictionary`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function dictionary(val: unknown, path: string[]): void; | ||
/** | ||
* Assert that `val` is of type `Dictionary`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function maybeDictionary(val: unknown, path: string[]): void; | ||
/** | ||
* Test if `val` is of type `Error`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `Error` | ||
*/ | ||
declare function isError(val: unknown): val is Error; | ||
/** | ||
* Assert that `val` is of type `Error`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function error(val: unknown, path: string[]): void; | ||
/** | ||
* Test if `val` is of type `TypeError`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `TypeError` | ||
*/ | ||
declare function isTypeError(val: unknown): val is TypeError; | ||
/** | ||
* Assert that `val` is of type `TypeError`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function typeError(val: unknown, path: string[]): void; | ||
/** | ||
* Test if `val` is of type `UndefinedError`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `UndefinedError` | ||
*/ | ||
declare function isUndefinedError(val: unknown): val is UndefinedError; | ||
/** | ||
* Assert that `val` is of type `UndefinedError`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function undefinedError(val: unknown, path: string[]): void; | ||
export declare type Func<T extends unknown[] = unknown[], R = unknown> = (...args: T) => R; | ||
/** | ||
* Test if `val` is of type `Func`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `Func` | ||
*/ | ||
declare function isFunc(val: unknown): val is Func; | ||
/** | ||
* Assert that `val` is of type `Func`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function func(val: unknown, path: string[]): void; | ||
/** | ||
* Assert that `val` is of type `Func`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function maybeFunc(val: unknown, path: string[]): void; | ||
/** | ||
* Test if `val` is of type `Nil`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `Func` | ||
*/ | ||
declare function isNil(val: unknown): val is Nil; | ||
/** | ||
* Assert that `val` is of type `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function nil(val: unknown, path: string[]): void; | ||
/** | ||
* Test if `val` is of type `string`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `string` | ||
*/ | ||
declare function isString(val: unknown): val is string; | ||
/** | ||
* Test if `val` is of type `string` with zero length or `Nil`. | ||
* | ||
* @remarks | ||
* Implementation of string.IsNullOrEmpty(): https://docs.microsoft.com/en-us/dotnet/api/system.string.isnullorempty?view=netcore-3.1 | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of `string` with zero length or `Nil` | ||
*/ | ||
declare function isStringNullOrEmpty(val: unknown): val is Maybe<string>; | ||
/** | ||
* Assert that `val` is of type `string`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function string(val: unknown, path: string[]): void; | ||
/** | ||
* Assert that `val` is of type `string`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function maybeString(val: unknown, path: string[]): void; | ||
/** | ||
* Test if `val` is of type `number`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `number` | ||
*/ | ||
declare function isNumber(val: unknown): val is number; | ||
/** | ||
* Assert that `val` is of type `number`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function number(val: unknown, path: string[]): void; | ||
/** | ||
* Assert that `val` is of type `number`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function maybeNumber(val: unknown, path: string[]): void; | ||
/** | ||
* Test if `val` is of type `object`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `object` | ||
*/ | ||
declare function isObject(val: unknown): val is object; | ||
/** | ||
* Assert that `val` is of type `object`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function object(val: unknown, path: string[]): void; | ||
/** | ||
* Assert that `val` is of type `object`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function maybeObject(val: unknown, path: string[]): void; | ||
/** | ||
* Test if `val` is of type `unknown`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `unknown` | ||
*/ | ||
declare function isUnknown(val: unknown): val is unknown; | ||
/** | ||
* Assert that `val` is of type `unknown`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function unknown(val: unknown, path: string[]): void; | ||
/** | ||
* Assert that `val` is of type `unknown`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function maybeUnknown(val: unknown, path: string[]): void; | ||
/** | ||
* Make a type test function out of an assertion | ||
* | ||
* @template T the type to test | ||
* @param {Assertion<T>} assertion an assertion | ||
* @returns {Test<T>} a type test that returns true if an unknown value is of type `T` | ||
*/ | ||
declare function makeTest<T>(assertion: Assertion<T>): Test<T>; | ||
/** | ||
* **UNSAFE** | ||
* Test if `val` is of type `object`. | ||
* This test does not actually verify that `val` is of type `T`. It is useful as the first | ||
* line in a nested assertion so that remaining assertion calls can leverage helpful intellisense. | ||
* This method is only exported under the `unsafe` keyword as a constant reminder of this fact. | ||
* | ||
* @template T the type to cast `val` to, should extend `Dictionary<unknown>`, i.e. be itself an object | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `object` | ||
*/ | ||
declare function isObjectAs<T>(val: unknown): val is T; | ||
export declare const tests: { | ||
isAny: typeof isAny; | ||
isArray: typeof isArray; | ||
isBoolean: typeof isBoolean; | ||
isDate: typeof isDate; | ||
isDictionary: typeof isDictionary; | ||
isFunc: typeof isFunc; | ||
isNil: typeof isNil; | ||
isNumber: typeof isNumber; | ||
isObject: typeof isObject; | ||
isString: typeof isString; | ||
isStringNullOrEmpty: typeof isStringNullOrEmpty; | ||
isUnknown: typeof isUnknown; | ||
isError: typeof isError; | ||
isTypeError: typeof isTypeError; | ||
isUndefinedError: typeof isUndefinedError; | ||
fromAssertion: typeof makeTest; | ||
toAssertion: typeof makeAssertion; | ||
unsafe: { | ||
isObjectAs: typeof isObjectAs; | ||
}; | ||
}; | ||
/** | ||
* Construct an assertion that an unknown value is an array with items of type `T` | ||
* | ||
* @template T the item type | ||
* @param {Assertion<T>} assertion the assertion | ||
* @returns {Assertion<Array<T>>} an assertion that asserts an unknown value is an array with items of type `T` | ||
*/ | ||
declare function arrayOf<T>(assertion: Assertion<T>): Assertion<Array<T>>; | ||
/** | ||
* Assert that `val` is of type `string[]`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function arrayOfString(val: unknown, path: string[]): void; | ||
/** | ||
* Construct an assertion that an unknown value is an array with items of type `T`, or `Nil` | ||
* | ||
* @template T the item type | ||
* @param {Assertion<T>} assertion the assertion | ||
* @returns {Assertion<Maybe<Array<T>>>} an assertion that asserts an unknown value is an array with | ||
* items of type `T`, or `Nil` | ||
*/ | ||
declare function maybeArrayOf<T>(assertion: Assertion<T>): Assertion<Maybe<Array<T>>>; | ||
/** | ||
* Construct an assertion that an unknown value is an instance of type `T` | ||
* | ||
* @template T the instance type | ||
* @param {string} typeName the name of type `T` | ||
* @param {Newable<T> | Extends<T>} ctor a constructor reference for type `T` | ||
* @returns {Assertion<T>} an assertion that asserts an unknown value is an instance of type `T` | ||
*/ | ||
declare function instanceOf<T>(typeName: string, ctor: Newable<T> | Extends<T>): Assertion<T>; | ||
/** | ||
* Construct an assertion that an unknown value is an instance of type `T`, or `Nil` | ||
* | ||
* @template T the instance type | ||
* @param {string} typeName the name of type `T` | ||
* @param {Newable<T> | Extends<T>} ctor a constructor reference for type `T` | ||
* @returns {Assertion<Maybe<T>>} an assertion that asserts an unknown value is an instance of type `T`, or `Nil` | ||
*/ | ||
declare function maybeInstanceOf<T>(typeName: string, ctor: Newable<T> | Extends<T>): Assertion<Maybe<T>>; | ||
/** | ||
* Construct an assertion that an unknown value is of type `T`, likely a union type | ||
* | ||
* @template T the type, likely a union of other types | ||
* @param {Array<Test<T>>} tests a set of tests for type `T` | ||
* @returns {Assertion<T>} an assertion that asserts an unknown value is of type `T` | ||
*/ | ||
declare function oneOf<T>(...tests: Array<Test<T>>): Assertion<T>; | ||
/** | ||
* Construct an assertion that an unknown value is of type `T`, likely a union type, or `Nil` | ||
* | ||
* @template T the type, likely a union of other types | ||
* @param {Array<Test<T>>} tests a set of tests for type `T` | ||
* @returns {Assertion<Maybe<T>>} an assertion that asserts an unknown value is of type `T`, or `Nil` | ||
*/ | ||
declare function maybeOneOf<T>(...tests: Array<Test<T>>): Assertion<Maybe<T>>; | ||
/** | ||
* **UNSAFE** | ||
* This assertion does not actually verify that `val` is of type `T`. It is useful as the first | ||
* line in a nested assertion so that remaining assertion calls can leverage helpful intellisense. | ||
* This method is only exported under the `unsafe` keyword as a constant reminder of this fact. | ||
* | ||
* @template T the type to cast `val` to, should extend `Dictionary<unknown>`, i.e. be itself an object | ||
* @param {any} val the unknown value | ||
* @param {string[]} path the accumulated assertion path | ||
*/ | ||
declare function castObjectAs<T>(val: unknown, path: string[]): void; | ||
export declare const assert: { | ||
condition: typeof condition; | ||
any: typeof any; | ||
maybeAny: typeof maybeAny; | ||
array: typeof array; | ||
maybeArray: typeof maybeArray; | ||
boolean: typeof boolean; | ||
maybeBoolean: typeof maybeBoolean; | ||
date: typeof date; | ||
maybeDate: typeof maybeDate; | ||
dictionary: typeof dictionary; | ||
maybeDictionary: typeof maybeDictionary; | ||
error: typeof error; | ||
undefinedError: typeof undefinedError; | ||
typeError: typeof typeError; | ||
func: typeof func; | ||
maybeFunc: typeof maybeFunc; | ||
nil: typeof nil; | ||
number: typeof number; | ||
maybeNumber: typeof maybeNumber; | ||
object: typeof object; | ||
maybeObject: typeof maybeObject; | ||
string: typeof string; | ||
maybeString: typeof maybeString; | ||
unknown: typeof unknown; | ||
maybeUnknown: typeof maybeUnknown; | ||
arrayOf: typeof arrayOf; | ||
maybeArrayOf: typeof maybeArrayOf; | ||
arrayOfString: typeof arrayOfString; | ||
instanceOf: typeof instanceOf; | ||
maybeInstanceOf: typeof maybeInstanceOf; | ||
oneOf: typeof oneOf; | ||
maybeOneOf: typeof maybeOneOf; | ||
fromTest: typeof makeAssertion; | ||
makeMaybe: typeof makeMaybeAssertion; | ||
makePartial: typeof makePartialAssertion; | ||
toTest: typeof makeTest; | ||
unsafe: { | ||
castObjectAs: typeof castObjectAs; | ||
}; | ||
}; | ||
export {}; | ||
//# sourceMappingURL=types.d.ts.map |
@@ -1,3 +0,3 @@ | ||
export * as assertExt from './assertExt'; | ||
export * from './types'; | ||
export * as stringExt from './stringExt'; | ||
export { delay } from './delay'; | ||
@@ -4,0 +4,0 @@ export { maybeCast } from './maybeCast'; |
@@ -16,2 +16,5 @@ "use strict"; | ||
}); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
@@ -24,9 +27,6 @@ if (mod && mod.__esModule) return mod; | ||
}; | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.retry = exports.maybeCast = exports.delay = exports.assertExt = void 0; | ||
exports.assertExt = __importStar(require("./assertExt")); | ||
exports.retry = exports.maybeCast = exports.delay = exports.stringExt = void 0; | ||
__exportStar(require("./types"), exports); | ||
exports.stringExt = __importStar(require("./stringExt")); | ||
var delay_1 = require("./delay"); | ||
@@ -33,0 +33,0 @@ Object.defineProperty(exports, "delay", { enumerable: true, get: function () { return delay_1.delay; } }); |
@@ -1,2 +0,1 @@ | ||
import { NewableError } from './assertExt'; | ||
export declare type Nil = null | undefined; | ||
@@ -8,476 +7,2 @@ export declare type Maybe<T> = T | Nil; | ||
}; | ||
export declare type Dictionary<V = unknown, K extends string | number = string | number> = Record<K, V>; | ||
export declare type Test<T> = (val: unknown) => val is T; | ||
export declare type Assertion<T> = (val: unknown, path: string[]) => asserts val is T; | ||
/** | ||
* An error that indicates that the source of the error was an undefined value | ||
*/ | ||
export declare class UndefinedError extends Error { | ||
} | ||
/** | ||
* Asserts `cond` to the typescript compiler | ||
* | ||
* @param {any} cond a condition to assert | ||
* @param {string[]} path the accumulated path for the assertion | ||
* @param {string} message an error message to use | ||
* @param {NewableError} errorCtor an optional error constructor | ||
*/ | ||
declare function condition(cond: unknown, path: string[], message: string, errorCtor?: NewableError): asserts cond; | ||
/** | ||
* Construct an assertion function | ||
* | ||
* @template T the type to assert | ||
* @param {string} typeName the name of type `T` | ||
* @param {Test<T>} test a method to test if an unknown value is of type `T` | ||
* @param {boolean} acceptNil true if null or undefined values are acceptable | ||
* @returns {Assertion<T>} an assertion that asserts an unknown value is of type `T` | ||
*/ | ||
declare function makeAssertion<T>(typeName: string, test: Test<T>, acceptNil?: boolean): Assertion<T>; | ||
/** | ||
* Constructs a type assertion that is enforced if the value is not null or undefined | ||
* | ||
* @template T the type to assert | ||
* @param {Assertion<T>} assertion the assertion | ||
* @returns {Assertion<Maybe<T>>} an assertion that asserts an unknown value is of type `T` or `Nil` | ||
*/ | ||
declare function makeMaybeAssertion<T>(assertion: Assertion<T>): Assertion<Maybe<T>>; | ||
/** | ||
* Takes an assertion for type `T` and returns an assertion for type `Partial<T>`. The implementation | ||
* expects that the assertion throws `UndefinedError` if an expected value is undefined. All the assertions | ||
* exported by this package satisfy that requirement. | ||
* | ||
* @template T a type extending from `Dictionary` | ||
* @param {Assertion<T>} assertion an assertion that asserts an unknown value is of type `T` | ||
* @returns {Assertion<Partial<T>>} an assertion that asserts an unknown value is of type `Partial<T>` | ||
*/ | ||
declare function makePartialAssertion<T extends Dictionary>(assertion: Assertion<T>): Assertion<Partial<T>>; | ||
/** | ||
* Test if `val` is of type `any`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `any` | ||
*/ | ||
declare function isAny(val: unknown): val is any; | ||
/** | ||
* Assert that `val` is of type `any`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function any(val: unknown, path: string[]): asserts val is any; | ||
/** | ||
* Assert that `val` is of type `any`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function maybeAny(val: unknown, path: string[]): asserts val is Maybe<any>; | ||
/** | ||
* Test if `val` is of type `array`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `array` | ||
*/ | ||
declare function isArray(val: unknown): val is unknown[]; | ||
/** | ||
* Assert that `val` is of type `array`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function array(val: unknown, path: string[]): asserts val is unknown[]; | ||
/** | ||
* Assert that `val` is of type `array`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function maybeArray(val: unknown, path: string[]): asserts val is Maybe<unknown[]>; | ||
/** | ||
* Test if `val` is of type `boolean`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `boolean` | ||
*/ | ||
declare function isBoolean(val: unknown): val is boolean; | ||
/** | ||
* Assert that `val` is of type `boolean`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function boolean(val: unknown, path: string[]): asserts val is boolean; | ||
/** | ||
* Assert that `val` is of type `boolean`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function maybeBoolean(val: unknown, path: string[]): asserts val is Maybe<boolean>; | ||
/** | ||
* Test if `val` is of type `Date`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `Date` | ||
*/ | ||
declare function isDate(val: unknown): val is Date; | ||
/** | ||
* Assert that `val` is of type `Date`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function date(val: unknown, path: string[]): asserts val is Date; | ||
/** | ||
* Assert that `val` is of type `Date`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function maybeDate(val: unknown, path: string[]): asserts val is Maybe<Date>; | ||
/** | ||
* Test if `val` is of type `Dictionary`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `Dictionary` | ||
*/ | ||
declare function isDictionary(val: unknown): val is Dictionary; | ||
/** | ||
* Assert that `val` is of type `Dictionary`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function dictionary(val: unknown, path: string[]): asserts val is Dictionary; | ||
/** | ||
* Assert that `val` is of type `Dictionary`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function maybeDictionary(val: unknown, path: string[]): asserts val is Maybe<Dictionary>; | ||
/** | ||
* Test if `val` is of type `Error`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `Error` | ||
*/ | ||
declare function isError(val: unknown): val is Error; | ||
/** | ||
* Assert that `val` is of type `Error`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function error(val: unknown, path: string[]): asserts val is Error; | ||
/** | ||
* Test if `val` is of type `TypeError`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `TypeError` | ||
*/ | ||
declare function isTypeError(val: unknown): val is TypeError; | ||
/** | ||
* Assert that `val` is of type `TypeError`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function typeError(val: unknown, path: string[]): asserts val is TypeError; | ||
/** | ||
* Test if `val` is of type `UndefinedError`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `UndefinedError` | ||
*/ | ||
declare function isUndefinedError(val: unknown): val is UndefinedError; | ||
/** | ||
* Assert that `val` is of type `UndefinedError`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function undefinedError(val: unknown, path: string[]): asserts val is UndefinedError; | ||
export declare type Func<T extends unknown[] = unknown[], R = unknown> = (...args: T) => R; | ||
/** | ||
* Test if `val` is of type `Func`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `Func` | ||
*/ | ||
declare function isFunc(val: unknown): val is Func; | ||
/** | ||
* Assert that `val` is of type `Func`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function func(val: unknown, path: string[]): asserts val is Func; | ||
/** | ||
* Assert that `val` is of type `Func`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function maybeFunc(val: unknown, path: string[]): asserts val is Maybe<Func>; | ||
/** | ||
* Test if `val` is of type `Nil`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `Func` | ||
*/ | ||
declare function isNil(val: unknown): val is Nil; | ||
/** | ||
* Assert that `val` is of type `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function nil(val: unknown, path: string[]): asserts val is Nil; | ||
/** | ||
* Test if `val` is of type `string`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `string` | ||
*/ | ||
declare function isString(val: unknown): val is string; | ||
/** | ||
* Test if `val` is of type `string` with zero length or `Nil`. | ||
* | ||
* @remarks | ||
* Implementation of string.IsNullOrEmpty(): https://docs.microsoft.com/en-us/dotnet/api/system.string.isnullorempty?view=netcore-3.1 | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of `string` with zero length or `Nil` | ||
*/ | ||
declare function isStringNullOrEmpty(val: unknown): val is Maybe<string>; | ||
/** | ||
* Assert that `val` is of type `string`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function string(val: unknown, path: string[]): asserts val is string; | ||
/** | ||
* Assert that `val` is of type `string`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function maybeString(val: unknown, path: string[]): asserts val is Maybe<string>; | ||
/** | ||
* Test if `val` is of type `number`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `number` | ||
*/ | ||
declare function isNumber(val: unknown): val is number; | ||
/** | ||
* Assert that `val` is of type `number`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function number(val: unknown, path: string[]): asserts val is number; | ||
/** | ||
* Assert that `val` is of type `number`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function maybeNumber(val: unknown, path: string[]): asserts val is Maybe<number>; | ||
/** | ||
* Test if `val` is of type `object`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `object` | ||
*/ | ||
declare function isObject(val: unknown): val is object; | ||
/** | ||
* Assert that `val` is of type `object`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function object(val: unknown, path: string[]): asserts val is object; | ||
/** | ||
* Assert that `val` is of type `object`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function maybeObject(val: unknown, path: string[]): asserts val is Maybe<object>; | ||
/** | ||
* Test if `val` is of type `unknown`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `unknown` | ||
*/ | ||
declare function isUnknown(val: unknown): val is unknown; | ||
/** | ||
* Assert that `val` is of type `unknown`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function unknown(val: unknown, path: string[]): asserts val is unknown; | ||
/** | ||
* Assert that `val` is of type `unknown`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function maybeUnknown(val: unknown, path: string[]): asserts val is Maybe<unknown>; | ||
/** | ||
* Make a type test function out of an assertion | ||
* | ||
* @template T the type to test | ||
* @param {Assertion<T>} assertion an assertion | ||
* @returns {Test<T>} a type test that returns true if an unknown value is of type `T` | ||
*/ | ||
declare function makeTest<T>(assertion: Assertion<T>): Test<T>; | ||
/** | ||
* **UNSAFE** | ||
* Test if `val` is of type `object`. | ||
* This test does not actually verify that `val` is of type `T`. It is useful as the first | ||
* line in a nested assertion so that remaining assertion calls can leverage helpful intellisense. | ||
* This method is only exported under the `unsafe` keyword as a constant reminder of this fact. | ||
* | ||
* @template T the type to cast `val` to, should extend `Dictionary<unknown>`, i.e. be itself an object | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `object` | ||
*/ | ||
declare function isObjectAs<T>(val: unknown): val is T; | ||
export declare const tests: { | ||
isAny: typeof isAny; | ||
isArray: typeof isArray; | ||
isBoolean: typeof isBoolean; | ||
isDate: typeof isDate; | ||
isDictionary: typeof isDictionary; | ||
isFunc: typeof isFunc; | ||
isNil: typeof isNil; | ||
isNumber: typeof isNumber; | ||
isObject: typeof isObject; | ||
isString: typeof isString; | ||
isStringNullOrEmpty: typeof isStringNullOrEmpty; | ||
isUnknown: typeof isUnknown; | ||
isError: typeof isError; | ||
isTypeError: typeof isTypeError; | ||
isUndefinedError: typeof isUndefinedError; | ||
fromAssertion: typeof makeTest; | ||
toAssertion: typeof makeAssertion; | ||
unsafe: { | ||
isObjectAs: typeof isObjectAs; | ||
}; | ||
}; | ||
/** | ||
* Construct an assertion that an unknown value is an array with items of type `T` | ||
* | ||
* @template T the item type | ||
* @param {Assertion<T>} assertion the assertion | ||
* @returns {Assertion<Array<T>>} an assertion that asserts an unknown value is an array with items of type `T` | ||
*/ | ||
declare function arrayOf<T>(assertion: Assertion<T>): Assertion<Array<T>>; | ||
/** | ||
* Assert that `val` is of type `string[]`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
declare function arrayOfString(val: unknown, path: string[]): asserts val is string[]; | ||
/** | ||
* Construct an assertion that an unknown value is an array with items of type `T`, or `Nil` | ||
* | ||
* @template T the item type | ||
* @param {Assertion<T>} assertion the assertion | ||
* @returns {Assertion<Maybe<Array<T>>>} an assertion that asserts an unknown value is an array with | ||
* items of type `T`, or `Nil` | ||
*/ | ||
declare function maybeArrayOf<T>(assertion: Assertion<T>): Assertion<Maybe<Array<T>>>; | ||
/** | ||
* Construct an assertion that an unknown value is an instance of type `T` | ||
* | ||
* @template T the instance type | ||
* @param {string} typeName the name of type `T` | ||
* @param {Newable<T> | Extends<T>} ctor a constructor reference for type `T` | ||
* @returns {Assertion<T>} an assertion that asserts an unknown value is an instance of type `T` | ||
*/ | ||
declare function instanceOf<T>(typeName: string, ctor: Newable<T> | Extends<T>): Assertion<T>; | ||
/** | ||
* Construct an assertion that an unknown value is an instance of type `T`, or `Nil` | ||
* | ||
* @template T the instance type | ||
* @param {string} typeName the name of type `T` | ||
* @param {Newable<T> | Extends<T>} ctor a constructor reference for type `T` | ||
* @returns {Assertion<Maybe<T>>} an assertion that asserts an unknown value is an instance of type `T`, or `Nil` | ||
*/ | ||
declare function maybeInstanceOf<T>(typeName: string, ctor: Newable<T> | Extends<T>): Assertion<Maybe<T>>; | ||
/** | ||
* Construct an assertion that an unknown value is of type `T`, likely a union type | ||
* | ||
* @template T the type, likely a union of other types | ||
* @param {Array<Test<T>>} tests a set of tests for type `T` | ||
* @returns {Assertion<T>} an assertion that asserts an unknown value is of type `T` | ||
*/ | ||
declare function oneOf<T>(...tests: Array<Test<T>>): Assertion<T>; | ||
/** | ||
* Construct an assertion that an unknown value is of type `T`, likely a union type, or `Nil` | ||
* | ||
* @template T the type, likely a union of other types | ||
* @param {Array<Test<T>>} tests a set of tests for type `T` | ||
* @returns {Assertion<Maybe<T>>} an assertion that asserts an unknown value is of type `T`, or `Nil` | ||
*/ | ||
declare function maybeOneOf<T>(...tests: Array<Test<T>>): Assertion<Maybe<T>>; | ||
/** | ||
* **UNSAFE** | ||
* This assertion does not actually verify that `val` is of type `T`. It is useful as the first | ||
* line in a nested assertion so that remaining assertion calls can leverage helpful intellisense. | ||
* This method is only exported under the `unsafe` keyword as a constant reminder of this fact. | ||
* | ||
* @template T the type to cast `val` to, should extend `Dictionary<unknown>`, i.e. be itself an object | ||
* @param {any} val the unknown value | ||
* @param {string[]} path the accumulated assertion path | ||
*/ | ||
declare function castObjectAs<T>(val: unknown, path: string[]): asserts val is T; | ||
export declare const assert: { | ||
condition: typeof condition; | ||
any: typeof any; | ||
maybeAny: typeof maybeAny; | ||
array: typeof array; | ||
maybeArray: typeof maybeArray; | ||
boolean: typeof boolean; | ||
maybeBoolean: typeof maybeBoolean; | ||
date: typeof date; | ||
maybeDate: typeof maybeDate; | ||
dictionary: typeof dictionary; | ||
maybeDictionary: typeof maybeDictionary; | ||
error: typeof error; | ||
undefinedError: typeof undefinedError; | ||
typeError: typeof typeError; | ||
func: typeof func; | ||
maybeFunc: typeof maybeFunc; | ||
nil: typeof nil; | ||
number: typeof number; | ||
maybeNumber: typeof maybeNumber; | ||
object: typeof object; | ||
maybeObject: typeof maybeObject; | ||
string: typeof string; | ||
maybeString: typeof maybeString; | ||
unknown: typeof unknown; | ||
maybeUnknown: typeof maybeUnknown; | ||
arrayOf: typeof arrayOf; | ||
maybeArrayOf: typeof maybeArrayOf; | ||
arrayOfString: typeof arrayOfString; | ||
instanceOf: typeof instanceOf; | ||
maybeInstanceOf: typeof maybeInstanceOf; | ||
oneOf: typeof oneOf; | ||
maybeOneOf: typeof maybeOneOf; | ||
fromTest: typeof makeAssertion; | ||
makeMaybe: typeof makeMaybeAssertion; | ||
makePartial: typeof makePartialAssertion; | ||
toTest: typeof makeTest; | ||
unsafe: { | ||
castObjectAs: typeof castObjectAs; | ||
}; | ||
}; | ||
export {}; | ||
//# sourceMappingURL=types.d.ts.map |
656
lib/types.js
@@ -5,658 +5,2 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.assert = exports.tests = exports.UndefinedError = void 0; | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
/* eslint-disable @typescript-eslint/ban-types */ | ||
const assertExt_1 = require("./assertExt"); | ||
// Formats error messages for assertion failures | ||
const formatPathAndMessage = (path, message) => `\`${path.join('.')}\` ${message}`; | ||
/** | ||
* An error that indicates that the source of the error was an undefined value | ||
*/ | ||
class UndefinedError extends Error { | ||
} | ||
exports.UndefinedError = UndefinedError; | ||
/** | ||
* Asserts `cond` to the typescript compiler | ||
* | ||
* @param {any} cond a condition to assert | ||
* @param {string[]} path the accumulated path for the assertion | ||
* @param {string} message an error message to use | ||
* @param {NewableError} errorCtor an optional error constructor | ||
*/ | ||
function condition(cond, path, message, errorCtor = TypeError) { | ||
assertExt_1.assertCondition(cond, formatPathAndMessage(path, message), errorCtor); | ||
} | ||
/** | ||
* Construct an assertion function | ||
* | ||
* @template T the type to assert | ||
* @param {string} typeName the name of type `T` | ||
* @param {Test<T>} test a method to test if an unknown value is of type `T` | ||
* @param {boolean} acceptNil true if null or undefined values are acceptable | ||
* @returns {Assertion<T>} an assertion that asserts an unknown value is of type `T` | ||
*/ | ||
function makeAssertion(typeName, test, acceptNil = false) { | ||
return (val, path) => { | ||
if (!acceptNil) { | ||
condition(!isNil(val), path, 'must be defined', UndefinedError); | ||
} | ||
condition(test(val), path, `must be of type "${typeName}"`); | ||
}; | ||
} | ||
/** | ||
* Constructs a type assertion that is enforced if the value is not null or undefined | ||
* | ||
* @template T the type to assert | ||
* @param {Assertion<T>} assertion the assertion | ||
* @returns {Assertion<Maybe<T>>} an assertion that asserts an unknown value is of type `T` or `Nil` | ||
*/ | ||
function makeMaybeAssertion(assertion) { | ||
return (val, path) => { | ||
if (!isNil(val)) { | ||
assertion(val, path); | ||
} | ||
}; | ||
} | ||
/** | ||
* Takes an assertion for type `T` and returns an assertion for type `Partial<T>`. The implementation | ||
* expects that the assertion throws `UndefinedError` if an expected value is undefined. All the assertions | ||
* exported by this package satisfy that requirement. | ||
* | ||
* @template T a type extending from `Dictionary` | ||
* @param {Assertion<T>} assertion an assertion that asserts an unknown value is of type `T` | ||
* @returns {Assertion<Partial<T>>} an assertion that asserts an unknown value is of type `Partial<T>` | ||
*/ | ||
function makePartialAssertion(assertion) { | ||
return (val, path) => { | ||
try { | ||
assertion(val, path); | ||
} | ||
catch (err) { | ||
if (!isUndefinedError(err)) { | ||
throw err; | ||
} | ||
} | ||
}; | ||
} | ||
/** | ||
* Test if `val` is of type `any`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `any` | ||
*/ | ||
function isAny(val) { | ||
return true; | ||
} | ||
/** | ||
* Assert that `val` is of type `any`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function any(val, path) { | ||
const assertion = makeAssertion('any', isAny); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Assert that `val` is of type `any`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function maybeAny(val, path) { | ||
const assertion = makeMaybeAssertion(any); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Test if `val` is of type `array`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `array` | ||
*/ | ||
function isArray(val) { | ||
return Array.isArray(val); | ||
} | ||
/** | ||
* Assert that `val` is of type `array`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function array(val, path) { | ||
const assertion = makeAssertion('array', isArray); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Assert that `val` is of type `array`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function maybeArray(val, path) { | ||
const assertion = makeMaybeAssertion(array); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Test if `val` is of type `boolean`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `boolean` | ||
*/ | ||
function isBoolean(val) { | ||
return typeof val === 'boolean'; | ||
} | ||
/** | ||
* Assert that `val` is of type `boolean`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function boolean(val, path) { | ||
const assertion = makeAssertion('boolean', isBoolean); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Assert that `val` is of type `boolean`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function maybeBoolean(val, path) { | ||
const assertion = makeMaybeAssertion(boolean); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Test if `val` is of type `Date`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `Date` | ||
*/ | ||
function isDate(val) { | ||
return val instanceof Date; | ||
} | ||
/** | ||
* Assert that `val` is of type `Date`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function date(val, path) { | ||
const assertion = makeAssertion('Date', isDate); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Assert that `val` is of type `Date`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function maybeDate(val, path) { | ||
const assertion = makeMaybeAssertion(date); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Test if `val` is of type `Dictionary`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `Dictionary` | ||
*/ | ||
function isDictionary(val) { | ||
return isObject(val); | ||
} | ||
/** | ||
* Assert that `val` is of type `Dictionary`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function dictionary(val, path) { | ||
const assertion = makeAssertion('Dictionary', isDictionary); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Assert that `val` is of type `Dictionary`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function maybeDictionary(val, path) { | ||
const assertion = makeMaybeAssertion(dictionary); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Test if `val` is of type `Error`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `Error` | ||
*/ | ||
function isError(val) { | ||
return val instanceof Error; | ||
} | ||
/** | ||
* Assert that `val` is of type `Error`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function error(val, path) { | ||
const assertion = makeAssertion('Error', isError); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Test if `val` is of type `TypeError`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `TypeError` | ||
*/ | ||
function isTypeError(val) { | ||
return val instanceof TypeError; | ||
} | ||
/** | ||
* Assert that `val` is of type `TypeError`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function typeError(val, path) { | ||
const assertion = makeAssertion('TypeError', isTypeError); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Test if `val` is of type `UndefinedError`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `UndefinedError` | ||
*/ | ||
function isUndefinedError(val) { | ||
return val instanceof UndefinedError; | ||
} | ||
/** | ||
* Assert that `val` is of type `UndefinedError`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function undefinedError(val, path) { | ||
const assertion = makeAssertion('UndefinedError', isUndefinedError); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Test if `val` is of type `Func`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `Func` | ||
*/ | ||
function isFunc(val) { | ||
return typeof val === 'function'; | ||
} | ||
/** | ||
* Assert that `val` is of type `Func`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function func(val, path) { | ||
const assertion = makeAssertion('Function', isFunc); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Assert that `val` is of type `Func`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function maybeFunc(val, path) { | ||
const assertion = makeMaybeAssertion(func); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Test if `val` is of type `Nil`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `Func` | ||
*/ | ||
function isNil(val) { | ||
return val == null; | ||
} | ||
/** | ||
* Assert that `val` is of type `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function nil(val, path) { | ||
const assertion = makeAssertion('nil', isNil, true); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Test if `val` is of type `string`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `string` | ||
*/ | ||
function isString(val) { | ||
return typeof val === 'string'; | ||
} | ||
/** | ||
* Test if `val` is of type `string` with zero length or `Nil`. | ||
* | ||
* @remarks | ||
* Implementation of string.IsNullOrEmpty(): https://docs.microsoft.com/en-us/dotnet/api/system.string.isnullorempty?view=netcore-3.1 | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of `string` with zero length or `Nil` | ||
*/ | ||
function isStringNullOrEmpty(val) { | ||
return exports.tests.isNil(val) || (exports.tests.isString(val) && !val.length); | ||
} | ||
/** | ||
* Assert that `val` is of type `string`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function string(val, path) { | ||
const assertion = makeAssertion('string', isString); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Assert that `val` is of type `string`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function maybeString(val, path) { | ||
const assertion = makeMaybeAssertion(string); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Test if `val` is of type `number`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `number` | ||
*/ | ||
function isNumber(val) { | ||
return typeof val === 'number' && !isNaN(val); | ||
} | ||
/** | ||
* Assert that `val` is of type `number`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function number(val, path) { | ||
const assertion = makeAssertion('number', isNumber); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Assert that `val` is of type `number`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function maybeNumber(val, path) { | ||
const assertion = makeMaybeAssertion(number); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Test if `val` is of type `object`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `object` | ||
*/ | ||
function isObject(val) { | ||
return !isNil(val) && typeof val === 'object' && !isArray(val); | ||
} | ||
/** | ||
* Assert that `val` is of type `object`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function object(val, path) { | ||
const assertion = makeAssertion('object', isObject); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Assert that `val` is of type `object`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function maybeObject(val, path) { | ||
const assertion = makeMaybeAssertion(object); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Test if `val` is of type `unknown`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `unknown` | ||
*/ | ||
function isUnknown(val) { | ||
return true; | ||
} | ||
/** | ||
* Assert that `val` is of type `unknown`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function unknown(val, path) { | ||
const assertion = makeAssertion('unknown', isUnknown); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Assert that `val` is of type `unknown`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function maybeUnknown(val, path) { | ||
const assertion = makeMaybeAssertion(unknown); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Make a type test function out of an assertion | ||
* | ||
* @template T the type to test | ||
* @param {Assertion<T>} assertion an assertion | ||
* @returns {Test<T>} a type test that returns true if an unknown value is of type `T` | ||
*/ | ||
function makeTest(assertion) { | ||
return (val) => { | ||
try { | ||
assertion(val, []); | ||
return true; | ||
} | ||
catch (_err) { | ||
return false; | ||
} | ||
}; | ||
} | ||
/** | ||
* **UNSAFE** | ||
* Test if `val` is of type `object`. | ||
* This test does not actually verify that `val` is of type `T`. It is useful as the first | ||
* line in a nested assertion so that remaining assertion calls can leverage helpful intellisense. | ||
* This method is only exported under the `unsafe` keyword as a constant reminder of this fact. | ||
* | ||
* @template T the type to cast `val` to, should extend `Dictionary<unknown>`, i.e. be itself an object | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `object` | ||
*/ | ||
function isObjectAs(val) { | ||
castObjectAs(val, []); | ||
return isObject(val); | ||
} | ||
exports.tests = { | ||
isAny, | ||
isArray, | ||
isBoolean, | ||
isDate, | ||
isDictionary, | ||
isFunc, | ||
isNil, | ||
isNumber, | ||
isObject, | ||
isString, | ||
isStringNullOrEmpty, | ||
isUnknown, | ||
isError, | ||
isTypeError, | ||
isUndefinedError, | ||
fromAssertion: makeTest, | ||
toAssertion: makeAssertion, | ||
unsafe: { isObjectAs }, | ||
}; | ||
/** | ||
* Construct an assertion that an unknown value is an array with items of type `T` | ||
* | ||
* @template T the item type | ||
* @param {Assertion<T>} assertion the assertion | ||
* @returns {Assertion<Array<T>>} an assertion that asserts an unknown value is an array with items of type `T` | ||
*/ | ||
function arrayOf(assertion) { | ||
return (val, path) => { | ||
const assertArray = array; | ||
assertArray(val, path); | ||
val.forEach((val, idx) => assertion(val, path.concat(`[${idx}]`))); | ||
}; | ||
} | ||
/** | ||
* Assert that `val` is of type `string[]`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function arrayOfString(val, path) { | ||
const assertion = arrayOf(string); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Construct an assertion that an unknown value is an array with items of type `T`, or `Nil` | ||
* | ||
* @template T the item type | ||
* @param {Assertion<T>} assertion the assertion | ||
* @returns {Assertion<Maybe<Array<T>>>} an assertion that asserts an unknown value is an array with | ||
* items of type `T`, or `Nil` | ||
*/ | ||
function maybeArrayOf(assertion) { | ||
return (val, path) => { | ||
const assertArrayOf = arrayOf(assertion); | ||
const assertMaybeArrayOf = makeMaybeAssertion(assertArrayOf); | ||
assertMaybeArrayOf(val, path); | ||
}; | ||
} | ||
/** | ||
* Construct an assertion that an unknown value is an instance of type `T` | ||
* | ||
* @template T the instance type | ||
* @param {string} typeName the name of type `T` | ||
* @param {Newable<T> | Extends<T>} ctor a constructor reference for type `T` | ||
* @returns {Assertion<T>} an assertion that asserts an unknown value is an instance of type `T` | ||
*/ | ||
function instanceOf(typeName, ctor) { | ||
return (val, path) => { | ||
condition(!isNil(val), path, 'must be defined', UndefinedError); | ||
condition(val instanceof ctor, path, `must be an instance of "${typeName}"`); | ||
}; | ||
} | ||
/** | ||
* Construct an assertion that an unknown value is an instance of type `T`, or `Nil` | ||
* | ||
* @template T the instance type | ||
* @param {string} typeName the name of type `T` | ||
* @param {Newable<T> | Extends<T>} ctor a constructor reference for type `T` | ||
* @returns {Assertion<Maybe<T>>} an assertion that asserts an unknown value is an instance of type `T`, or `Nil` | ||
*/ | ||
function maybeInstanceOf(typeName, ctor) { | ||
return (val, path) => { | ||
const assertInstanceOf = instanceOf(typeName, ctor); | ||
const assertMaybeInstanceOf = makeMaybeAssertion(assertInstanceOf); | ||
assertMaybeInstanceOf(val, path); | ||
}; | ||
} | ||
/** | ||
* Construct an assertion that an unknown value is of type `T`, likely a union type | ||
* | ||
* @template T the type, likely a union of other types | ||
* @param {Array<Test<T>>} tests a set of tests for type `T` | ||
* @returns {Assertion<T>} an assertion that asserts an unknown value is of type `T` | ||
*/ | ||
function oneOf(...tests) { | ||
return (val, path) => { | ||
condition(!isNil(val), path, 'must be defined', UndefinedError); | ||
if (!tests.some((test) => test(val))) { | ||
condition(false, path, 'is of wrong type'); | ||
} | ||
}; | ||
} | ||
/** | ||
* Construct an assertion that an unknown value is of type `T`, likely a union type, or `Nil` | ||
* | ||
* @template T the type, likely a union of other types | ||
* @param {Array<Test<T>>} tests a set of tests for type `T` | ||
* @returns {Assertion<Maybe<T>>} an assertion that asserts an unknown value is of type `T`, or `Nil` | ||
*/ | ||
function maybeOneOf(...tests) { | ||
return (val, path) => { | ||
const assertOneOf = oneOf(...tests); | ||
const assertMaybeOneOf = makeMaybeAssertion(assertOneOf); | ||
assertMaybeOneOf(val, path); | ||
}; | ||
} | ||
/** | ||
* **UNSAFE** | ||
* This assertion does not actually verify that `val` is of type `T`. It is useful as the first | ||
* line in a nested assertion so that remaining assertion calls can leverage helpful intellisense. | ||
* This method is only exported under the `unsafe` keyword as a constant reminder of this fact. | ||
* | ||
* @template T the type to cast `val` to, should extend `Dictionary<unknown>`, i.e. be itself an object | ||
* @param {any} val the unknown value | ||
* @param {string[]} path the accumulated assertion path | ||
*/ | ||
function castObjectAs(val, path) { | ||
const assertWithCast = object; | ||
assertWithCast(val, path); | ||
} | ||
exports.assert = { | ||
condition, | ||
any, | ||
maybeAny, | ||
array, | ||
maybeArray, | ||
boolean, | ||
maybeBoolean, | ||
date, | ||
maybeDate, | ||
dictionary, | ||
maybeDictionary, | ||
error, | ||
undefinedError, | ||
typeError, | ||
func, | ||
maybeFunc, | ||
nil, | ||
number, | ||
maybeNumber, | ||
object, | ||
maybeObject, | ||
string, | ||
maybeString, | ||
unknown, | ||
maybeUnknown, | ||
arrayOf, | ||
maybeArrayOf, | ||
arrayOfString, | ||
instanceOf, | ||
maybeInstanceOf, | ||
oneOf, | ||
maybeOneOf, | ||
// Some helpful, well, helpers | ||
fromTest: makeAssertion, | ||
makeMaybe: makeMaybeAssertion, | ||
makePartial: makePartialAssertion, | ||
toTest: makeTest, | ||
unsafe: { castObjectAs }, | ||
}; | ||
//# sourceMappingURL=types.js.map |
@@ -5,3 +5,3 @@ { | ||
"description": "BotBuilder shared libraries, internal only", | ||
"version": "4.15.0-dev.internal.ffde26a", | ||
"version": "4.15.0-internal", | ||
"internal": true, | ||
@@ -8,0 +8,0 @@ "license": "MIT", |
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
export * as assertExt from './assertExt'; | ||
export * from './types'; | ||
export * as stringExt from './stringExt'; | ||
export { delay } from './delay'; | ||
export { maybeCast } from './maybeCast'; | ||
export { retry } from './retry'; |
743
src/types.ts
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
/* eslint-disable @typescript-eslint/ban-types */ | ||
import { NewableError, assertCondition } from './assertExt'; | ||
// Nil describes a null or undefined value; | ||
@@ -20,739 +15,1 @@ export type Nil = null | undefined; | ||
export type Extends<T> = Function & { prototype: T }; // eslint-disable-line @typescript-eslint/ban-types | ||
// A dictionary type describes a common Javascript object | ||
export type Dictionary<V = unknown, K extends string | number = string | number> = Record<K, V>; | ||
// A type test function signature | ||
export type Test<T> = (val: unknown) => val is T; | ||
// A type assertion method signature | ||
export type Assertion<T> = (val: unknown, path: string[]) => asserts val is T; | ||
// Formats error messages for assertion failures | ||
const formatPathAndMessage = (path: string[], message: string): string => `\`${path.join('.')}\` ${message}`; | ||
/** | ||
* An error that indicates that the source of the error was an undefined value | ||
*/ | ||
export class UndefinedError extends Error {} | ||
/** | ||
* Asserts `cond` to the typescript compiler | ||
* | ||
* @param {any} cond a condition to assert | ||
* @param {string[]} path the accumulated path for the assertion | ||
* @param {string} message an error message to use | ||
* @param {NewableError} errorCtor an optional error constructor | ||
*/ | ||
function condition(cond: unknown, path: string[], message: string, errorCtor: NewableError = TypeError): asserts cond { | ||
assertCondition(cond, formatPathAndMessage(path, message), errorCtor); | ||
} | ||
/** | ||
* Construct an assertion function | ||
* | ||
* @template T the type to assert | ||
* @param {string} typeName the name of type `T` | ||
* @param {Test<T>} test a method to test if an unknown value is of type `T` | ||
* @param {boolean} acceptNil true if null or undefined values are acceptable | ||
* @returns {Assertion<T>} an assertion that asserts an unknown value is of type `T` | ||
*/ | ||
function makeAssertion<T>(typeName: string, test: Test<T>, acceptNil = false): Assertion<T> { | ||
return (val, path) => { | ||
if (!acceptNil) { | ||
condition(!isNil(val), path, 'must be defined', UndefinedError); | ||
} | ||
condition(test(val), path, `must be of type "${typeName}"`); | ||
}; | ||
} | ||
/** | ||
* Constructs a type assertion that is enforced if the value is not null or undefined | ||
* | ||
* @template T the type to assert | ||
* @param {Assertion<T>} assertion the assertion | ||
* @returns {Assertion<Maybe<T>>} an assertion that asserts an unknown value is of type `T` or `Nil` | ||
*/ | ||
function makeMaybeAssertion<T>(assertion: Assertion<T>): Assertion<Maybe<T>> { | ||
return (val, path) => { | ||
if (!isNil(val)) { | ||
assertion(val, path); | ||
} | ||
}; | ||
} | ||
/** | ||
* Takes an assertion for type `T` and returns an assertion for type `Partial<T>`. The implementation | ||
* expects that the assertion throws `UndefinedError` if an expected value is undefined. All the assertions | ||
* exported by this package satisfy that requirement. | ||
* | ||
* @template T a type extending from `Dictionary` | ||
* @param {Assertion<T>} assertion an assertion that asserts an unknown value is of type `T` | ||
* @returns {Assertion<Partial<T>>} an assertion that asserts an unknown value is of type `Partial<T>` | ||
*/ | ||
function makePartialAssertion<T extends Dictionary>(assertion: Assertion<T>): Assertion<Partial<T>> { | ||
return (val, path) => { | ||
try { | ||
assertion(val, path); | ||
} catch (err) { | ||
if (!isUndefinedError(err)) { | ||
throw err; | ||
} | ||
} | ||
}; | ||
} | ||
/** | ||
* Test if `val` is of type `any`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `any` | ||
*/ | ||
function isAny(val: unknown): val is any { | ||
return true; | ||
} | ||
/** | ||
* Assert that `val` is of type `any`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function any(val: unknown, path: string[]): asserts val is any { | ||
const assertion: Assertion<any> = makeAssertion('any', isAny); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Assert that `val` is of type `any`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function maybeAny(val: unknown, path: string[]): asserts val is Maybe<any> { | ||
const assertion: Assertion<Maybe<any>> = makeMaybeAssertion(any); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Test if `val` is of type `array`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `array` | ||
*/ | ||
function isArray(val: unknown): val is unknown[] { | ||
return Array.isArray(val); | ||
} | ||
/** | ||
* Assert that `val` is of type `array`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function array(val: unknown, path: string[]): asserts val is unknown[] { | ||
const assertion: Assertion<unknown[]> = makeAssertion('array', isArray); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Assert that `val` is of type `array`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function maybeArray(val: unknown, path: string[]): asserts val is Maybe<unknown[]> { | ||
const assertion: Assertion<Maybe<unknown[]>> = makeMaybeAssertion(array); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Test if `val` is of type `boolean`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `boolean` | ||
*/ | ||
function isBoolean(val: unknown): val is boolean { | ||
return typeof val === 'boolean'; | ||
} | ||
/** | ||
* Assert that `val` is of type `boolean`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function boolean(val: unknown, path: string[]): asserts val is boolean { | ||
const assertion: Assertion<boolean> = makeAssertion('boolean', isBoolean); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Assert that `val` is of type `boolean`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function maybeBoolean(val: unknown, path: string[]): asserts val is Maybe<boolean> { | ||
const assertion: Assertion<Maybe<boolean>> = makeMaybeAssertion(boolean); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Test if `val` is of type `Date`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `Date` | ||
*/ | ||
function isDate(val: unknown): val is Date { | ||
return val instanceof Date; | ||
} | ||
/** | ||
* Assert that `val` is of type `Date`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function date(val: unknown, path: string[]): asserts val is Date { | ||
const assertion: Assertion<Date> = makeAssertion('Date', isDate); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Assert that `val` is of type `Date`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function maybeDate(val: unknown, path: string[]): asserts val is Maybe<Date> { | ||
const assertion: Assertion<Maybe<Date>> = makeMaybeAssertion(date); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Test if `val` is of type `Dictionary`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `Dictionary` | ||
*/ | ||
function isDictionary(val: unknown): val is Dictionary { | ||
return isObject(val); | ||
} | ||
/** | ||
* Assert that `val` is of type `Dictionary`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function dictionary(val: unknown, path: string[]): asserts val is Dictionary { | ||
const assertion: Assertion<Dictionary> = makeAssertion('Dictionary', isDictionary); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Assert that `val` is of type `Dictionary`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function maybeDictionary(val: unknown, path: string[]): asserts val is Maybe<Dictionary> { | ||
const assertion: Assertion<Maybe<Dictionary>> = makeMaybeAssertion(dictionary); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Test if `val` is of type `Error`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `Error` | ||
*/ | ||
function isError(val: unknown): val is Error { | ||
return val instanceof Error; | ||
} | ||
/** | ||
* Assert that `val` is of type `Error`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function error(val: unknown, path: string[]): asserts val is Error { | ||
const assertion: Assertion<Error> = makeAssertion('Error', isError); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Test if `val` is of type `TypeError`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `TypeError` | ||
*/ | ||
function isTypeError(val: unknown): val is TypeError { | ||
return val instanceof TypeError; | ||
} | ||
/** | ||
* Assert that `val` is of type `TypeError`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function typeError(val: unknown, path: string[]): asserts val is TypeError { | ||
const assertion: Assertion<TypeError> = makeAssertion('TypeError', isTypeError); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Test if `val` is of type `UndefinedError`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `UndefinedError` | ||
*/ | ||
function isUndefinedError(val: unknown): val is UndefinedError { | ||
return val instanceof UndefinedError; | ||
} | ||
/** | ||
* Assert that `val` is of type `UndefinedError`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function undefinedError(val: unknown, path: string[]): asserts val is UndefinedError { | ||
const assertion: Assertion<UndefinedError> = makeAssertion('UndefinedError', isUndefinedError); | ||
assertion(val, path); | ||
} | ||
// Represents a generic function | ||
export type Func<T extends unknown[] = unknown[], R = unknown> = (...args: T) => R; | ||
/** | ||
* Test if `val` is of type `Func`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `Func` | ||
*/ | ||
function isFunc(val: unknown): val is Func { | ||
return typeof val === 'function'; | ||
} | ||
/** | ||
* Assert that `val` is of type `Func`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function func(val: unknown, path: string[]): asserts val is Func { | ||
const assertion: Assertion<Func> = makeAssertion('Function', isFunc); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Assert that `val` is of type `Func`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function maybeFunc(val: unknown, path: string[]): asserts val is Maybe<Func> { | ||
const assertion: Assertion<Maybe<Func>> = makeMaybeAssertion(func); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Test if `val` is of type `Nil`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `Func` | ||
*/ | ||
function isNil(val: unknown): val is Nil { | ||
return val == null; | ||
} | ||
/** | ||
* Assert that `val` is of type `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function nil(val: unknown, path: string[]): asserts val is Nil { | ||
const assertion: Assertion<Nil> = makeAssertion('nil', isNil, true); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Test if `val` is of type `string`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `string` | ||
*/ | ||
function isString(val: unknown): val is string { | ||
return typeof val === 'string'; | ||
} | ||
/** | ||
* Test if `val` is of type `string` with zero length or `Nil`. | ||
* | ||
* @remarks | ||
* Implementation of string.IsNullOrEmpty(): https://docs.microsoft.com/en-us/dotnet/api/system.string.isnullorempty?view=netcore-3.1 | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of `string` with zero length or `Nil` | ||
*/ | ||
function isStringNullOrEmpty(val: unknown): val is Maybe<string> { | ||
return tests.isNil(val) || (tests.isString(val) && !val.length); | ||
} | ||
/** | ||
* Assert that `val` is of type `string`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function string(val: unknown, path: string[]): asserts val is string { | ||
const assertion: Assertion<string> = makeAssertion('string', isString); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Assert that `val` is of type `string`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function maybeString(val: unknown, path: string[]): asserts val is Maybe<string> { | ||
const assertion: Assertion<Maybe<string>> = makeMaybeAssertion(string); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Test if `val` is of type `number`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `number` | ||
*/ | ||
function isNumber(val: unknown): val is number { | ||
return typeof val === 'number' && !isNaN(val); | ||
} | ||
/** | ||
* Assert that `val` is of type `number`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function number(val: unknown, path: string[]): asserts val is number { | ||
const assertion: Assertion<number> = makeAssertion('number', isNumber); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Assert that `val` is of type `number`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function maybeNumber(val: unknown, path: string[]): asserts val is Maybe<number> { | ||
const assertion: Assertion<Maybe<number>> = makeMaybeAssertion(number); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Test if `val` is of type `object`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `object` | ||
*/ | ||
function isObject(val: unknown): val is object { | ||
return !isNil(val) && typeof val === 'object' && !isArray(val); | ||
} | ||
/** | ||
* Assert that `val` is of type `object`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function object(val: unknown, path: string[]): asserts val is object { | ||
const assertion: Assertion<object> = makeAssertion('object', isObject); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Assert that `val` is of type `object`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function maybeObject(val: unknown, path: string[]): asserts val is Maybe<object> { | ||
const assertion: Assertion<Maybe<object>> = makeMaybeAssertion(object); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Test if `val` is of type `unknown`. | ||
* | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `unknown` | ||
*/ | ||
function isUnknown(val: unknown): val is unknown { | ||
return true; | ||
} | ||
/** | ||
* Assert that `val` is of type `unknown`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function unknown(val: unknown, path: string[]): asserts val is unknown { | ||
const assertion: Assertion<unknown> = makeAssertion('unknown', isUnknown); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Assert that `val` is of type `unknown`, or `Nil`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function maybeUnknown(val: unknown, path: string[]): asserts val is Maybe<unknown> { | ||
const assertion: Assertion<Maybe<unknown>> = makeMaybeAssertion(unknown); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Make a type test function out of an assertion | ||
* | ||
* @template T the type to test | ||
* @param {Assertion<T>} assertion an assertion | ||
* @returns {Test<T>} a type test that returns true if an unknown value is of type `T` | ||
*/ | ||
function makeTest<T>(assertion: Assertion<T>): Test<T> { | ||
return (val): val is T => { | ||
try { | ||
assertion(val, []); | ||
return true; | ||
} catch (_err) { | ||
return false; | ||
} | ||
}; | ||
} | ||
/** | ||
* **UNSAFE** | ||
* Test if `val` is of type `object`. | ||
* This test does not actually verify that `val` is of type `T`. It is useful as the first | ||
* line in a nested assertion so that remaining assertion calls can leverage helpful intellisense. | ||
* This method is only exported under the `unsafe` keyword as a constant reminder of this fact. | ||
* | ||
* @template T the type to cast `val` to, should extend `Dictionary<unknown>`, i.e. be itself an object | ||
* @param {any} val value to test | ||
* @returns {boolean} true if `val` is of type `object` | ||
*/ | ||
function isObjectAs<T>(val: unknown): val is T { | ||
castObjectAs<T>(val, []); | ||
return isObject(val); | ||
} | ||
export const tests = { | ||
isAny, | ||
isArray, | ||
isBoolean, | ||
isDate, | ||
isDictionary, | ||
isFunc, | ||
isNil, | ||
isNumber, | ||
isObject, | ||
isString, | ||
isStringNullOrEmpty, | ||
isUnknown, | ||
isError, | ||
isTypeError, | ||
isUndefinedError, | ||
fromAssertion: makeTest, | ||
toAssertion: makeAssertion, | ||
unsafe: { isObjectAs }, | ||
}; | ||
/** | ||
* Construct an assertion that an unknown value is an array with items of type `T` | ||
* | ||
* @template T the item type | ||
* @param {Assertion<T>} assertion the assertion | ||
* @returns {Assertion<Array<T>>} an assertion that asserts an unknown value is an array with items of type `T` | ||
*/ | ||
function arrayOf<T>(assertion: Assertion<T>): Assertion<Array<T>> { | ||
return (val, path) => { | ||
const assertArray: Assertion<unknown[]> = array; | ||
assertArray(val, path); | ||
val.forEach((val, idx) => assertion(val, path.concat(`[${idx}]`))); | ||
}; | ||
} | ||
/** | ||
* Assert that `val` is of type `string[]`. | ||
* | ||
* @param {any} val value to assert | ||
* @param {string[]} path path to val (useful for nested assertions) | ||
*/ | ||
function arrayOfString(val: unknown, path: string[]): asserts val is string[] { | ||
const assertion: Assertion<string[]> = arrayOf(string); | ||
assertion(val, path); | ||
} | ||
/** | ||
* Construct an assertion that an unknown value is an array with items of type `T`, or `Nil` | ||
* | ||
* @template T the item type | ||
* @param {Assertion<T>} assertion the assertion | ||
* @returns {Assertion<Maybe<Array<T>>>} an assertion that asserts an unknown value is an array with | ||
* items of type `T`, or `Nil` | ||
*/ | ||
function maybeArrayOf<T>(assertion: Assertion<T>): Assertion<Maybe<Array<T>>> { | ||
return (val, path) => { | ||
const assertArrayOf: Assertion<Array<T>> = arrayOf(assertion); | ||
const assertMaybeArrayOf: Assertion<Array<T> | Nil> = makeMaybeAssertion(assertArrayOf); | ||
assertMaybeArrayOf(val, path); | ||
}; | ||
} | ||
/** | ||
* Construct an assertion that an unknown value is an instance of type `T` | ||
* | ||
* @template T the instance type | ||
* @param {string} typeName the name of type `T` | ||
* @param {Newable<T> | Extends<T>} ctor a constructor reference for type `T` | ||
* @returns {Assertion<T>} an assertion that asserts an unknown value is an instance of type `T` | ||
*/ | ||
function instanceOf<T>(typeName: string, ctor: Newable<T> | Extends<T>): Assertion<T> { | ||
return (val, path) => { | ||
condition(!isNil(val), path, 'must be defined', UndefinedError); | ||
condition(val instanceof ctor, path, `must be an instance of "${typeName}"`); | ||
}; | ||
} | ||
/** | ||
* Construct an assertion that an unknown value is an instance of type `T`, or `Nil` | ||
* | ||
* @template T the instance type | ||
* @param {string} typeName the name of type `T` | ||
* @param {Newable<T> | Extends<T>} ctor a constructor reference for type `T` | ||
* @returns {Assertion<Maybe<T>>} an assertion that asserts an unknown value is an instance of type `T`, or `Nil` | ||
*/ | ||
function maybeInstanceOf<T>(typeName: string, ctor: Newable<T> | Extends<T>): Assertion<Maybe<T>> { | ||
return (val, path) => { | ||
const assertInstanceOf: Assertion<T> = instanceOf(typeName, ctor); | ||
const assertMaybeInstanceOf: Assertion<T | Nil> = makeMaybeAssertion(assertInstanceOf); | ||
assertMaybeInstanceOf(val, path); | ||
}; | ||
} | ||
/** | ||
* Construct an assertion that an unknown value is of type `T`, likely a union type | ||
* | ||
* @template T the type, likely a union of other types | ||
* @param {Array<Test<T>>} tests a set of tests for type `T` | ||
* @returns {Assertion<T>} an assertion that asserts an unknown value is of type `T` | ||
*/ | ||
function oneOf<T>(...tests: Array<Test<T>>): Assertion<T> { | ||
return (val, path) => { | ||
condition(!isNil(val), path, 'must be defined', UndefinedError); | ||
if (!tests.some((test) => test(val))) { | ||
condition(false, path, 'is of wrong type'); | ||
} | ||
}; | ||
} | ||
/** | ||
* Construct an assertion that an unknown value is of type `T`, likely a union type, or `Nil` | ||
* | ||
* @template T the type, likely a union of other types | ||
* @param {Array<Test<T>>} tests a set of tests for type `T` | ||
* @returns {Assertion<Maybe<T>>} an assertion that asserts an unknown value is of type `T`, or `Nil` | ||
*/ | ||
function maybeOneOf<T>(...tests: Array<Test<T>>): Assertion<Maybe<T>> { | ||
return (val, path) => { | ||
const assertOneOf: Assertion<T> = oneOf(...tests); | ||
const assertMaybeOneOf: Assertion<T | Nil> = makeMaybeAssertion(assertOneOf); | ||
assertMaybeOneOf(val, path); | ||
}; | ||
} | ||
/** | ||
* **UNSAFE** | ||
* This assertion does not actually verify that `val` is of type `T`. It is useful as the first | ||
* line in a nested assertion so that remaining assertion calls can leverage helpful intellisense. | ||
* This method is only exported under the `unsafe` keyword as a constant reminder of this fact. | ||
* | ||
* @template T the type to cast `val` to, should extend `Dictionary<unknown>`, i.e. be itself an object | ||
* @param {any} val the unknown value | ||
* @param {string[]} path the accumulated assertion path | ||
*/ | ||
function castObjectAs<T>(val: unknown, path: string[]): asserts val is T { | ||
const assertWithCast: Assertion<T> = object; | ||
assertWithCast(val, path); | ||
} | ||
export const assert = { | ||
condition, | ||
any, | ||
maybeAny, | ||
array, | ||
maybeArray, | ||
boolean, | ||
maybeBoolean, | ||
date, | ||
maybeDate, | ||
dictionary, | ||
maybeDictionary, | ||
error, | ||
undefinedError, | ||
typeError, | ||
func, | ||
maybeFunc, | ||
nil, | ||
number, | ||
maybeNumber, | ||
object, | ||
maybeObject, | ||
string, | ||
maybeString, | ||
unknown, | ||
maybeUnknown, | ||
arrayOf, | ||
maybeArrayOf, | ||
arrayOfString, | ||
instanceOf, | ||
maybeInstanceOf, | ||
oneOf, | ||
maybeOneOf, | ||
// Some helpful, well, helpers | ||
fromTest: makeAssertion, | ||
makeMaybe: makeMaybeAssertion, | ||
makePartial: makePartialAssertion, | ||
toTest: makeTest, | ||
unsafe: { castObjectAs }, | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
19565
357
1