Stepler
Define iteration rules for certain data structure and compute next step value from input data.
Basics
const data = value: 2 min: 0 max: 3 const opts = value // required. Get current value from your data structure. max // required min // optional, default to 0); const next = ; // => 3 optsstep = -1;data; // => 1
Step size
Default step size is 1, which can be changed:
const data = value: 1 min: 0 max: 3 const opts = value max step: 2); const next = ; // => 3
Formatting
const data = value: 2 min: 0 max: 3 labels: "a" "b" "c" "d" const opts = value max labelsidx ); const next = ; // => "d"
Overflow
By default, if new value will overflow defined bounds, an old value will be returned:
const data = value: 3 min: 0 max: 3 const opts = value max); const next = ; // => 3
This can be changed with overflow
option:
optsoveflow = steplerOVERFLOW_LOOP;data; // => 0 datavalue = 2;optsstep = 2 ;optsoveflow = steplerOVERFLOW_SNAP;data; // => 3 opts { // something custom};
N.B. Using function for overflow
will disable formatting. I.e. value returned from overflow function will be returned as-is from iterator function.
It is assumed that when you need custom logic for handling overflow – it's a kind of exception, so normal flow should be interrupted.
NOTE, that with OVERFLOW_LOOP
it returns 0
, not 1
(as you might thought, i.e. "current + step - max").
With this option it does not calculate remainder and does not take care about step size.
Just "if new value exceeds limit, start from the opposite end".
Lists
There is a helper to work with arrays. It searches for next allowed index and returns array value at that index.
const data = value: "c" labels: "a" "b" "c" "d" const opts = value labels // required. Get list to iterate over from your data structureconst next = stepler; // => "d"
min
and max
options are denied for list iterator. They are internally set to 0
and list.length - 1
respectively.
Fractional step size is also denied here, I'm sure you know why.
Custom matcher
const data = value: foo: "c" labels: bar: "a" bar: "b" bar: "c" bar: "d" const opts = value labels nextfoo === itembar;const next = stepler; // => { bar: "d" }
Two directions
const data = value: "c" labels: "a" "b" "c" "d" const opts = value labelsconst letters = steplerlist;lettersnextdata // => "d"letters // => "b" // there are separate options to handle overflow for paired iterator:letters = steplerlist;lettersnext...data value: "d" // => "a"letters // => "a"
If a single overflow
option is defined, it will be used for both direction. Combining overflow
and overflowForward
/ overflowBackward
is not allowed.
Negative step size is denied for paired iterators, to be sure that next
goes forward and prev
goes backward.
Constant and dynamic
All options, except of overflow
, can be defined either as functions or as contants.
So you can, for example, create iterator with dynamic step:
const opts = min: 0 max: 42 thissomeCurrentValue vconst next = ;;