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

demolish

v1.0.2

Published

Generate a destruction method which clean's up and destroys all references on the instance

Downloads

2,978

Readme

demolish

Made by unshiftVersion npmBuild StatusDependenciesCoverage StatusIRC channel

Demolish is a small module which helps you clean, release and destroy your created instances.

Install

This module is intended for Node.js and Browserify usage and can be installed using:

npm install --save demolish

Usage

The module is exported as a function and be required as following:

'use strict';

var demolish = require('demolish');

The demolish function returns a function which will destroy the specified properties from your instance.

function Foo() {
  this.bar = 1;
  this.banana = new Banana();
}

Foo.prototype.destroy = demolish('bar banana');

In the example above we've created a new destroy method on our Foo class. Once the method is called it will set the bar property to null and check if banana also has a destroy method, if so, it will call that method and set the property to null after the execution.

After everything is cleaned up we will emit a destroy event if there is an emit method available.

The destroy method will automatically prevent double execution by checking if the first supplied property is still active on the prototype. So in the example above it will check if bar is not null.

But nulling objects and destroying things you've set on an instance might not be enough. Sometimes you need a bit more and for those cases we have the additional before and after hooks. These hooks can be specified in the options:

Foo.prototype.destroy = demolish('bar banana', {
  before: 'clear',
  after: ['removeAllListeners', function () {
    // things
  }]
});

In the example above you see all the supported styles. If you supply a string we assume that it's a function on the prototype that needs to be executed in order to clean up things correctly. If you need to run multiple tasks you can supply an array with strings. In addition to strings we also support functions, these functions will be called with their this value set to the instance of the class where destroy works.

So in the example above the execution flow is the following:

  1. Check if destroy has already been called, if not, continue to step 2.
  2. Execute the before hook.
  3. Iterate over all properties that need to be destroyed and nulled.
  4. Emit the destroy event, where possible.
  5. Execute the after hook.

License

MIT