1.0.0 • Published 7 years ago
barr v1.0.0
barr
A collection of binary search functions for sorted arrays.
Installation
Requires Node.js 8.3.0 or above.
npm i barrAPI
The module exports an object with four function methods.
includes (arr, value, [options])
Parameters
arr(array): The sorted array which may or may not containvalue.value(any): The value to look for inarr.- Optional: Object argument:
compare(function, array, or any): Specifies how array elements will be compared:- If a function: When passed two arguments
aandb, expected to return-1ifais less thanb,1ifais greater thanb, and0if they are equal. - If an array: An array of Map/object keys, the values of which can be used to compare Maps/objects in the array. Comparison is first performed using the key in the first array element of
compare. If any two Map/object elements inarrhave values for that key that are equal, the next key in thecomparearray is used, and so on. If any given element in acomparearray is itself an array, it is interpreted as a nested keychain. - Otherwise: A single Map/object key.
- If omitted: Will compare numbers and strings. Will coerce everything else into strings.
- If a function: When passed two arguments
Return Value
Returns true if arr contains an element that compare says is sort-equivalent to value.
Example
const {includes} = require('barr')
includes([1, 2, 3], 2) // trueindexOf (arr, value, [options])
Parameters
arr(array): The sorted array which may or may not containvalue.value(any): The value to look for inarr.- Optional: Object argument:
compare(function, array, or any): Specifies how array elements will be compared. Refer above to this option’s definition under the documentation forincludes.- Optional:
multiple(string): Specifies behavior in the event that more than one existing array element is sort-equivalent withvalue. If set tofirstorlast, the index of the first or last sort-equivalent element (respectively) will be returned. (Note that this will slow down the seek operation.) Otherwise, the index of whatever sort-equivalent item the algorithm comes across first will be returned.
Return Value
Returns the integer index if one is found; otherwise returns -1.
insert (arr, value, [options])
Parameters
arr(array): The array into which to insertvalue.value(any): The value to insert intoarr.- Optional: Object argument:
compare(function, array, or any): Specifies how array elements will be compared. Refer above to this option’s definition under the documentation forincludes.- Optional:
multiple(string): Only applies ifuniqueisfalseor undefined. Specifies behavior in the event that more than one existing array element is sort-equivalent withvalue. If set tofirstorlast, thenvaluewill be inserted before/after the first/last sort-equivalent element, respectively. (This will slow down the insert operation.) Otherwise,valuewill be inserted anywhere in the range of sort-equivalent items. unique(bool): If set totrue, then no item in the collection may be sort-equivalent with another; so if an existing item is sort-equivalent withvalue, it will be overwritten. If set tofalse, multiple sort-equivalent items are allowed, sovaluewill always be inserted. Defaults tofalse.
Return Value
Returns an object:
found(boolean):trueifcomparereported thatarralready contains a value with the same sort value asvalue;falseotherwise.index(positive integer): The index inarrat whichvaluewas inserted.
Example
const {insert} = require('barr')
const arr = ['a', 'c', 'e']
insert(arr, 'b') // {found: false, index: 1}
arr // ['a', 'b', 'c', 'e']
insert(arr, 'd') // {found: false, index: 3}
arr // ['a', 'b', 'c', 'd', 'e']
insert(arr, 'e', {multiple: 'last'}) // {found: true, index: 5}
arr // ['a', 'b', 'c', 'd', 'e', 'e']
// inserted here--^
insert(arr, 'e', {multiple: 'first'}) // {found: true, index: 4}
arr // ['a', 'b', 'c', 'd', 'e', 'e', 'e']
// inserted here--^push (arr, ...values)
Parameters
arr(array): The array into which to insertvalue.- Variadic:
values(one or more of: any): The values to insert intoarr.
Return Value
Returns the new length of arr.
Example
const {push} = require('barr')
const arr = ['a', 'c', 'e']
push(arr, 'b', 'd') // 5
arr // ['a', 'b', 'c', 'd', 'e']Related
This module is part of the “b” family of binary search modules.
1.0.0
7 years ago