CommonType

Common methods and helpers to implement the RangeType interface

Methods

(static) compare()

Basic implementation of RangeType.compare. This uses the numeric difference for distance, and the sign of that value for side. The inputs are coerced to numbers prior to calculation.

This isn't useful on its own (technically a normalized, real type) but can be used to build other types.

(static) compareBinarySearch(type, subclassopt) → {RangeType}

Simple decorator that forces the RangeType.compare method to opt in to binary search, rather than interpolation search. It does this by forcing non-zero distance to have the same magnitude.

Parameters:
NameTypeAttributesDefaultDescription
typeRangeType

the type whose RangeType.compare method should be wrapped

subclassboolean<optional>
true

if true, a new RangeType is created that extends type; if false, the RangeType.compare method of type is simply overwritten

Returns:

depending on the value of extend, either type or a new type that inherits from type

Type: 
RangeType

(static) compareEpsilon(epsilon, type, subclassopt) → {RangeType}

Simple decorator that adds fuzzy comparison to an existing RangeType. This causes ranges to be merged if their endpoints are within epsilon distance. It can be especially useful if using RealType or FloatNormType, where imprecision in floating point arithmetic can mean adjacent ranges don't quite line up.

Parameters:
NameTypeAttributesDefaultDescription
epsilonnumber

the absolute threshold at which RangeType~CompareResult distance is clamped to zero

typeRangeType

the type whose RangeType.compare method should be wrapped

subclassboolean<optional>
true

if true, a new RangeType is created that extends type; if false, the RangeType.compare method of type is simply overwritten

Returns:

depending on the value of extend, either type or a new type that inherits from type

Type: 
RangeType

(static) copy()

Basic implementation of RangeType.copy. This uses Object.assign to copy the object

(static) create(start, end, startExclnullable, endExclnullable) → {Range}

Basic implementation of RangeType.create. This uses RangeType.setStart and RangeType.setEnd to initialize the range.

Parameters:
NameTypeAttributesDescription
startany

starting bound

endany

ending bound

startExclboolean<nullable>

whether starting bound is exclusive

endExclboolean<nullable>

whether ending bound is exclusive

Returns:
Type: 
Range

(static) setEnd()

Basic implementation of RangeType.setEnd. This deletes Range#endExcl, rather than set it to false

(static) setStart()

Basic implementation of RangeType.setStart. This deletes Range#startExcl, rather than set it to false

(static) size()

Basic implementation of RangeType.size. This simply takes the difference in Range#end and Range#start, with values coerced to numbers. This is suitable for continuous types, but not for discrete.