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

domod

v1.0.0

Published

A lightweight data-binding (or so-called mvvm) library, providing both declarative template and imperative call ways.

Downloads

9

Readme

gzipped license

A lightweight data-binding (or so-called mvvm) library, providing both declarative template and imperative call ways.

"domod" is named from "DOM", "Model" and their two-way relationship.

Philosophy

  • Connection, relation, dependency. No matter it is data or dom, it is a tree. Tree and tree node relations make up graphs.
  • Reactive programming.

Simple Application

A simple application:

const state = {
    count: 0,
    increase() {
        this.count++ // `this` points to `state`
    }
}

DMD.relate(state, {
    negative: {
        resultFrom() {
            return this.count < 0 // `this` points to `state`
        }
    }
})

const view = `
    <div>
        <h1 m-style="{ color: $negative ? 'red' : 'green' }">{{$count}}</h1>
        <button m-onclick="$count = $count - 1">-</button>
        <button m-onclick="$increase()">+</button>
    </div>
`

DMD('#app', view, state)

API

An online demo.

An example of a form:

<script src="path/to/domod.js" charset="utf-8"></script>

<form id="form1">
    <div>
        <label>gender:</label>
        <input m-onclick="$form.gender = 'male'" 
               type="radio" name="gender" value="male" m-checked="$form.gender === 'male'">Male
        <input m-onclick="$form.gender = 'female'" 
               type="radio" name="gender" value="female" m-checked="$form.gender === 'female'">Female
    </div>
    <div>
        <label>mobile:</label>
        <input type="text" name="mobile" m-value="$form.mobile">
        <span m-style="{ display: !!$form.mobile ? 'inline' : 'none' }">
            You input: {{$parse($form.mobile)}}.
        </span>
        <span m-class="['some-class', { red: !$mobileInputStatus }]" 
              m-style="{ display: !$mobileInputStatus ? 'inline' : 'none' }">
            {{$form.mobile}} is {{$mobileInputStatus}}!
        </span>
    </div>
    <div>
        <label>age:</label>
        <select name="age" m-value="$form.age.value">
            <option m-each="$val in $form.age.options" m-value="$val">{{$val}}</option>
        </select>
    </div>
    <div>
        <label>city:</label>
        <select name="city" m-value="$form.city.value">
            <option m-each="($val, $key) in $form.city.options" m-value="$key">
                {{$val.code}}.{{$val.name}}
            </option>
        </select>
    </div>
</form>

<script>
    var store = {
        parse: function (v) {
            return parseInt(v);
        },
        validateMobile: function () {
            return /^1[3|4|5|8][0-9]\d{8}$/.test(this.form.mobile);
        },
        form: {
            gender: 'female',
            mobile: '15210001000',
            age: {
                value: '37-54',
                options: [
                    '0-18',
                    '19-36',
                    '37-54',
                    '55-200'
                ]
            },
            city: {
                value: '1',
                options: [
                    { code: 1, name: 'beijing' },
                    { code: 2, name: 'newyork' },
                    { code: 3, name: 'tokyo' },
                    { code: 4, name: 'london' },
                    { code: 5, name: 'paris' }
                ]
            }
        }
    };

    /* Watch a property mutation */
    store.mobileInputStatus = store.validateMobile();
    DMD.relate(store, {
        'form.mobile': {
            dnstream: 'mobileInputStatus',
            resultIn: function (v) {
                var newVal = store.validateMobile();
                console.log(`mobile is ${newVal}`);
                DMD.$(store, 'mobileInputStatus', newVal);
            }
        }
    });

    DMD('#form1', store);
</script>

Advance

Imperative Calling

Providing a data object:

var store = {
    value: '1'
};

Now assign a property color which would be computed from the property value.

There are two methods available, kernel and relate.

The kernel method, which is actually a constructor function, accepts two parameters besides the data object: the property and its relation object.

new domod.kernel(store, 'color', {
    resultFrom: function () {
        return isNaN(parseInt(store.value)) ? 'red' : 'green';
    }
});

The relate method, which is somehow a composition of kernel, accepts one parameter besides the data object: an object representing a map from property names to relation objects.

domod.relate(store, {
    color: {
        resultFrom: function () {
            return isNaN(parseInt(store.value)) ? 'red' : 'green';
        }
    }
});

Relation Object

A relation object can be declared with these properties:

Property | Effect :---: | :--- resultFrom | Do when the property is being got, and return the value computed. resultIn | Do when the property is being set. upstream | Other properties as the property's dependencies. dnstream | Other properties as the property's effects.

Standalone Libraries

Kernel

See the code and doc for more.

ObservableArray

Also called OArray for short. See the code and doc for more.

License

MIT