Coder Social home page Coder Social logo

bit.js's Introduction

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

bit.js's People

Stargazers

 avatar David Kirby avatar Loki avatar Brian Pattison avatar  avatar bitVice avatar Christian Rebelsky avatar Ladislav Gazo avatar InBasic avatar zhe.liu avatar James Edward Lewis II avatar Klāvs Priedītis avatar

Watchers

Krasimir Tsonev avatar Klāvs Priedītis avatar bitVice avatar  avatar  avatar

Forkers

brydzu

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.