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

mixinify

v1.0.0

Published

A small library to create factories with useful mixins-based inheritence. Inspired by React's createClass method.

Downloads

10

Readme

mixin.js

A small library to create powerful factories with useful mixins-based inheritence. Inspired by React.js's createClass factory method.

var MyLib.classFactory = mixin({

    getInitialStat: mixin.policy.method.MERGE_RESULT,

    render: mixin.policy.method.REQUIRED_ONCE,

    onStart: mixin.policy.method.DEFINE_MANY,

    updateComponent: mixin.policy.method.OVERRIDABLE

},{
    initialize: function () {

    },

});

This library is heavily inspired by the behavior of React's createClass method. This factory method has subtle complexity. mixins are able to be defined, and the mixins allow overriding of some methods, and special handling of others such as getInitialState() and propTypes.

Using the mixinjs library, we could create a factory method very similar to React's createClass method as shown below:

var mixin = require('mixin');

var React = {};

React.createClass = mixin({

    // this is provided by default
    //mixins: mixin.policy.method.DEFINE_MANY,

    // this is provided by default
    //statics: mixin.policy.method.DEFINE_MANY,

    propTypes: mixin.policy.object.MERGE,

    contextTypes: mixin.policy.object.MERGE,

    childContextTypes: mixin.policy.object.MERGE,

    getDefaultProps: mixin.policy.method.MERGE_RESULT,

    getInitialState: mixin.policy.method.MERGE_RESULT,

    getChildContext: mixin.policy.method.MERGE_RESULT,

    render: mixin.policy.method.REQUIRED_ONCE,

    componentWillMount: mixin.policy.method.DEFINE_MANY,

    componentDidMount: mixin.policy.method.DEFINE_MANY,

    componentWillReceiveProps: mixin.policy.method.DEFINE_MANY,

    shouldComponentUpdate: mixin.policy.method.REQUIRED_ONCE,

    componentWillUpdate: mixin.policy.method.DEFINE_MANY,

    componentDidUpdate: mixin.policy.method.DEFINE_MANY,

    componentWillUnmount: mixin.policy.method.DEFINE_MANY,

    updateComponent: mixin.policy.method.OVERRIDABLE

}, {
    initialize: function (props, context) {
        this.props = props;
        this.context = context;
        this.state = this.getInitialState();
    },

    isMounted: function () {

    },

    setProps: function (newProps, callback) {

    },

    replaceProps: function (newProps, callback) {

    }
});

Mixinjs has the following function signature:

var factory = mixin(policy [, baseImplementation]);

Policies

policy is an object with prop names and their corresponding policy declarations. The following policy declarations are available by default:

Policy | Behavior ------ | -------- mixin.policy.method.REQUIRED_ONCE | The corresponding method can only be implemented once. Multiple definitions will throw mixin.policy.method.DEFINE_MANY | The corresponding method can be defined as many times as you want. When calling the method, each definition will be invoked sequentially. mixin.policy.method.MERGE_RESULT | The corresponding method can be defined as many times as you want. It is expected that the method definitions return objects, and the results of each are merged together. mixin.policy.method.OVERRIDABLE | The corresponding method can be defined as many times as you want, but only the bottom-most definition will be used. mixin.policy.object.REQUIRED_ONCE | The corresponding property can only be defined once. Multiple definitions will throw. mixin.policy.object.MERGE | The corresponding property can be defined as many times as you want, and each definition will be merged together. mixin.policy.object.OVERRIDABLE | The corresponding property can be defined as many times as you want, but only the bottom-most definition will be used.

Add your own

You can add your own policy by making the following call:

mixin.addPolicy("MY_CUSTOM_POLICY", function (Constructor, propName, prop, classPolicy) {
    // do what you need to do here. Default behavior could be emulated as:
    Constructor.prototype[propName] = prop;
});

Default Policy Behaviors

By default, each class factory created using mixin has the following policy defined:

{
    /**
     * An array of Mixin objects to include when defining your class. Recursive.
     *
     * @type {array}
     * @optional
     */
    mixins: "MIXINS",

    /**
     * An object containing properties and methods that should be defined on
     * the class's constructor instead of its prototype (static methods).
     *
     * @type {object}
     * @optional
     */
    statics: "STATICS",

    /**
     * A function that can be used to initialize an instance
     *
     * @type {function}
     * @optional
     */
    initialize: mixin.policy.method.DEFINE_MANY,

    /**
     * Mainly for debugging purposes. This is the displayName applied to the constructor function.
     *
     * @type {String}
     * @optional
     */
    displayName: "CONSTRUCTOR_DISPLAY_NAME"
}

You can use an initialize method to any mixin or implementation and it will be used on instance initialization.