RangeObject
- class RangeObject(startOrObject, end, step=1, group)
An object which represents a range of values.
- Arguments:
startOrObject (number|object) – Beginning of range, or object literal with start and end properties.
end (number) – End of range
step (number) – Step size in range
group (number|undefined) – Groups in range
Examples:
<caption>With implicit step size = 1.</caption> let rangeObject = new RangeObject(0.5,3); rangeObject.toArray(); // [ 0.5, 1.5, 2.5 ]
<caption>With explicit step size.</caption> let rangeObject = new RangeObject(1,9,3); rangeObject.toArray(); // [ 1, 4, 7 ]
<caption>With grouping.</caption> let rangeObject = new RangeObject(1,9,3,2); rangeObject.toArray(); // [ [ 1, 4 ], [ 7 ] ]
Examples
With implicit step size = 1
const { RangeObject } = require('dcp/range-object');
const rangeObject = new RangeObject(0.5, 3);
rangeObject.toArray();
// [ 0.5, 1.5, 2.5 ]
With explicit step size
const { RangeObject } = require('dcp/range-object');
const rangeObject = new RangeObject(1, 9, 3);
rangeObject.toArray();
// [ 1, 4, 7 ]
With grouping
const { RangeObject } = require('dcp/range-object');
const rangeObject = new RangeObject(1, 9, 3, 2);
console.log(rangeObject.toArray());
// [ [ 1, 4 ], [ 7 ] ]
Attributes
length
- RangeObject.length
Number of elements in range, or number of groups if grouped.
Examples:
<caption>Without grouping</caption> let rangeObject = new RangeObject(1,10,3); rangeObject.toArray(); // [ 1, 4, 7, 10 ] rangeObject.length; // 4
<caption>With grouping</caption> let rangeObject = new RangeObject(1,9,3,2); rangeObject.toArray(); // [ [ 1, 4 ], [ 7 ] ] rangeObject.length; // 2
Methods
filter
slice
nthValue
- RangeObject.nthValue(n)
Return nth value in range
- Arguments:
n (number)
Examples:
let rangeObject = new RangeObject(1,10,3,2); rangeObject.toArray(); // [ [ 1, 4 ], [ 7, 10 ] ] rangeObject.nthValue(1); // [ 7, 10 ]
toString
- RangeObject.toString()
Create string representation of range: [object RangeObject start,end,step,group]
toObject
- RangeObject.toObject()
Create object literal for range with properties: start, end, step, and group.