@bemoje/arr-sorted-index-of

1.0.2 • Public • Published

@bemoje/arr-sorted-index-of

Binary search -based indexOf for sorted arrays.

Version

NPM version

Travis CI

dependencies

Dependencies

dependencies

Stats

NPM downloads Forks

Donate

Buy Me A Beer donate button PayPal donate button

Installation

npm install @bemoje/arr-sorted-index-of
npm install --save @bemoje/arr-sorted-index-of
npm install --save-dev @bemoje/arr-sorted-index-of

Usage

import arrSortedIndexOf from '@bemoje/arr-sorted-index-of'

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

arrSortedIndexOf(alpha, 'b')
//=> 1

arrSortedIndexOf(alpha, 'e')
//=> -1

const numeric = [2, 13, 20]

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

arrSortedIndexOf(numeric, 20, (a, b) => {
  return a - b
})
//=> 2

const arrays = [
  [192, 168, 0, 0],
  [192, 168, 0, 1],
  [192, 168, 1, 0],
]

arrSortedIndexOf(arrays, [192, 168, 0, 1], {
  numeric: true,
  arrays: true,
})
//=> 1

let elem

const objectsByName = [
  { name: 'bonzo', age: 9 },
  { name: 'john', age: 7 },
]

elem = { name: 'john', age: 7 }

arrSortedIndexOf(objectsByName, elem, {
  by: 'name',
})
//=> 1

const objectsByAge = [
  { name: 'john', age: 7 },
  { name: 'bonzo', age: 9 },
]

elem = { name: 'john', age: 7 }

arrSortedIndexOf(objectsByAge, elem, {
  by: 'age',
})
//=> 0

const valuesByAge = [
  ['john', 7],
  ['bonzo', 9],
]

elem = ['bonzo', 9]

arrSortedIndexOf(objectsByAge, elem, {
  by: 1,
})
//=> 1

const valuesByFirstInt = [
  ['john', 'johnson', 7],
  ['tracy', 'chapman', 9],
]

elem = ['tracy', 'chapman', 9]

arrSortedIndexOf(valuesByFirstInt, elem, {
  by: (arrElem) => {
    for (let val of arrElem) {
      if (Number.isInteger(val)) {
        return val
      }
    }
  },
})
//=> 1

Tests

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

npm run test

API

Table of Contents

arrSortedIndexOf

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 number Returns -1 if not found, if found, returns the elements index position.

comparator

Comparator function callback definition.

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

Callback type definition.

Type: Function

Parameters
  • a any The value

Returns any The value to be compared

Package Sidebar

Install

npm i @bemoje/arr-sorted-index-of

Weekly Downloads

13

Version

1.0.2

License

MIT

Unpacked Size

15.5 kB

Total Files

5

Last publish

Collaborators

  • bemoje