1.0.0 • Published 10 years ago

sub-index v1.0.0

Weekly downloads
188
License
MIT
Repository
github
Last release
10 years ago

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.

1.0.0

10 years ago

0.0.7

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago

0.0.0

10 years ago