If the start bound of a range more than the end bound, the created range will enumerate elements by descending order.
This behavior works well with any range.
It's possible to create an infinite range. Just skip one or both bound the creating the range.
importRangefrom'core/range';
// Range from 0 to Infinity // 0.. console.log(newRange(0).toString());
// Range from -Infinity to a // ..a console.log(newRange(null, 'a').toString());
// Range from -Infinity to Infinity // .. console.log(newRange().toArray());
Mind, you can't transform infinite ranges to arrays by invoking toArray, but you free to use iterators.
Physically, the number and date ranges start from Number.MIN_SAFE_INTEGER to Number.Max_SAFE_INTEGER.
The string rages start from \0 to the last Unicode code point.
importRangefrom'core/range';
// Range from 0 to Infinity // 0.. console.log(newRange(0).toString());
All ranges support three kinds of iterators.
Each kind of iterators can take a step value to iterate elements (for date ranges it means milliseconds to shift).
By values (used by default).
importRangefrom'core/range';
for (constelofnewRange('a', 'c')) { // 'a' 'b' 'c' console.log(el); }
The method 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.
The method 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.
The method creates an array from the range and returns it.
Also, it can take a step to iterate elements (for date ranges, it means milliseconds to shift).
Mind, you can't transform infinite ranges to arrays, but you free to use iterators.
core/range
This module provides a class to create a range structure.
Supported ranges
The ranges can be different: numbers, characters, dates.
number
string
date
Excluding the range bounds
By default, all ranges include their bounds, but you free to change this behavior. Just wrap bounds by an array.
Reversed ranges
If the start bound of a range more than the end bound, the created range will enumerate elements by descending order. This behavior works well with any range.
Infinite ranges
It's possible to create an infinite range. Just skip one or both bound the creating the range.
Mind, you can't transform infinite ranges to arrays by invoking
toArray
, but you free to use iterators. Physically, the number and date ranges start fromNumber.MIN_SAFE_INTEGER
toNumber.Max_SAFE_INTEGER
. The string rages start from\0
to the last Unicode code point.Iterators
All ranges support three kinds of iterators. Each kind of iterators can take a step value to iterate elements (for date ranges it means milliseconds to shift).
API
Ranges support a bunch of methods to work with them.
contains
The method returns true if an element is contained inside the range (the element can be a simple value or another range).
intersect
The method 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.
union
The method 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.
clone
The method clones the range and returns a cloned one.
reverse
The method clones the range with reversing of element ordering and returns a new one.
clamp
The method clamps an element to be within the range if it falls outside. If the range is invalid or empty, the method always returns
null
.span
The method 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
.toArray
The method creates an array from the range and returns it. Also, it can take a step to iterate elements (for date ranges, it means milliseconds to shift). Mind, you can't transform infinite ranges to arrays, but you free to use iterators.
toString
The method creates a string from the range and returns it. If the range invalid or empty, the method always returns an empty string.