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

black-hole-object

v1.0.3

Published

Create lightweight, chainable, callable, no-op black hole objects in JavaScript

Downloads

1,456

Readme

black-hole-object

Create lightweight, chainable, callable, no-op black hole objects in JavaScript!

The basics

This is an extremely simple package, with no dependencies. It supplies a constructor function (BlackHoleObject) for an infinitely chainable, callable, and accessible black hole object.

Heavily inspired by a feature of the fantastic Ruby gem Naught.

Installation

Install via npm:

npm install black-hole-object

Alternatively, install via yarn:

yarn add black-hole-object

Requirements

This package depends on Proxy being supported on the client. If you're using Node.js v6.0.0 or above, you're fine! But if you're developing for the browser, please read further.

Important note regarding use in the browser

While most browsers today support this feature, older browsers will not. And, unfortunately, although there are polyfills for Proxy, they are limited in functionality and cannot support the kind of dynamic property accession that is required for this package. If you need to support older browsers... then don't make this a necessary facet of your project. Them's the breaks.

You can always add a conditional guard to ensure window.Proxy is defined before using it, but that just means you can't build an architecture around the assumption that this thing is going to be available in old browsers. Step away for a bit, give it a li'l time. Those browser versions will be soil soon enough.

You can check out the compatibility table on MDN, or you can get a better picture of its browser support (complete with percentages and sliders) on caniuse.com/#feat=proxy. If your target browser supports Proxy, then it will support black-hole-object.

Using BlackHoleObject

Basic use

Check it out:

import BlackHoleObject from 'black-hole-object';

const foo = new BlackHoleObject();

foo.bar
// => no errors

foo.bar('baz')
// => no errors

foo.bar.baz.what('up', 'dude').silly.hey("yeah")("I'm")("serious")[123].for.real();
// => no errors (!)

const earth = foo.or.whatever;

earth.turtle.turtle.turtle.turtle;
//=> all the way down

Example use cases

Want to use console.log, console.warn, etc., but only want logging in a development environment (not in production)?

const debugMode = someConfig.environment === 'development';

const safeConsole = debugMode ? console : new BlackHoleObject();

// when someConfig.environment === 'production'...
// logs nothing to the console
safeConsole.log('hi')

// when someConfig.environment === 'development'...
// logs 'hi' to the console
safeConsole.log('hi')

Want to prevent API calls?

const letsNotHammerTheServer = true;

const axios = letsNotHammerTheServer ? new BlackHoleObject() : require('axios');

// when letsNotHammerTheServer === true...
// does nothing
axios.get('https://example.com/some/url').then((response) => {
  // ...
}).catch((error) => {
  // ...
});

// when letsNotHammerTheServer === false...
// makes the actual API call
axios.get('https://example.com/some/url').then((response) => {
  // ...
}).catch((error) => {
  // ...
});

Want a permissive "default" object?

// maybe you don't necessarily have a `user` object defined, so you do:

// before...
function setZipcodeOnUser(zipcode, user) {
  if (user && user.contactInfo && user.contactInfo.address) {
    user.contactInfo.address.zipcode = zipcode;
  }
}

// ...after!
function setZipcodeOnUser(zipcode, user = new BlackHoleObject()) {
  user.contactInfo.address.zipcode = zipcode;
}

The world is your oyster.

const oyster = new BlackHoleObject();

oyster.oyster.oyster.the.world.isMy('oyster');
//=> no complaints

Contributing

Bug reports and pull requests for this project are welcome at its GitHub page. If you choose to contribute, please be nice so I don't have to run out of bubblegum, etc.

License

This project is open source, under the terms of the MIT license.