# isStringLengthBetween()

## `isStringLengthBetween()`

Checks if [`any`](https://www.typescriptlang.org/docs/handbook/basic-types.html#any) value is a [`string`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) type or an instance of [`String`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) by using [`isString()`](https://docs.angular-package.dev/type/is/isstring) of length within the specified range.

{% code title="is-string-length-between.func.ts" %}

```typescript
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
  );
```

{% endcode %}

### Generic type variables

#### <mark style="color:green;">**`Type`**</mark>**`extends`**<mark style="color:green;">**`AnyString`**</mark>**`=`**<mark style="color:green;">**`string`**</mark>

A generic type variable `Type` constrained by [`AnyString`](https://docs.angular-package.dev/type/type/anystring) indicates [`string`](https://www.typescriptlang.org/docs/handbook/basic-types.html#string) type of the given [`value`](#value-any) via the [return type](#return-type) and the [`value`](https://docs.angular-package.dev/type/type/resultcallback#value-value) parameter of the provided [`callback`](#callback-resultcallback-less-than-any-min-min-max-max-and-payload-greater-than) function [`ResultCallback`](https://docs.angular-package.dev/type/type/resultcallback) type.

#### <mark style="color:green;">**`Min`**</mark>**`extends`**<mark style="color:green;">**`number`**</mark>

A generic type variable `Min` constrained by the [`number`](https://www.typescriptlang.org/docs/handbook/basic-types.html#number) type, by default of value captured from the supplied [`min`](#min-max) indicates the **minimum** length of the provided [`value`](#value-any) via the [return type](#return-type) and the fixed shape of optional [`payload`](https://docs.angular-package.dev/type/type/resultcallback#payload-payload) parameter of the provided [`callback`](#callback-resultcallback-less-than-type-payload-greater-than) function [`ResultCallback`](https://docs.angular-package.dev/type/type/resultcallback) type.

#### <mark style="color:green;">**`Max`**</mark>**`extends`**<mark style="color:green;">**`number`**</mark>

A generic type variable `Max` constrained by the [`number`](https://www.typescriptlang.org/docs/handbook/basic-types.html#number) type, by default of value captured from the supplied [`max`](#max-max) indicates the **maximum** length of the provided [`value`](#value-any) via the [return type](#return-type) and the fixed shape of optional [`payload`](https://docs.angular-package.dev/type/type/resultcallback#payload-payload) parameter of the provided [`callback`](#callback-resultcallback-less-than-type-payload-greater-than) function [`ResultCallback`](https://docs.angular-package.dev/type/type/resultcallback).&#x20;

#### <mark style="color:green;">**`Payload`**</mark>**`extends`**<mark style="color:green;">**`object`**</mark>**`=`**<mark style="color:green;">**`object`**</mark>

The `Payload` generic type variable constrained by [`object`](https://www.typescriptlang.org/docs/handbook/basic-types.html#object) indicates the type of optional parameter [`payload`](https://docs.angular-package.dev/type/type/resultcallback#payload-payload) of the supplied [`callback`](#callback-resultcallback-less-than-any-min-min-max-max-and-payload-greater-than) function and [`payload`](#payload-payload) optional parameter of the [`isStringLength()`](#isstringlength) function from which it captures its value.

### Parameters

#### `value: any`

The value of [`any`](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any) type to check.

#### `min: Max`

The **minimum** length of generic type variable [`Min`](#minextendsnumber) for a given [`value`](#value-any).

#### `max: Max`

The **maximum** length of generic type variable [`Max`](#maxextendsnumber) for a given [`value`](#value-any).

#### `callback: ResultCallback<any, { min: Min, max: Max } & Payload>`

A callback `function` of [`ResultCallback`](https://docs.angular-package.dev/type/type/resultcallback) type with parameters, the [`value`](#value-any) that has been checked, the [`result`](https://docs.angular-package.dev/type/type/resultcallback#result-boolean) of this check, and [`payload`](https://docs.angular-package.dev/type/type/resultcallback#payload-payload) of generic type variable [`Payload`](#payloadextendsobject-object) with optional properties from the provided [`payload`](#payload-payload), to handle them before the [`result`](https://docs.angular-package.dev/type/type/resultcallback#result-boolean) return. By default, it uses [`resultCallback()`](https://docs.angular-package.dev/type/helper/resultcallback) function.

{% hint style="info" %}
The [`payload`](https://docs.angular-package.dev/type/type/resultcallback#payload-payload) parameter of the [`callback`](#callback-resultcallback-less-than-any-min-min-max-max-and-payload-greater-than) function consists of the [`min`](#min-max) and [`max`](#max-max) properties given in parameters of the core function, and they can't be overwritten by the given [`payload`](#payload-payload) parameter of the core function.
{% endhint %}

#### `payload?: Payload`

An optional [`object`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) of the generic type variable [`Payload`](#payloadextendsobject-object) is assigned to the [`payload`](https://docs.angular-package.dev/type/type/resultcallback#payload-payload) of the given [`callback`](#callback-resultcallback-less-than-any-payload-greater-than) function.

### Return type

#### `value is StringOfLength<Min, Max, Type>`

The **return type** is a [`boolean`](https://www.typescriptlang.org/docs/handbook/basic-types.html#boolean) as the result of its statement indicating the [`value`](#value-any) is a generic type [`StringOfLength`](https://docs.angular-package.dev/type/type/stringoflength) that takes generic type variables [`Min`](#minextendsnumber) and [`Max`](#maxextendsnumber) as the **length** of the supplied [`value`](#value-any), and generic type variable [`Type`](#typeextendsanystring) as the type of the supplied [`value`](#value-any).

### Returns

The **return value** is a [`boolean`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean) indicating whether the provided [`value`](#value-any) is a [`string`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) type or an instance of [`String`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) of length within the specified range.

## Example usage

### `string` type

```typescript
// 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

```typescript
// 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);
```
