5.0.0-rc.0

isStringLength()

isStringLength()

Checks if any value is a string type or an instance of String(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 string type of the given value via the return type.

Lengthextendsnumber

A generic type variable Length constrained by the number 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 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.

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.

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 object 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 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 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 boolean indicating whether the provided value is a string type or an instance of String of the specified length.

Example usage

string type

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

const firstName = 'my first name';

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

String instance

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

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

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

Last updated