5.0.0-rc.0

guardStringLengthBetween()

guardStringLengthBetween()

Guards the value to be string or String instance of a length between the specified range.

guard-string-length-between.func.ts
const guardStringLengthBetween = <
  Type extends AnyString,
  Min extends number,
  Max extends number,
  Payload extends object = object
>(
  value: Type,
  min: Min,
  max: Max,
  callback?: ResultCallback<Type, { min: Min; max: Max } & Payload>,
  payload?: Payload
): value is StringOfLength<Min, Max, Type> =>
  isStringLengthBetween(value, min, max, callback, payload);

Generic type variables

TypeextendsAnyString

A generic type variable Obj constrained by AnyString indicates captured string type of the given value via the return type and the value parameter of the provided callback function ResultCallback type.

Minextendsnumber

A generic type variable Min constrained by the number type, by default of value captured from the supplied min indicates the payload parameter type of the provided callback function ResultCallback type and the minimum length of the provided value via the return type.

Maxextendsnumber

A generic type variable Max constrained by the number type, by default of value captured from the supplied max indicates the payload parameter type of the provided callback function ResultCallback type and the maximum length of the provided value via the return type.

Payloadextendsobject=object

The Payload generic type variable constrained by object indicates the type of optional parameter payload of the supplied callback function and payload optional parameter of the guardStringLengthBetween() function from which it captures its value.

Parameters

value: Type

The value of a generic type variable Type constrained by AnyString, by default of the type captured from itself to guard.

min: Max

The minimum length of generic type variable Min of a given value.

max: Max

The maximum length of generic type variable Max of a given value

callback?: ResultCallback<Type, { min: Min; max: Max } & Payload>

The optional 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.

The payload parameter of given callback function consists of the min and max properties of the given min and max parameters, and they can't be overwritten by the given payload parameter of the main function.

payload?: Payload

An optional object of the generic type variable Payload is assigned to the payload of the given callback function.

Return type

value is StringOfLength<Min, Max, Type>

The return type is a boolean as the result of its statement, indicating the value is a generic type StringOfLength that takes generic type variables Min and Max(from the provided min and max parameter) as the length range of the supplied value, and Type as the type of the supplied value.

Returns

The return value is a boolean indicating whether the value is a string type or an instance of String of a length between the specified range.

Example usage

// Example usage.
import { guardStringLengthBetween } from '@angular-package/type';

// true; return type `value is StringOfLength<11, 12, "not my name">`
guardStringLengthBetween('not my name', 11, 12);
// true; return type `value is StringOfLength<10, 11, "not my name">`
guardStringLengthBetween('not my name', 10, 11);
// true; return type `value is StringOfLength<11, 11, "not my name">`
guardStringLengthBetween('not my name', 11, 11);
// false; return type `value is StringOfLength<12, 15, "not my name">`
guardStringLengthBetween('not my name', 12, 15);

// Long text for the captured value type.
const value = `Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting,
remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset
sheets containing Lorem Ipsum passages, and more recently with desktop publishing software
like Aldus PageMaker including versions of Lorem Ipsum.` as string;

// false, value is StringOfLength<0, 3, string>
guardStringLengthBetween(value, 0, 3); 

Last updated