Natural Preference for Minification
Get unlimited public & private packages + package-based permissions with npm Pro.Get started »

@strong-roots-capital/ratchet

2.0.0 • Public • Published

ratchet Build status npm version codecov

Filter a stream of values monotonically

Use the Maybe monad to "ratchet" a stream of values, guaranteeing processing over monotonically-changing values.

This function obeys the following invariants:

  • only yields monotonically-changing values
  • does not yield duplicates
  • does not yield any value that was not passed in

Install

npm install @strong-roots-capital/ratchet

Use

import Ratchet from '@strong-roots-capital/ratchet'
import randomInt from 'random-int'
 
const comparator = (x: number, y: number) => x - y
const ratchet = Ratchet(comparator)
 
function doTheThing(x: number) {
    ratchet(x)
        .map((element: number) => {
            /* safely do something here with element */
            console.log(x)
        })
    // `map` will only run when `x` is an element that would be
    // sorted after all previously-seen values
}
 
const input = Array(10).fill(100).map(randomInt)
console.log(input)
//=>[ 69, 33, 65, 12, 91, 34, 95, 91, 80, 12 ]
 
input.forEach(doTheThing)
//=>69
//=>91
//=>95

In this example, we use the ratchet to ensure each value processed is numerically higher than all previously-processed values.

Note: the ratchet will never emit a duplicate value.

To define a custom definition of equality (e.g. deepEqual) pass a comparison function as the second argument.

Related

Acknowledgments

Index

Functions


Functions

Ratchet

Ratchet<T>(comparator: function, equality?: function): function

Defined in ratchet.ts:46

Filter a stream of values monotonically.

Type parameters:

T

Parameters:

Name Type Default value
comparator function -
Default value equality function (x, y) => x === y

Returns: function


Install

npm i @strong-roots-capital/ratchet

DownloadsWeekly Downloads

33

Version

2.0.0

License

ISC

Unpacked Size

9.22 kB

Total Files

6

Last publish

Collaborators

  • avatar
  • avatar