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

atom-utils

v0.9.2

Published

A bunch of general purpose utilities for Atom packages

Downloads

139

Readme

atom-utils

Build Status

A bunch of general purpose utilities for Atom packages.

requirePackages(packageNames...)

Returns a promise that is only resolved when all the requested packages have been activated.

{requirePackages} = require 'atom-utils'

requirePackages('tree-view', 'find-and-replace', 'snippets')
.then ([treeView, findAndReplace, snippets]) ->
  # Do something with the required packages

registerOrUpdateElement(elementName, prototype)

Registers or updates a custom element whose name is elementName.

{registerOrUpdateElement} = require 'atom-utils'

class MyElement
  createdCallback: ->
    console.log 'element created'

registerOrUpdateElement('my-element', MyElement.prototype)

The update is performed by copying the properties from the passed-in prototype in the registered element prototype. As a node's callback methods can't be overriden once the element have been registered, a generic version is created that will invoke the concrete callback when called, that way even the node's callback methods can be updated.

Ancestors (previously AncestorsMethods)

A mixin that provides jQuery a like method to retrieve a node's parents:

{Ancestors} = require 'atom-utils'

class DummyNode extends HTMLElement
  Ancestors.includeInto(this)

  attachedCallback: ->
    # Returns all the ancestors to the html element
    parents = @parents()

    # Returns all the ancestors that matches the selector
    filteredParents = @parents('selector')

# It creates the custom element and register it as the `dummy-node` tag.
DummyNode = document.registerElement 'dummy-node', prototype: DummyNode.prototype

::parents(selector)

Can be called with or without the selector argument.

If called without argument, the method will return every parents of the current node.

If called with an argument, the method will return only the parent elements that match the passed-in selector.

Parents in the returned array are sorted by their distance from the node.

::queryParentSelectorAll(selector)

An alias of ::parents except it throw an error if called without a selector.

::queryParentSelector(selector)

Returns only the first parent element that matches the passed-in selector.

Throws an error when called without a selector.

::eachParent(iterator)

Iterates over each parent the node and calls iterator with the current parent node.

DisposableEvents

A mixin that provides a addDisposableEventListener method that registers an event listener on an element and returns a Disposable to unregister it:

{DisposableEvents} = require 'atom-utils'
{CompositeDisposable} = require 'event-kit'

class DummyNode extends HTMLElement
  DisposableEvents.includeInto(this)

  createdCallback: ->
    @subscriptions = new CompositeDisposable

    @subscriptions.add @addDisposableEventListener this, 'click', (e) =>
      # ...

# It creates the custom element and register it as the `dummy-node` tag.
DummyNode = document.registerElement 'dummy-node', prototype: DummyNode.prototype

EventsDelegation

A mixin that provides events delegation ala jQuery without jQuery. Use it by including it into your custom element:

{EventsDelegation} = require 'atom-utils'
{CompositeDisposable} = require 'event-kit'

class DummyNode extends HTMLElement
  # It includes the mixin on the class prototype.
  EventsDelegation.includeInto(this)

  # Custom element's callback on creation.
  createdCallback: ->
    @subscriptions = new CompositeDisposable

    @appendChild(document.createElement('div'))
    @firstChild.appendChild(document.createElement('span'))

    # Without a target and a selector, it registers to the event on the
    # element itself.
    # The `subscribeTo` method returns a disposable that unsubscribe from
    # all the events that was added by this call.
    @subscriptions.add @subscribeTo
      click: (e) ->
        console.log("won't be called if the click is done on the child div")

    # With just a selector, it registers to the event on the elements children
    # matching the passed-in selector.
    @subscriptions.add @subscribeTo 'div',
      click: (e) ->
        console.log("won't be called if the click is done on the child span")
        # Events propagation can be used to prevents the delegated handlers
        # to catch the events in continuation.
        e.stopPropagation()

    # By passing a node and a selector, it registers to the event on the
    # elements children matching the passed-in selector.
    @subscriptions.add @subscribeTo @firstChild, 'span',
      click: (e) ->
        e.stopPropagation()

# It creates the custom element and register it as the `dummy-node` tag.
DummyNode = document.registerElement 'dummy-node', prototype: DummyNode.prototype

ResizeDetection

As there is no standard way to detect when an element size changed this mixin provides a DOM polling mechanism to custom element whose display and logic may rely on the element size:

{ResizeDetection} = require 'atom-utils'

class DummyNode extends HTMLElement
  ResizeDetection.includeInto(this)

  # Starts the DOM poll when the element is attached
  attachedCallback: -> @initializeDOMPolling()

  # Stops the DOM poll when the element is detached
  detachedCallback: -> @disposeDOMPolling()

  # Callback called when the poll routine has detected a size change.
  # The callback receive the new width and height as arguments.
  resizeDetected: (width, height) ->

  # If you need to prevent the poll routine to check the element size,
  # redefine this method with your own conditions. The function should return
  # false when the polling can occurs.
  # isDOMPollingPrevented: -> @domPollingPaused

If you need to suspend the polling while keeping the interval active you can use the pauseDOMPolling to pause the polling routine and resumeDOMPolling to resume it.

SpacePenDSL

A mixin that provides the same content creation mechanism as space-pen but for custom elements:

{SpacePenDSL} = require 'atom-utils'

class DummyNode extends HTMLElement
  SpacePenDSL.includeInto(this)

  @content: ->
    @div outlet: 'container', class: 'container', =>
      @span outlet: 'label', class: 'label'

  createdCallback: ->
    # Content is available in the created callback

# It creates the custom element and register it as the `dummy-node` tag.
DummyNode = document.registerElement 'dummy-node', prototype: DummyNode.prototype