This project is part of the @thi.ng/umbrella monorepo.
Array / Arraylike utilities.
STABLE - used in production
Search or submit any issues for this package
yarn add @thi.ng/arrays
// ES module
<script type="module" src="https://unpkg.com/@thi.ng/arrays?module" crossorigin></script>
// UMD
<script src="https://unpkg.com/@thi.ng/arrays/lib/index.umd.js" crossorigin></script>
Package sizes (gzipped, pre-treeshake): ESM: 2.08 KB / CJS: 2.27 KB / UMD: 2.23 KB
- arrayIterator()
- binarySearch()
- bisect()
- endsWith()
- ensureArray()
- ensureIterable()
- fillRange()
- find()
- fuzzyMatch()
- insert()
- into()
- isSorted()
- levenshtein()
- multiSwap()
- peek()
- quickSort()
- shuffle() (w/ custom PRNG support)
- shuffleRange() (w/ custom PRNG support)
- sortByCachedKey()
- startsWith()
- swap()
- swizzle()
The following predicates can be used to perform predecessor / successor
queries using binarySearch()
.
bsLT()
- Returns index of last item less than search value or -1 if no such values existbsLE()
- Similar tobsLT()
, but for less-than-equals queriesbsGT()
- Returns index of first item greater than search value or -1 if no such values existbsGE()
- Similar tobsGT()
, but for less-than-equals queriesbsEQ()
- Merely syntax sugar, casting any non-found result indices to -1
const src = [10, 20, 30, 40];
bsLT(binarySearch(src, 25))
// 1
// greater-than queries also require the array length
bsGT(binarySearch(src, 25), src.length)
// 2
bsGT(binarySearch(src, 40), src.length)
// -1
Karsten Schmidt
If this project contributes to an academic publication, please cite it as:
@misc{thing-arrays,
title = "@thi.ng/arrays",
author = "Karsten Schmidt",
note = "https://thi.ng/arrays",
year = 2018
}
© 2018 - 2021 Karsten Schmidt // Apache Software License 2.0