static isRange()

Checks whether the value is an instance of `Range` of any or the given minimum, maximum, and step

Range.isRange()

The static isRange() method checks whether the value is an instance of Range of any or the given minimum, maximum range and step.

range.class.ts
public static isRange<
  Min extends number,
  Max extends number,
  Step extends number
>(
  value: any,
  min?: Min,
  max?: Max,
  step?: Step
): value is Range<Min, Max, Step> {
  return typeof value === 'object' && value instanceof this
    ? (typeof min === 'number' ? value.min === min : true) &&
        (typeof max === 'number' ? value.max === max : true) &&
        (typeof step === 'number' ? value.step === step : true)
    : false;
}

Generic type variables

Minextendsnumber

A generic type variable constrained by the number, by default of the value captured from the supplied min indicates the minimum range type via the return type.

Maxextendsnumber

A generic type variable constrained by the number, by default of the value captured from the supplied max indicates the maximum range type via the return type.

Stepextendsnumber

A generic type variable constrained by the number, by default of the value equal to 1, optionally captured from the supplied step indicates the range step type via return type.

Parameters

value:any

The value of any type to test against the Range instance.

min?:Min

The optional minimum range of generic type variable Min to check whether it's equal to a minimum of the given value.

max?:Max

The optional maximum range of generic type variable Max to check whether it's equal to a maximum of the given value.

step?:Step

Optional step of generic type variable Step to check whether it's equal to the step of the given value.

Return type

value isRange<Min,Max,Step>

The return type is a boolean resulting from its statement indicating the value is the Range object that takes the generic type variable Min, Max and Step.

Returns

The return value is a boolean indicating whether the provided value is an instance of Range of any or the given minimum, maximum range and step.

Example usage

// Example usage.
import { Range } from '@angular-package/range';

// Create new instance.
const range = new Range(4, 27, 1.5);

// Returns true of type value is Range<number, number, number>
Range.isRange(range);

// Returns true of type value is Range<4, number, number>
Range.isRange(range, 4);

// Returns true of type value is Range<4, 27, number>
Range.isRange(range, 4, 27);

// Returns true of type value is Range<4, 27, 1.5>
Range.isRange(range, 4, 27, 1.5);

// Returns false of type value is Range<3, number, number>
Range.isRange(range, 3);

// Returns false of type value is Range<3, 26, number>
Range.isRange(range, 3, 26);

// Returns false of type value is Range<3, 26, 1.0>
Range.isRange(range, 3, 26, 1.0);

// Returns true of type value is Range<number, 27, number>
Range.isRange(range, undefined, 27);

// Returns true of type value is Range<number, number, 1.5>
Range.isRange(range, undefined, undefined, 1.5);

Last updated