isStringLength()

isStringLength()

Checks if anyarrow-up-right value is a stringarrow-up-right type or an instance of Stringarrow-up-right(by using isString()) of a specified length.

is-string-length.func.ts
const isStringLength = <
  Type extends AnyString = string,
  Length extends number = number,
  Payload extends object = object
>(
  value: any,
  length: Length,
  callback: ResultCallback<any, { length: Length } & Payload> = resultCallback,
  payload?: Payload
): value is StringOfLength<Length, Length, Type> =>
  callback(
    isString(value) && isNumberType(length) && length > 0
      ? value.valueOf().length === length
      : false,
    value,
    { ...payload, length } as any
  );

Generic type variables

TypeextendsAnyString=string

A generic type variable Type constrained by AnyString indicates the stringarrow-up-right type of the given value via the return type.

Lengthextendsnumber

A generic type variable Length constrained by the numberarrow-up-right type, by default of value captured from the supplied length indicates the length of the provided value via the return type and the fixed shape of optional payload parameter of the provided callback function.

Payloadextendsobject=object

The Payload generic type variable constrained by objectarrow-up-right indicates the type of optional parameter payload of the supplied callback function and payload optional parameter of the isStringLength() function from which it captures its value.

Parameters

value: any

The value of anyarrow-up-right type to check.

length: Length

The length of generic type variable Length of a given value.

callback: ResultCallback<any, { length: Length } & Payload>

A callback function of ResultCallback type with parameters, the value that has been checked, the result of this check, and payload of generic type variable Payload with optional properties from the provided payload, to handle them before the result return. By default, it uses resultCallback() function.

circle-info

The payload parameter of the callback function consists of the length property given in parameter of the core function, and it can't be overwritten by the given payload parameter of the core function.

payload?: Payload

An optional objectarrow-up-right of the generic type variable Payload is assigned to the payload of the given callback function.

Return type

value is StringOfLength<Length, Length, Type>

The return type is a booleanarrow-up-right as the result of its statement indicating the value is a generic type StringOfLength that takes generic type variables Min and Max from the generic type variable Length as the length of the supplied value, and generic type variable Type as the type of the provided value.

Returns

The return value is a booleanarrow-up-right indicating whether the provided value is a stringarrow-up-right type or an instance of Stringarrow-up-right of the specified length.

Example usage

string type

String instance

Last updated