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

@momsfriendlydevco/chainable

v1.1.0

Published

Tiny, zero-dependency chainable object wrapper

Downloads

104

Readme

@MomsFriendlyDevCo/Chainable

Make any JavaScript object a chainable, functional interface.

This library is intentionally tiny and dependency free. The entire library, minified is less than 1kb!

Make the tiny Mitt event library chainable

import chainable from '../lib/chainable.js';
import mitt from 'mitt';

let emitter = chainable(mitt)
    .on('blarp', ()=> { /* Handle blarp emit */ })
    .on('flonk', ()=> { /* Handle flonk emit */ })
    .emit('blarp')
    .value() // Get original unwrapped emitter

Make a browser DOM element chainable

import chainable from '@momsfriendlydevco/chainable';

// Make a chainable DOM object
chainable(document.createElement('div'));
    .addEventListener('click', ()=> { /* Handle clicks */ })
    .set('style.position', 'absolute') // Set a deeply nested property
    .value() // Output the original object (if needed)

Why?

This library exists this because handling chainability in some non-chainable interfaces gets repetitive.

// Horrible <div> setup using the native DOM
let widget = document.createElement('div');
widget.addEventListener('click', ()=> { /* Handle click */ });
widget.style.position = 'absolute';
widget.style.top = '0px';
widget.style.left = '0px';
widget.classList.add('red');

document.body.append(widget);
// Equivalent using chainable
document.append(
    chainable(document.createElement('div'));
        .addEventListener('click', ()=> { /* Handle clicks */ })
        .set('style.position', 'absolute') // Set a deeply nested property
        .set('style.top', '0px')
        .set('style.left', '0px')
        .call('classList.add', 'red')
        .value() // Output the original raw DOMElement
);

API

chainable(object)

Create a new chainable instance. This also sets $source to the original object.

chainable.$source

Also available as chainable.source if there isn't any conflict with the name. The chainable source object. Generally you don't want to access this directly.

chainable.$call(name, ...args)

Also available as chainable.call(name, ...args) if there isn't any conflict with the name. As with .$get the name can be in dotted notation or array notation for deeply nested methods. Call a method on source but ignore the response and return the chainable instance.

chainable.$set(key, val)

Also available as chainable.set(key, val) if there isn't any conflict with the name. Set one or more properties within a source object. Can be called as a simple key/val setter - chainable.$set(keyName, value) - or as an object - chainable.$set({key1: val1, key2: val2}) The key can also use dotted notation (e.g. this.is.a.nested.key) or an array notation (e.g. ['this', 'is', 'a', 'nested', 'key']) to set deeply nested properties. Returns the chainable instance.

chainable.$get(path)

Also available as chainable.get(path) if there isn't any conflict with the name. Fetch a value from the source object, returning it. The path can also use dotted notation (e.g. this.is.a.nested.key) or an array notation (e.g. ['this', 'is', 'a', 'nested', 'key']) to get deeply nested properties. Returns the value found at the path or undefined if none.

chainable.$value()

Also available as chainable.value() if there isn't any conflict with the name. Return the original source object - i.e. unwrap the original.

chainable.$tap(callback)

Also available as chainable.tap(callback) if there isn't any conflict with the name. Run a callback inline as (chainable) => { /* handler */ }, ignore the result and return the chainable instance.

chainable.$thru(callback)

Also available as chainable.tap(callback) if there isn't any conflict with the name. Run a callback inline as (chainable) => { /* handler */ }, take the returned value as the new $source and and return the chainable instance.