arraynge
Various transforms for ranges of array/list indices.
Usage
see test/usage.js
API
Import it
var arraynge = require('arraynge');
Create arraynges
range = arraynge();
range = arraynge(from, upto);
range = arraynge(from, upto, list);
makeRangeFromList = arraynge.withThisList.bind(list);
range = makeRangeFromList(from, upto);
list.makeRange = arraynge.withThisList;
range = list.makeRange(from, upto);
Configure an arraynge
The config functions return the arraynge they were called on,
so you can chain them.
range.bounds(newFirstIndex, newLastIndex);
range.from(newFirstIndex);
range.upto(newLastIndex);
range.list(null || false);
range.list(someObject);
range.list(someNumber);
Query the config
range.toString();
range.bounds();
range.from();
range.upto();
range.list();
range.len();
Modify the bounds
Parameter max
or len
is optional. If provided, it should be a number.
(Maximum valid integer index for your list, or the list's length.)
If not provided or undefined
, it is calculated from .len()
.
range.trueMax(max);
range.warpZero(len);
range.modulo(len);
range.confine(max);
range.validateBounds(max);
range.translate(bothDist);
range.translate(fromDist, lastDist);
Cautious transforms
- They won't modify the config.
- If any of the bounds is not a finite number, or is out of range,
the cautious transforms will abort without any action or
side effect and will return
false
.
- Parameters
len
or max
optional as above.
range.boundsIfValid(max);
range.enumerate();
range.map(iter);
License
ISC