binsert
v1.0.0
Published
Uses binary search to insert a value into any sorted collection.
Downloads
8
Maintainers
Readme
binsert
Uses binary search to insert a value into any sorted collection.
Installation
Requires Node.js 8.3.0 or above.
npm i binsert
API
The module exports a single function.
Parameters
- Object argument:
- Optional:
compare
(function, array, or any):- If a function: When passed two arguments
a
andb
, expected to return-1
ifa
is less thanb
,1
ifa
is greater thanb
, and0
if they are equal. - If an array: An array of Map/object keys, the values of which can be used to compare two Maps/objects. The first key is checked first, and if the two values for that key are equal, the next key in the array is checked, and so on. If any given element 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
get
(function): A callback that should return a value for a given index from0
tolength - 1
.- Optional:
insert
(function): A callback that accepts the index at whichvalue
should be inserted. The callback is not expected to return anything. Ifinsert
is omitted, it is assumed you will take care of insertion later using theindex
return value. length
(positive integer): The length of the collection.- Optional:
multiple
(string): Only applies ifunique
isfalse
or undefined. Specifies behavior in the event that more than one existing collection item is sort-equivalent withvalue
. If set tofirst
orlast
, thenvalue
will be inserted before/after the first/last sort-equivalent item, respectively. (This will slow down the insert operation.) Otherwise,value
will be inserted anywhere in the range of sort-equivalent items. - Optional:
set
(function): A callback that accepts the index at which an existing value should be overwritten withvalue
. This only applies ifunique
istrue
. The callback is not expected to return anything. - Optional:
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 either be overwritten with theset
callback (if one is provided) or else nothing will happen (ifset
is not specified). If set tofalse
, multiple sort-equivalent items are allowed, sovalue
will always be inserted. Defaults tofalse
. value
(any): The value to insert.
- Optional:
Return Value
Returns an object:
found
(boolean):true
ifcompare
reported that the collection already contained a value with the same sort value asvalue
;false
otherwise.index
(positive integer): The index at whichvalue
was (or should be) inserted.
Example
const binsert = require('binsert')
const arr = ['a', 'c', 'e']
binsert({get: i => arr[i], insert: (i, v) => { arr.splice(i, 0, v) }, length: arr.length, value: 'b'}) // {found: false, index: 1}
arr // ['a', 'b', 'c', 'e']
Related
This module is part of the “b” family of binary search modules.