1.0.2 • Published 4 years ago

@bemoje/arr-sorted-has v1.0.2

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

@bemoje/arr-sorted-has

Binary search -based indexOf for sorted arrays.

Version

Travis CI

Dependencies

Stats

Donate

Installation

npm install @bemoje/arr-sorted-has
npm install --save @bemoje/arr-sorted-has
npm install --save-dev @bemoje/arr-sorted-has

Usage

import arrSortedHas from '@bemoje/arr-sorted-has'

const alpha = ['a', 'b', 'c']

arrSortedHas(alpha, 'b')
//=> true

arrSortedHas(alpha, 'e')
//=> false

const numeric = [2, 13, 20]

arrSortedHas(numeric, 20, {
  numeric: true,
})
//=> true

Tests

Uses Jest to test module functionality. Run tests to get coverage details.

npm run test

API

Table of Contents

arrSortedHas

Binary search -based indexOf for sorted arrays.

Parameters
  • arr Array The array to search

  • element any The element to find

  • compare (comparator | object)?

    • compare.numeric boolean Sort numerically. Defaults to lexicographic/alphabetic sort. (optional, default false)

    • compare.descending boolean Sort in descending order. Defaults to ascending order. (optional, default false)

    • compare.array boolean Sort arrays. Nested arrays are also compared recursively. (optional, default false)

    • compare.by (number | string | getter) Sort by either array index, a callback(element): any - or by object keys with dot-notation support. (optional, default undefined)

Returns boolean

comparator

Type: Function

Parameters
  • a any The first value to compare

  • b any The second value to compare

Returns number A negative number if a > b, a positive number if a < b, 0 otherwise.

getter

Type: Function

Parameters
  • a any The value

Returns any The value to be compared