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

fluid-dom

v1.2.0

Published

A fluid DOM (DSL style) library.

Downloads

4

Readme

Fluid DOM

It has been said elsewhere that an API that exposes functions that return objects that let you chain multiple function calls together becomes like a domain specific language. Personally, I don't think I'd go that far but it is certainly convenient. Further, if the function names are consistent then it starts to get very predictable, what makes for much nicer API... don't you think?

Inspired partly by .NET's LINQ, the Fluid DOM is a fluid or functional DSL styled API for the HTML JavaScript DOM.

Why Use It?

Convenience!

The standard DOM APIs are a bit clunky in that some things are done with properties and some with methods.

Fluid-DOM is very small and doesn't get away of the standard DOM APIs. Instead, this library is more of a convenience wrapper to bring the standard APIs into a more functional way of thinking about things. That said, it's more about convenience than true functional programming. Let's face it, we're mutating the DOM.

How to Reference

Fluid-dom is built to a browser iife bundle and a commonjs library. Each is supplied in minified and developer/debug forms. For example, the bundle comes as fluid-dom.bundle.js and fluid-dom.bundle.dev.js for convenience.

Direct Script-src

For simplicity, the fluid-dom library builds to a single (iife) file and exposes a global variable called fluid as a namespace object.

For the browser, the use a script tag:

    <script src="fluid-dom.bundle.js"></script>
    <script>
        var dom = new fluid.DOM();
        // rest...
    </script>

Browserify

You can use browserify to bundle your JavaScript and use require to import the fluid-dom library into your own code. That needs to use the commonjs version (also included).

var fluid = require('fluid-dom/fluid-dom.commonjs');

var dom = new fluid.DOM();
// and so on...

Examples

Say for an HTML document...

<html>
<body>
    <section id="iteration">
        <h1>Iteration Demo</h1>
        <ul>
            <li>An item</li>
            <li>Second item yay</li>
            <li>Third item yay</li>
        </ul>
    </section>
</body>
</html>

To iterate through the list items and add a class called "fancy"...

var dom = new fluid.DOM()
dom.findElement({id:'iteration'})
    .findAll({selector:'ul>li'})
    .each( li => li.classes().add('fancy') )

Or if you want to add 'mouseover' events to the List Items and use a selector to get all the list items...

dom.findAll({selector:'#iteration>ul>li'})
    .each(item => item.on({
        event: dom.events.MOUSEOVER,
        handler: () => alert('Mouse over list item')
    }))

The Array methods of filter, map and reduce are also available on an element list, so you can do the following to summarise LI text.

var summary = dom
    .findAll({tagName: 'li'})
    .map( li => li.text() )
    .reduce( (li1, li2) => `${li1}, ${li2}` )

console.log(summary)

And if you wanted to hide any LI member that had the word 'yay' in their text...

dom.findAll({tagName: 'li'})
    .each(item => item.text( item.text().replace(/yay/,'boo') ))

And finally, if you need to use the regular DOM API for some reason, that's available to you as well. The element property on an Element object gives you the standard HTMLElement object.

dom.findElement({id: 'iteration'}).element.innerHTML = '<b>If you must</b>'

But I still think it's nicer to stay with Fluid DOM most of the time. For instance...

dom.findElement({id: 'iteration'})
    .html('<b>can be done</b>')
    .classes().add('fancy').add('lined-box')