ts-predicate v1.1.4
ts-predicate
Getting started
yarn add ts-predicate
# or
npm install ts-predicateDocumentation
TypeAssertion
IsDefined
static IsDefined(value: unknown): voidAsserts that the value is not nullable, nor NaN.
IsBoolean
static IsBoolean(value: unknown): voidAsserts that the value is a boolean.
IsNumber
static IsNumber(value: unknown): voidAsserts that the value is a number, but not NaN.
IsInteger
static IsInteger(value: unknown): voidAsserts that the value is a safe integer.
IsFiniteNumber
static IsFiniteNumber(value: unknown): voidAsserts that the value is a number, but not NaN nor +/-Infinity.
IsString
static IsString(value: unknown, constraints?: StringConstraints): voidAsserts that the value is a string.
The optional parameter constraints accept an object described by the following interface.
interface StringConstraints
{
minLength?: number;
maxLength?: number;
}If minLength is provided, it'll asserts that the value has at least this length.
If maxLength is provided, it'll asserts that the value has at most this length.
IsFilledString
static IsFilledString(value: unknown): voidLike IsString, but asserts that the string is never empty too.
IsArray
static IsArray(value: unknown, constraints?: ArrayConstraints): voidAsserts that the value is an array.
The optional parameter constraints accept an object described by the following interface.
interface ArrayConstraints<T>
{
minLength?: number;
itemGuard?: (item: unknown) => item is T;
}If minLength is provided, it'll asserts that the value has at least that many items.
If itemGuard is provided, it'll asserts that the predicate hold true for every item.
IsPopulatedArray
static IsPopulatedArray(value: unknown, constraints?: ArrayConstraints): voidLike IsArray, but asserts that the array is never empty too.
IsRecord
static IsRecord(value: unknown): voidAsserts that the value is a record: an object with no prototype, or directly using Object prototype.
IsObject
static IsObject(value: unknown): voidAsserts that the value is an object.
IsFunction
static IsFunction(value: unknown): voidAsserts that the value is a function, generator function, method, or class.
IsCallable
static IsCallable(value: unknown): voidAsserts that the value is not constructible.
HasNullableProperty
static HasNullableProperty(value: object, property: string): voidAsserts that the value is an object with the property defined, though it may be nullish or NaN.
HasProperty
static HasProperty(value: object, property: string): voidAsserts that the value is an object with the property defined.
TypeGuard
IsPrimitive
static IsPrimitive(value: unknown): booleanNarrow down the value to being nullish, NaN, a boolean, a number, or a string.
IsDefined
static IsDefined(value: unknown): booleanNarrow down the value to being not nullable, nor NaN.
IsBoolean
static IsBoolean(value: unknown): booleanNarrow down the value to being a boolean.
IsNumber
static IsNumber(value: unknown): booleanNarrow down the value to being a number, but not NaN.
IsInteger
static IsInteger(value: unknown): booleanNarrow down the value to being a safe integer.
IsFiniteNumber
static IsFiniteNumber(value: unknown): booleanNarrow down the value to being a number, but not NaN nor +/-Infinity.
IsString
static IsString(value: unknown): booleanNarrow down the value to being a string.
IsFilledString
static IsFilledString(value: unknown): voidAsserts that the value is a non empty string.
IsArray
static IsArray(value: unknown, constraints?: ArrayConstraints): booleanNarrow down the value to being an array.
The optional parameter constraints accept an object described by the following interface.
interface ArrayConstraints<T>
{
minLength?: number;
itemGuard?: (item: unknown) => item is T;
}If minLength is provided, it'll confirm that the value has at least that many items.
If itemGuard is provided, it'll confirm that the predicate hold true for every item.
IsPopulatedArray
static IsPopulatedArray(value: unknown, constraints?: ArrayConstraints): booleanLike IsArray, but narrow it to being a populated array.
IsRecord
static IsRecord(value: unknown): booleanNarrow down the value to being a record: an object with no prototype, or directly using Object prototype.
IsObject
static IsObject(value: unknown): booleanNarrow down the value to being an object.
IsFunction
static IsFunction(value: unknown): booleanNarrow down the value to being a function, generator function, method, or class.
IsCallable
static IsCallable(value: unknown): booleanNarrow down the value to being not constructible.
HasNullableProperty
static HasNullableProperty(value: object, property: string): booleanNarrow down the value to being an object with the property defined, though it may be nullish or NaN.
HasProperty
static HasProperty(value: object, property: string): booleanNarrow down the value to being an object with the property defined.
TypeHint
GetBaseType
static GetBaseType(value: any): stringReturn a string depending on the type of the given value.
Possible values:
- undefined
- null
- NaN
- boolean
- number
- string
- array
- object
- function
- generator
- class
Note: generator refers to a generator function.
GetDetailedType
static GetDetailedType(value: any): stringReturn a string depending on the type of the given value.
Provide more details than GetBaseType.
Possible values:
- undefined
- null
- NaN
- boolean (true or false)
- number (N)
- string (N characters)
- array (N items)
- anonymous object
- object anonymous class
- object ClassName
- anonymous function
- function name
- anonymous generator
- generator name
- anonymous class
- class Name
Note: generator refers to a generator function.
GetName
static GetName(value: any): string|undefinedIf given a function, generator function, method, or class, return its name. If given an object, return its constructor name. If the given value doesn't have a name, return an empty string. For any other value, return undefined.
ValueGuard
IsSimilar
static IsSimilar(a: any, b: any): booleanReturn true in the following cases :
- The same value has been passed as both arguments.
- Similar primitive values have been passed as arguments.
- Deeply similar arrays or records have been passed as arguments.
Otherwise, return false.