Find an array index of the substitute value that should be chosen when a given index is deleted
import subIndex from 'sub-index';
const array = new Array(3);
subIndex(array, 1); //=> 0
subIndex(array, 2); //=> 1
subIndex(array, 0); //=> 1npm install sub-index
bower install sub-index
array: Array (non-empty array)
index: Number (index of the array assumed to be deleted)
Return: Number or null
Essentially, it returns the predecessor number of a given index.
index: 0 1 2
value: A B C
deleted: ^
substitute: ^
subIndex(['A', 'B', 'C'], 1); //=> 0If index is 0, it returns the successor number 1 because the first element has no predecessor elements.
index: 0 1 2
value: A B C
deleted: ^
substitute: ^
subIndex(['A', 'B', 'C'], 0); //=> 1If the array includes only a single value, it returns null because no value exists after deletion.
index: 0
value: A
deleted: ^
substitute: (none)
subIndex(['A'], 0); //=> nullCopyright (c) 2016 Shinnosuke Watanabe
Licensed under the MIT License.