sub-index

1.0.0 • Public • Published

sub-index

NPM version Bower version Build Status Coverage Status devDependencies Status

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, 3); //=> 2

subIndex(array, 0); //=> 1

Installation

npm

npm install sub-index

bower

bower install sub-index

API

subIndex(array, index)

array: Array (non-empty array)
index: Number (index of the array)
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); //=> 0

If 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); //=> 1

If 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); //=> null

License

Copyright (c) 2016 Shinnosuke Watanabe

Licensed under the MIT License.

Package Sidebar

Install

npm i sub-index

Weekly Downloads

1,455

Version

1.0.0

License

MIT

Last publish

Collaborators

  • shinnn