1.0.2 • Published 6 years ago

bracketing v1.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

Bracketing

bracketing is a small utility function to effeciantly walk and compare consecutive elements in an array or string and extract the longest sub array that fulfills this predicate. It is essentially two managed array pointers to traverse an array with the bare minimum number of comparisons. It will return the longest sequence that satisfies the supplied predicate function.

Current Version Build Status

Useage

These following examples demonstrate some basic usage and should give you a better idea what bracketing is all about

const bracketing = require('bracketing');

let result;

result = bracketing((left, right) => right > left, [1, 2, 3, 0, 5, 6, 7, 8, 100]);
// result equals [0, 5, 6, 7, 8, 100] because it is the longest sub array where the right value is greater than the left
result = bracketing((left, right) => left === right, 'aaaaaabbbbbccdeeeeeeeeeee');
// result equals "eeeeeeeeeee" because it is the longest substring where each character equals the one to the right

API

bracketing(predicate, sequence)

Returns the result of running the predicate against sequence. The result is either a string or array, matching the type of sequence.

  • predicate - a function to run against the elements of sequence. It must return a boolean value indicating pass or fail of the two elements. Has the following signature
    • left - the left value
    • right - the right value
  • sequence - a string or array to traverse.

Browser Usage

There is a UMD build provided in the "dist" folder. bracketing should have no issues working in a browser context. The only potential "got ya" is that accessing a string using bracket notation, which this module does, will not work in Internet Explorer 7.