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

bit.js

v0.0.11

Published

A set of utility functions for JavaScript functions

Downloads

22

Readme

Bit.js

A set of utility functions for JavaScript functions.


Installation

If you want to use Bit.js in the browser simply download build/bit.min.js and include it in your page:

<script src="./js/bit.min.js"></script>

It is also available via npm:

npm install bit.js

Then in your code:

require('bit.js');

API (well, not exactly ...)

There is no initialization or calling a constructor. Once Bit.js is loaded it registers bunch of functions to Function.prototype. So the following functions are available on the fly.

f.callWith(...args)

callWith appends its parameters to the ones passed to the original function.

var whatsUp = function (a, b) {
  return a + ' ' + b;
}.callWith('is coming');

whatsUp('Winter'); // Winter is coming
whatsUp('Arya Stark'); // Arya Stark is coming

f.once()

f will be called only once.

var killJonSnow = function () {
  // this could happen only once
}.once();

killJonSnow(); // Nooooo, he is dead
killJonSnow(); // You already killed it
killJonSnow(); // ... seriously, he IS dead

f.twice()

f will be called only two times.

var amIAryaStark = function () {
  // It depends who you ask
}.twice();

amIAryaStark(); // Hm ...
amIAryaStark(); // Yes, I am
amIAryaStark(); // You already know ... does nothing
amIAryaStark(); // You already know ... does nothing

f.debounce([milliseconds])

Limits the rate at which f can fire.

var watchGameOfThrones = function () {
  // ... fun
}.debounce(604800000); // 604800000 milliseconds === 1 week

watchGameOfThrones(); // It works!
watchGameOfThrones(); // Who dies we'll see next week ... does nothing
watchGameOfThrones(); // Next week bro, next week ... does nothing

f.callIf([condition function])

Execute the function only if the condition function returns true.

var isSamwellTarlyHungry = function (time) {
  return time >= 0;
};
var feedNightsWatch = function (time) {
  // eating ...
}.callIf(isSamwellTarlyHungry);

feedNightsWatch(8); // eating ...
feedNightsWatch(22); // eating ...
feedNightsWatch(-2); // no eating

f.format([formatter function])

Process/format the result of your function.

var theTruth = function (result) {
  return result === 'Reek' ? 'Theon Greyjoy' : result;
};
var whoIsTheonGreyjoy = function () {
  return 'Reek';
}.format(theTruth);

whoIsTheonGreyjoy(); // Theon Greyjoy

f.middlewares(...args)

Pass as many functions are you need. They'll be run against the result of your function one by one.

var answerA = function (result) {
  result.push('the First of Her Name');
  return result;
};
var answerB = function (result) {
  result.push('the Unburnt');
  return result;
};
var whoIsDaenerysTargaryen = function () {
  return ['Mother of Dragons'];
}.middlewares(answerA, answerB);

whoIsDaenerysTargaryen(); // ["Mother of Dragons", "the First of Her Name", "the Unburnt"]

f.observe([function])

[function] will be called when f is executed. The observer will receive the output of the original function.

var smile = function () {
  console.log(':)');
};

var TyrionLannisterIsGreatWarior = function () {
  // no way
}.observe(smile);

TyrionLannisterIsGreatWarior(); // prints ':)'
TyrionLannisterIsGreatWarior(); // prints ':)'

f.enabled([true | false])

Enable or disable a function execution.

var JoffreyBaratheonKillsSomeone = function () {
  console.log('Joffrey: why not!');
}.enabled(true);

JoffreyBaratheonKillsSomeone(); // Joffrey: why not!
JoffreyBaratheonKillsSomeone(); // Joffrey: why not!
JoffreyBaratheonKillsSomeone.enabled(false);
JoffreyBaratheonKillsSomeone(); // does nothing
JoffreyBaratheonKillsSomeone(); // does nothing