@philiprehberger/sorted-array
v0.1.1
Published
Sorted array with binary search — maintains order on insert
Readme
@philiprehberger/sorted-array
Sorted array with binary search — maintains order on insert.
Installation
npm install @philiprehberger/sorted-arrayUsage
import { sortedArray } from '@philiprehberger/sorted-array';
const arr = sortedArray<number>();
arr.insert(5);
arr.insert(1);
arr.insert(3);
console.log(arr.toArray()); // [1, 3, 5]
console.log(arr.has(3)); // true
console.log(arr.range(1, 4)); // [1, 3]
// Custom comparator (descending)
const desc = sortedArray<number>((a, b) => b - a);
desc.insert(1);
desc.insert(3);
desc.insert(2);
console.log(desc.toArray()); // [3, 2, 1]API
sortedArray<T>(comparator?)
Creates a new sorted array. Accepts an optional comparator function.
Returns a SortedArray<T> with:
insert(value)— Insert a value in sorted positionremove(value)— Remove a value, returnstrueif foundhas(value)— Check if a value existsindexOf(value)— Get index of value, or-1range(min, max)— Get all values between min and max (inclusive)toArray()— Get a copy of the internal arraylength— Number of elementsfirst— First (smallest) element orundefinedlast— Last (largest) element orundefined[Symbol.iterator]()— Iterate in sorted order
Development
npm install
npm run build
npm testLicense
MIT
