Options
All
  • Public
  • Public/Protected
  • All
Menu

A class to create a range with the specified type. The class supports ranges of numbers, strings, and dates.

Type parameters

Hierarchy

  • default

Index

Constructors

  • example
    // [0, 1, 2, 3]
    console.log(new Range(0, 3).toArray());

    // [0, 1, 2]
    console.log(new Range(0, [3]).toArray());

    // ['b', 'c']
    console.log(new Range(['a'], ['d']).toArray());

    // []
    console.log(new Range('a', ['a']).toArray());

    // 'Wed Oct 18 1989 00:00:00..'
    console.log(new Range(new Date(1989, 9, 18)).string());

    // '..Wed Oct 18 1989 00:00:00'
    console.log(new Range(null, new Date(1989, 9, 18)).string());

    Type parameters

    Parameters

    • start: number | T[] | Nullable<T> = -Infinity
    • end: number | T[] | Nullable<T> = Infinity

    Returns default<T>

Properties

end: number

Top bound

isReversed: boolean = false

True if the range is reversed

start: number

Bottom bound

type: RangeType

Range type

Methods

  • [iterator](): IterableIterator<T>
  • clamp(el: unknown): null | T
  • Clamps an element to be within the range if it falls outside. If the range is invalid or empty, the method always returns null.

    example
    // 3
    console.log(new Range(0, 10).clamp(3));

    // 'd'
    console.log(new Range('a', 'd').clamp('z'));

    // null
    console.log(new Range(0, [0]).clamp(10));

    Parameters

    • el: unknown

    Returns null | T

  • contains(el: unknown): boolean
  • Returns true if the specified element is contained inside the range (the element can be a simple value or another range)

    example
    // true
    console.log(new Range(0, 10).contains(4));

    // false
    console.log(new Range(0, [10]).contains(10));

    // false
    console.log(new Range(0, 10).contains(12));

    // false
    console.log(new Range(0, 10).contains('a'));

    // true
    console.log(new Range(0, 10).contains(Range(3, 6)));

    // false
    console.log(new Range(0, 10).contains(Range(3, 16)));

    // false
    console.log(new Range(0, 10).contains(Range('a', 'b')));

    Parameters

    • el: unknown

    Returns boolean

  • entries(step?: number): IterableIterator<[number, T]>
  • Returns an iterator from the range that produces pairs of iteration indices and values

    example
    for (const el of new Range(3, 1).entries()) {
    // [0, 3] [1, 2] [2 3]
    console.log(el);
    }

    for (const el of new Range(0, 3).entries(2)) {
    // [0, 0] [1, 2]
    console.log(el);
    }

    Parameters

    • Optional step: number

    Returns IterableIterator<[number, T]>

  • indices(step?: number): IterableIterator<number>
  • Returns an iterator from the range that produces iteration indices

    example
    for (const el of new Range(3, 1).indices()) {
    // 0 1 2
    console.log(el);
    }

    for (const el of new Range(0, 3).indices(2)) {
    // 0 1
    console.log(el);
    }

    Parameters

    • Optional step: number

    Returns IterableIterator<number>

  • Returns a new range with the latest starting point as its start, and the earliest ending point as its end. If the two ranges do not intersect, this will effectively produce an empty range.

    The method preserves element ordering of the first range. The intersection of ranges with different types will always produce an empty range.

    example
    // 8..10
    console.log(new Range(0, 10).intersect(new Range([7], 14)).toString());

    // 10..7
    console.log(new Range(10, 0).intersect(new Range(7, 14)).toString());

    // 7..10
    console.log(new Range(0, 10).intersect(new Range(7)).toString());

    // 7..
    console.log(new Range(0).intersect(new Range(7)).toString());

    // ''
    console.log(new Range(0, 10).intersect(new Range(11, 14)).toString());

    // ''
    console.log(new Range(0, 10).intersect(new Range('a', 'z')).toString());

    Parameters

    • range: default<T extends string ? string : T>

    Returns default<T>

  • isValid(): boolean
  • Returns true if the range is valid

    example
    console.log(new Range('a', {}).isValid() === false);

    console.log(new Range(new Date('boom!')).isValid() === false);

    // The empty range is not valid
    console.log(new Range([0], [0]).isValid() === false);

    Returns boolean

  • Clones the range with reversing of element ordering and returns a new

    example
    // [3, 2, 1, 0]
    console.log(new Range(0, 3).reverse().toArray());

    Returns default<T>

  • span(): number
  • Returns a span of the range. The span includes both the start and the end.

    If the range is a date range, the value is in milliseconds. If the range is invalid or empty, the method always returns 0.

    example
    // 4
    console.log(new Range(7, 10).span());

    // 0
    console.log(new Range(0, [0]).span());

    Returns number

  • toArray(step?: number): T[]
  • Creates an array from the range and returns it. Mind, you can't transform infinite ranges to arrays, but you free to use iterators.

    example
    // [0, 3, 6, 9]
    console.log(new Range(0, 10).toArray(3));

    // ['a', 'b']
    console.log(new Range('a', ['c']).toArray());

    // []
    console.log(new Range(0, [0]).toArray());

    Parameters

    • Optional step: number

    Returns T[]

  • toString(): string
  • Creates a string from the range and returns it. If the range invalid or empty, the method always returns an empty string.

    example
    // 0..10
    console.log(new Range(0, 10).toString());

    // 0..9
    console.log(new Range(0, [10]).toString());

    // 0..
    console.log(new Range(0).toString());

    // ..z
    console.log(new Range(null, 'z').toString());

    // ''
    console.log(new Range(0, [0]).toString());

    Returns string

  • toType(value: number): T
  • Converts a value to the real range type

    example
    // j
    console.log(new Range('a', 'z).toType(106));

    Parameters

    • value: number

    Returns T

  • Returns a new range with the earliest starting point as its start, and the latest ending point as its end. If the two ranges do not intersect, this will effectively remove the "gap" between them.

    The method preserves element ordering of the first range. The union of ranges with different types will always produce an empty range.

    example
    // 0..13
    console.log(new Range(0, 10).union(new Range(7, [14])).toString());

    // 14..0
    console.log(new Range(10, 0).union(new Range(7, 14)).toString());

    // 0..
    console.log(new Range(0, 10).union(new Range(7)).toString());

    // ..
    console.log(new Range().union(new Range(7)).toString());

    // ''
    console.log(new Range(0, 10).union(new Range('a', 'z')).toString());

    Parameters

    • range: default<T extends string ? string : T>

    Returns default<T>

  • values(step?: number): IterableIterator<T>
  • Returns an iterator from the range

    example
    for (const el of new Range(0, 3).values()) {
    // 0 1 2 3
    console.log(el);
    }

    for (const el of new Range(0, 3).values(2)) {
    // 0 2
    console.log(el);
    }

    Parameters

    • Optional step: number

    Returns IterableIterator<T>