isStringLengthBetween()

isStringLengthBetween()

Checks if any value is a string type or an instance of String by using isString() of length within the specified range.

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

Generic type variables

TypeextendsAnyString=string

A generic type variable Type constrained by AnyString indicates 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 minimum length of the provided value via the return type and the fixed shape of optional payload parameter of the provided callback function ResultCallback type.

Maxextendsnumber

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

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 isStringLength() function from which it captures its value.

Parameters

value: any

The value of any type to check.

min: Max

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

max: Max

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

callback: ResultCallback<any, { min: Min, max: Max } & 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.

The payload parameter of the callback function consists of the min and max properties given in parameters of the core function, and they can't be overwritten by the given payload parameter of the core 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 as the length of the supplied value, and generic type variable Type as the type of the supplied value.

Returns

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

Example usage

string type

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

const firstName = 'my first name';

// true; The return type `value is StringOfLength<0, 13, string>`
isStringLengthBetween(firstName, 0, 13);
// false; The return type `value is StringOfLength<0, 12, string>`
isStringLengthBetween(firstName, 0, 12);
v// false; The return type `value is StringOfLength<14, 28, string>`
isStringLengthBetween(firstName, 14, 28);
// true; The return type `value is StringOfLength<13, 13, string>`
isStringLengthBetween(firstName, 13, 13 ); 

String instance

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

const firstName = 'my first name';
const firstNameBox = new String(firstName);

// true; The return type `value is StringOfLength<0, 13, string>`
isStringLengthBetween(firstNameBox, 0, 13);
// false; The return type `value is StringOfLength<0, 12, string>`
isStringLengthBetween(firstNameBox, 0, 12);
// false; The return type `value is StringOfLength<14, 28, string>`
isStringLengthBetween(firstNameBox, 14, 28);
// true; The return type `value is StringOfLength<13, 13, string>`
isStringLengthBetween(firstNameBox, 13, 13);

Last updated