array-find-predecessor
Find a predecessor value of the array that should be chosen when a given index is deleted
; const array = 'foo' 'bar' 'baz'; ; //=> 'foo'; //=> 'bar' ; //=> 'bar'
Installation
npm
npm install array-find-predecessor
bower
bower install array-find-predecessor
API
arrayFindPredecessor(array, index)
array: Array
(non-empty array)
index: Number
(index of the array assumed to be deleted)
Return: Number
or null
Essentially, it returns an array value one index before the given index
.
value: A B C
deleted: ^
substitute: ^
; //=> 'A'
If index
is 0
, it returns the successor value because the first element has no predecessor elements.
value: A B C
deleted: ^
substitute: ^
; //=> 'B'
If the array includes only a single value, it returns null
because no value exists except for the deleted value.
value: A
deleted: ^
substitute: (none)
; //=> null
License
Copyright (c) 2016 Shinnosuke Watanabe
Licensed under the MIT License.