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

bananawatcher

v2.0.1

Published

A library to help quickly and easily set up getters and setters.

Downloads

14

Readme

bananaWatch

A library to help quickly and easily set up getters and setters for object properties.

Installation

npm install bananawatcher

How it works

To watch an object all you need to do is call bananawatcher passing in the object to watch, the specific key you want to watch and the callback functions you want to use.

The object

bananawatcher should be able to watch any object including built in objects, it can even watch DOM elements however it only fires when Javascript is interacting with the element e.g. if you watch for set on an input field it won't fire when the user types in it but it will if you set it's value through JavaScript.

The key

The key is just the key of the object you want to watch e.g. var obj={test: 'test', bluePill: 'should have taken it'} do if you wanted to watch for getting or setting on bluePill you would pass in 'bluePill'.

The callback functions

There are 2 callback functions you can call they are get and set.

get

get is called every time something requests the object property, it is passed the value of the property and is expected to return what should be returned to the requester.

set

set is called every time something tries to set the object property, it is passed both the current value and the attempted set value and it is expected to return what the value should be set as.

Usage

Setting up a getter

// getter.js
var bWatch = require('bananawatcher');

var fruitPrices = {
  apples  : 0.75,
  bananas : 0.80,
  lemons  : 0.90,
  oranges : 0.75,
};

// Set up getters to convert the prices to pence
for (var key in fruitPrices) {
  bWatch(fruitPrices, key, {
    get: function(value){
      return Number(value * 100) + 'p';
    }
  });
}

console.log('The price of bananas is:', fruitPrices.bananas);
node getter.js

The price of bananas is: 80p

Setting up a setter

// setter.js
var bWatch = require('bananawatcher');

var user = {
  user_id   : 1337,
  name      : 'Barry Manilow',
  username  : 'bazaTheGreat'
};

// Set up a setter to remove illegal username characters
bWatch(user, 'username', {
  set: function(oldValue, newValue){
    return newValue.replace(/[^a-zA-Z0-9]/g, '');
  }
});

user.username = '<---IamBarry--->';
console.log(user.name, 'your username is:', user.username);
node setter.js

Barry Manilow your username is IamBarry

Setting up another setter

// watch_setter.js
var bWatch = require('bananawatcher');
var share = {
  name  : 'Alphabet Inc.',
  epic  : 'GOOG',
  price : 710.83
};

var notifyUser = function(message){
  console.log(message);
};

bWatch(share, 'price', {
  set: function(oldValue, newValue){
    if (newValue < 700) {
      notifyUser(share.name + '(' + share.epic + ') has gone below the threshold.');
    }
    return newValue;
  }
});

share.price = 650;
node watch_setter.js

Alphabet Inc.(GOOG) has gone below the threshold