array-find-predecessor

1.0.0 • Public • Published

array-find-predecessor

NPM version Bower version Build Status Coverage Status dependencies Status devDependencies Status

Find a predecessor value of the array that should be chosen when a given index is deleted

import arrayFindPredecessor from 'array-find-predecessor';
 
const array = ['foo', 'bar', 'baz'];
 
arrayFindPredecessor(array, 1); //=> 'foo'
arrayFindPredecessor(array, 2); //=> 'bar'
 
arrayFindPredecessor(array, 0); //=> '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: ^
arrayFindPredecessor(['A', 'B', 'C'], 1); //=> 'A'

If index is 0, it returns the successor value because the first element has no predecessor elements.

value:      A B C
deleted:    ^
substitute:   ^
arrayFindPredecessor(['A', 'B', 'C'], 0); //=> '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)
arrayFindPredecessor(['A'], 0); //=> null

License

Copyright (c) 2016 Shinnosuke Watanabe

Licensed under the MIT License.

Package Sidebar

Install

npm i array-find-predecessor

Weekly Downloads

274

Version

1.0.0

License

MIT

Last publish

Collaborators

  • shinnn