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

keep-tidy

v1.1.1

Published

Simple utilities and ES5 base class for robust and maintainable code.

Downloads

15

Readme

keep-tidy

Build Status Code coverage

A tiny ES5 base class featuring clean disposal of bound resources. It also provides a simple API for easier debugging and diagnostics. All this works in both back-end and front-end environments.

Processing DOM events or similar in OO code implies correct set-up and releasing of event handlers. Also, cross-referenced class instances and other objects need special care to prevent the dreaded memory leaks. The TidyOwner base class interface of own property and dispose() method may save you from writing boring boilerplate code and possibly making mistakes in those cases.

Installation

npm install -S keep-tidyor yarn add keep-tidy

Usage

  import Tidy from 'keep-tidy'

  class A extends Tidy {
    resizeHandler() { 
      this.debug('resized')           //  console output like: 'A#2 resized +0ms 3'
    }
  }

  let superItem = new A() 
  //  Nested structures are OK.
  superItem.own.item1 = new A().ownOn('resize', 'resizeHandler', window).debugOn(true)
  superItem.own.somethingElse = [1, 2, 3]
  //  Lots of interecting things in between...
  superItem.dispose()                             //  Tidy up everything now...
  superItem = undefined                           //  ... and yes, this is no C++ ;)

There is also lightweight debugging and assertion helpers available even if you won't use Javascript classes at all.

API

The package exports the following API:

Some parts can be loaded individually: 'owner-class/debug' and 'owner-class/helpers'.

Baseclass

constructor TidyOwner([classNameOverride : string])

When using this baseclass, you'll avoid writing boilerplate code for releasing event handlers and freeing other resources. Its interface consists of:

  • assertHooka class (not instance) method - a convenience shortcut for assert.hook().
  • debug(...)see debugging for details.
  • debugOn([*]): *for re-generating the debug method and returning this for chaining, if argument supplied; otherwise returns boolean showing if debugging is enabled.
  • dispose()call this to free up all bound resources. Base class method cleans the own container, firing dispose method of every object instance having it. Then it un-registers all handlers set by ownOn method.
  • ownOff(event= : string, emitter= : Object) : this un-registers handlers registered for matching (event, emitter) pairs. It is called by dispose(), so in most cases you don't need to call it explicitly.
  • ownOn(event : string, handler, emitter, api=) : this registers event handler with emitter object. If emitter API differs from addEventListener/removeEventListener or $on/$off or on/off, then you need explicitly define the API, like ['listenTo', 'ignore']. The handler parameter can be instance method name or a function.
  • own: Objecta keyed container for private data, that should be gracefully cleaned up.
  • ownClass: stringclass name (read-only).
  • ownId: numberglobally unique class instance number(read-only).
  • ownTag: stringis set to ownClass+ '#' +ownId (read-only).

Assigning a value to any instance property will throw TypeError.

Debugging

Debugging machinery uses debug NPM package and is available only in development mode. In production mode all its API methods will be just empty functions.

debugMe( tag: string, [yesNo : boolean]) : {function(...)} exported from the main package is factory function returning the actual debug function.

The same factory function is default export from the sub-package (see example below).

Debugging plain javascript code:

  const D = require('keep-tidy/debug')
  const myDebug = D(main.js, true), { debug } = D

  myDebug('try(%d, %o)', 1, 2, 3)       // --> 'main.js try(1, 2) +0ms 3'
  debug.enable('*')
  debug('natively')('yay!')             // --> 'natively yay! +0ms'

Compatibility

The package should be ECMAScript 2015 and it should run virtually anywhere. If you find any issues, please do not hesitate to report.