isStringIncludes()

isStringIncludes()

Checks if any value is a string type or an instance of String(by using isString()) that includes all of the specified words/sentences.

is-string-includes.func.ts
const isStringIncludes = <
  Type extends AnyString = string,
  Payload extends object = object
>(
  value: any,
  includes: string[],
  callback: ResultCallback<
    any,
    { includes: typeof includes } & Payload
  > = resultCallback,
  payload?: Payload
): value is Type =>
  callback(
    isString(value)
      ? isArray(includes)
        ? includes.every((include) => value.valueOf().includes(include))
        : false
      : false,
    value,
    { ...payload, includes } as any
  );

Generic type variables

TypeextendsAnyString=string

A generic type variable Type constrained by generic type AnyString indicates the string type of the given value via the return type.

Payloadextendsobject=object

Parameters

value: any

The value of any type to check against the string that contains words/sentences from a given includes.

includes: string[]

An Array of string as words/sentences to be case-sensitive searched for within the given value.

callback: ResultCallback<any, { includes: typeof includes } & Payload>

payload?: Payload

Return type

value is Type

The return type is a boolean as the result of its statement indicating the value is a generic type variable Type by default equal to the string.

Returns

The return value is a boolean indicating whether the provided value is a string type or an instance of String that includes all of the specified words/sentences.

Example usage

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

isStringIncludes('This is a person without age.', ['age']); // true; The return type `value is string`
isStringIncludes('This is a person without age.', ['Person']); // false; The return type `value is string`
isStringIncludes('This is a person without age.', ['age', 'Person']); // false; The return type `value is string`
isStringIncludes(new String('This is artificial intelligence.'), [
  'artificial',
  'intelligence',
]); // true; The return type `value is string`

Last updated

Was this helpful?