npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cutoff-catcher

v1.0.5

Published

A binary-search-flavored method to finding a cutoff

Downloads

12

Readme

Meta

Installation

To install cutoff-catcher using npm, run:

$ npm install cutoff-catcher

How it works

By putting in an initial step, cutoff-catcher will iterate over the integers by such step until the a condition is satisfied (the input function returns true). Then a binary search beginning at the midpoint of the iterator's position and the previous iterator position will begin until the cutoff between where the condition is and is not satisfied is found. Return format is (error, cutoffPosition).

Usage

With cutoff-catcher you can do more efficient iterations over a large range of numbers to search for where a certain quality changes. Specify an initial step size and a function to look for a specific quality. Will return an error if it could not find the cutoff in 1,000,000 steps, or the first integer where the test is successful. The following are 3 examples where one could apply the module (1 javascript and 2 coffeescript because coffeescript is life).

Find the smallest value for which the sum from i=1 to n of (2i)^3 <= (20n)^3.

var catcher = require('cutoff-catcher');

test = function(x){
  var sum = 0;

  for (var i=0; i<x; i++) {
    sum += Math.pow(2*i, 3);
  }

  return sum > Math.pow(20*x, 3);
}

catcher(1000, test, function(err, cutoff){
  if(err){
    console.log(err);
  }else{
    console.log(cutoff);
  }
});

Do a more efficient find for alpha-ordered lists (better for large lists).

catcher = require 'cutoff-catcher'

dict = ["apple", "grapefruit", "orange", "tomato", "zuchini", "banana", "kiwi", "grape"].sort()

catcher 4, (x) -> dict[x] >= "grape", (err, cutoff) ->
  if err then console.log err
  else console.log dict[cutoff]

Find a field attribute cutoff in a list of objects.

catcher = require 'cutoff-catcher'

ben = name: 'ben', present: yes
luke = name: 'luke', present: yes
joe = name: 'joe', present: yes
noah = name: 'noah', present: no
gary = name: 'gary', present: no

people = [ben, luke, joe, noah, gary]

test = (x) -> try not people[x].present

catcher 2, test, (err, cutoff) ->
  if err then console.log err
  else console.log people[cutoff].name