https://github.com/jamiebuilds/outward-search
Search an array of items outwards from a starting index
https://github.com/jamiebuilds/outward-search
array binary-search find search
Last synced: 3 months ago
JSON representation
Search an array of items outwards from a starting index
- Host: GitHub
- URL: https://github.com/jamiebuilds/outward-search
- Owner: jamiebuilds
- License: mit
- Created: 2017-10-07T05:22:23.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-10-07T23:21:41.000Z (over 7 years ago)
- Last Synced: 2025-03-11T00:08:03.529Z (4 months ago)
- Topics: array, binary-search, find, search
- Language: JavaScript
- Size: 37.1 KB
- Stars: 9
- Watchers: 2
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# outward-search
> Search an array of items outwards from a starting index
## Install
```sh
yarn add outward-search
```## Usage
```js
import outwardSearch from 'outward-search';let arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
let match = searchOutwards(arr, 3, (item, index) => {
console.log('called: ' + item);
return item === 'e';
});// called: d
// called: e
// called: c
// called: f
console.log(match); // 'f'
```If you need to find an item in an array that you know should be near another
item, you can use outward-search to do so efficiently.```js
export default function outwardSearch(
items: Array,
start: number,
callback: (item: T, index: number) => boolean,
): T | null;
```The `callback` is called on the `start` index then it works its way outwards to
either end of the array.So with the starting index of `2` and an array with 8 items:
```js
2 // start
3 // up
1 // down
4 // up
0 // down (hit min)
5 // up
6 // up
7 // up (hit max)
```