add Ox.nextValue
This commit is contained in:
parent
a4ab0c1eb3
commit
c4d2bb7a3d
1 changed files with 29 additions and 0 deletions
|
@ -568,6 +568,35 @@ Ox.makeArray = function(value) {
|
|||
return ret;
|
||||
};
|
||||
|
||||
/*@
|
||||
Ox.nextValue <f> Next value, given an array of numbers, a number and a direction
|
||||
(array, value, direction) -> <n> Next value
|
||||
array <[n]> Sorted array of numbers
|
||||
value <n> Number
|
||||
direction <n|1> Direction (-1 for left or 1 for right)
|
||||
> Ox.nextValue([0, 2, 4, 6, 8], 5, -1)
|
||||
4
|
||||
> Ox.nextValue([], 1, 1)
|
||||
void 0
|
||||
@*/
|
||||
Ox.nextValue = function(array, value, direction) {
|
||||
var found = false, nextValue;
|
||||
direction = direction || 1;
|
||||
direction == -1 && array.reverse();
|
||||
Ox.forEach(array, function(v) {
|
||||
if (direction == 1 ? v > value : v < value) {
|
||||
nextValue = v;
|
||||
found = true;
|
||||
return false; // break
|
||||
}
|
||||
});
|
||||
direction == -1 && array.reverse();
|
||||
if (!found) {
|
||||
nextValue = array[direction == 1 ? 0 : array.length - 1];
|
||||
}
|
||||
return nextValue;
|
||||
};
|
||||
|
||||
/*@
|
||||
Ox.range <f> Python-style range
|
||||
(stop) -> <[n]> range
|
||||
|
|
Loading…
Reference in a new issue