0.0.11 • Published 9 years ago

bit.js v0.0.11

Weekly downloads
3
License
MIT
Repository
github
Last release
9 years ago

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
0.0.11

9 years ago

0.0.10

9 years ago

0.0.9

9 years ago

0.0.8

9 years ago

0.0.6

9 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago