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

mayjs

v0.2.6

Published

[![Build Status](http://travis-ci.org/zhongxingdou/mayjs.png)](http://travis-ci.org/zhongxingdou/mayjs) [![NPM version](https://badge.fury.io/js/mayjs.png)](http://badge.fury.io/js/mayjs) [![Views](https://sourcegraph.com/api/repos/github.com/zhongxingdou

Downloads

8

Readme

Build Status NPM version Views

NPM

Principle

  • Simple, but clear

  • Focus better javascript

  • Keep JavaScript as JavaScript

Features

  • object wrapping
    similary $() in jQuery but supported wrap any object

  • module
    inspirits by module of Ruby

  • classes and inheritance
    bring onExtend() and onInitialize() hook and more

  • interface
    for documentation implicit interface and validate objects

  • method overload
    help you clean up type assertion of paramerters in function head lines

  • keyword-like function
    a series of function to simplify complex expressions and repetitive code

Install

install for nodejs

npm intall mayjs

install for browser

bower install mayjs

Getting started

The quickest way to start using May.js in your project, is by simply including may.js

<!DOCTYPE html>
<html>
	<head>
		<script src="may.js"></script>
		<script>
			M(function($, $$){
                $.reg({
                    capitalize: function(){
                        var s = this.valueOf();
                        return s.charAt(0).toUpperCase() + s.slice(1);
                    }
                }, String);

                alert($("china").capitalize()); //china => China
			});
		</script>
	</head>
	<body>
	</body>
</html>

Export May.js keyword-like function to global object

M.exportDSL(M.global); 
// so you will got $class $module $interface $fn ...

Module

var MEvent = $module({
    onIncluded: function(obj){
        obj.__observers = {};
    },
    subscribe: function(obj, eventName, observer){
        if(!obj.__observers[eventName]) obj.__observers[eventName] = [];
        obj.__observers[eventName].push(observer);
    },
    publish: function(obj, eventName, data){
        (obj.__observers[eventName] || []).forEach(function(observer){
            observer(data);
        });
    },
    __option__: {
        methodize: true
    }
});

var man = {};

$include(man, MEvent);

man.setName = function(name){
    this.name = name;
    this.publish("nameChanged", name);
}

man.subscribe("nameChanged", function(newName){
   console.info("name changed to:" + newName); 
});

man.setName("Hal"); // => name changed to:Hal 
man.setName("Jerry"); // => name changed to:Jerry 

Object wrap

// register a global wrapper
M.$.reg({
    mix: function(obj, src){
        for(var p in src){ obj[p] = src[p] }
    }, 
    __option__: {
        methodize: true,
        supports: [Object]
    }
});

M(function($, $$){
    //$() and $$() both use the same wrapper table

    var Jim = {};
    $(Jim).mix({
        name: "Jim",
        age: 20
    });

    Jim.name; // => Jim
    Jim.hasOwnProperty("mix"); // => false
    typeof(Jim.mix); // => undefined

    $$(Jim); //copy mix() to Jim directly
    Jim.hasOwnProperty("mix"); // => true

    //register a wrapper locally
    $.reg({
        next: function(num){
            return num + 1;
        },
        __option__: {methodize: true}
    }, Number);

    $(8).next(); // => 9
})

M.$(8).next() //Oh, no!

Classes and inheritance

M(function(){
    var Animal = $class({
        initialize: function(name){
            var _name = name || "";
            this.getName = function(){
                return _name;
            }
        },
        eat: function(food){
            return this.getName() + " is eating " + food;
        }
    });
    
    var Duck = Animal.extend({
        initialize: function(name){
            this.base(name);
        },
        eat: function(food){
            return this.base(food) + ", she is so happy: quack quack!"
        }
    });
    
    var monkey = new Animal("monkey");
    monkey.eat("banana"); // => monkey is eating banana
    
    var duck = new Duck("Litter yellow duck");
    duck.eat("grass"); // => Little yellow duck is eating grass, she is so happy: quack quack!
});

Interface

Using interface to validate object

M(function(){
    var IStudent = $interface({
        name: String,
        birthday: Date,
        "[interest]": String
    });

    var Student = $class({
        initialize: function(studentInfo){
            $support(IStudent, studentInfo);
            $mix(this, studentInfo);
        }
    });

    var Jim = new Student({
        name: "Jim",
        birthday: new Date("1988/08/08")
    });

    var Lily = new Student({
        name: "Luly",
        birthday: new Date,
        interest: "swimming"
    });

    $ensure(function(){
        var Lucy = new Student({
            name: "Lucy",
            birthday: "1990/08/08"
        });
        // => error: birthday invlid
    });
});

Implementation interface

M(function($, $$){
    var IMoveAble = $interface({
        move: Function
    });

    var Car = $class({
        initialize: function(name){
            this.name = name;
        },
        move: function(place){
            console.info(this.name + " is running to " + place);
        }
    });
    $implement(IMoveAble, Car.prototype);

    var Man = $class({
        initialize: function(name){
            this.name = name;
        },
        move: function(place){
            console.info(this.name + " is walking to " + place);
        }
    });
    $implement(IMoveAble, Man.prototype);

Register an module map to an interface

    //continued codeblock above
    var MoveStatus = $enum("Stoped", "Moving", "Arrived");

    var Marathon = $module({
        gotoPlace: function(obj, place, distance, secondsSpeed){
            obj.status = MoveStatus.Moving;
            obj.__moveTask ={
                place: place,
                distance: distance,
                speed: secondsSpeed,
                lastCounterTime: new Date 
            }
        },
        cancel: function(){
            obj.status = MoveStatus.Stoped;
        },
        checkStatus: function(obj){
            var task = obj.__moveTask;
            switch(obj.status){
                case MoveStatus.Stoped:
                    console.log(obj.name + ": Stoped");
                    break;
                case MoveStatus.Moving:
                    var movedSeconds = (new Date() - task.lastCounterTime) / 1000;
                    var movedDistance = Math.round(movedSeconds * task.speed * Math.random());
                    if(movedDistance < task.distance){
                        obj.move(task.place);
                        var remaining = task.distance - movedDistance;
                        console.log("   To " + task.place + " remaining: " + remaining);
                        task.distance = remaining; 
                        task.lastCounterTime = task.lastCounterTime;
                    }else{
                        obj.status = MoveStatus.Arrived;
                    }
                    break;
                case MoveStatus.Arrived:
                    console.info(obj.name + ": Arrived");
                    break;
            }
            return obj.status;
        },
        onIncluded: function(obj){
            obj.status = MoveStatus.Stoped;
        },
        __option__: {
            methodize: true
        }
    });

    $.reg(Marathon, IMoveAble);


    var toyota = new Car("Toyota");
    var liuXiang = new Man("LiuXiang");

    //liuXiang and toyota will wrapping by Marathon cause Car and Man both implementation IMoveAble 
    var marathonMan = $(liuXiang);
    var marathonCar = $(toyota);

    marathonCar.gotoPlace("Beijing",  3 * 1000, 20);
    marathonMan.gotoPlace("Home",  1 * 1000, 8);

    var timer = setInterval(function(){
        if(marathonMan.checkStatus() == MoveStatus.Arrived){
            console.info("==== LiuXiang Win ====")
            return clearInterval(timer);
        }
        if(marathonCar.checkStatus() == MoveStatus.Arrived){
            console.info("==== Toyota Win ====")
            clearInterval(timer);
        }
    }, 3000);
});

Method overload

may.js $overload() rely on interface and $is()

lets write a tiny jQuery

M(function($, $$){
    var supportCollectionFn = function(fn){
        return function(el){
            var self = this;
            var args = Array.prototype.slice.call(arguments, 1);
            if(el instanceof HTMLCollection || el instanceof NodeList){
                var result;
                Array.prototype.slice.call(el).forEach(function (item) {
                    result = fn.apply(self, [item].concat(args));
                });
                return result;
            }else{
                return fn.apply(self, arguments);
            }
        }
    }

    var DomHelper = {
        css: supportCollectionFn(function(el, cssProp, cssValue){
            el.style[cssProp] = cssValue;
            return this;
        }),
        html: supportCollectionFn(function(el, html){
            if(html){
                el.innerHTML = html;
            }else{
                return el.innerHTML;
            }
            return this;
        }),
        prop: supportCollectionFn(function(el, prop, propValue){
            if(propValue){
                el[prop] = propValue;
            }else{
                return el[prop];
            }
            return this;
        }),
        __option__: {
            supports: [Element.prototype, NodeList.prototype, HTMLCollection.prototype],
            methodize: true
        }
    }

    $.reg(DomHelper);

    var jjQuery = $overload([String], function(selector){
        return $(document.querySelectorAll(selector));
    }).overload([Element.prototype],function(el){
        return $(el);
    }).overload([HTMLCollection.prototype],function(els){
        return $(els);
    }).overload([String,Element.prototype], function(selector, parentElement){
        return $(parentElement.querySelectorAll(selector));
    }).overload([/^#[^ ]*/],function(id){
        return $(document.getElementById(id));
    });

    //we assume you have some html like below
    /*
        <div id="content"></div>
        <form id="myForm">
            <legend>Form1</legend>
            <fieldset>
                <label for="name">Name</label><input type="" id="name"/>
                <label for="age" >Age</label><input type="" id="age"/>
                <button>Save</button>
                <button>Reset</button>
            </fieldset>
        </form>
        <form id="myForm2">
            <legend>Form2</legend>
        </form>
     */

    jjQuery("#content").html("Hi, Mayjs!");
    jjQuery(document.body).css("backgroundColor","gray");
    jjQuery(document.forms).css("backgroundColor", "blue");
    jjQuery("button", document.forms[0]).prop("disabled", true);
    jjQuery("#myForm input").css("backgroundColor", "yellow");
})

Keyword-like function

assertion

$is("string", "hello");
$is(String, "hello");

$is(Array, []); 
$is(null, undefined);

A=$class({});
$is(A, new a);

$hasProto(A.prototype, new A);

function Person(name, age){
   $check($is(String, name), "name invalid"); 
   $check($is(Number, age), "age invalid"); 
   this.name = name;
   this.age = age;
}

var lily = new Person("lily", "16"); //throw error=> age invalid

syntactic sugar


//$obj() object extend
var a=$obj({name: "jerry", "gender":"male"});
var b=a.extend({name:"hal", age:18});
b.gender; // => male
a.isPrototypeOf(b); // => true

//$run() just run function
$run(function(){
    console.info("hello, mayjs");
});

//$ensure() Preventing errors thrown
var errorFn = function(){
    throw "some error"
}
$ensure(errorFn); // => false
$ensure(function(){}); // => undefined
$ensure(function(){ return "hello"; }); // => hello

// $methodize()
// in may.js, 'Pure function' means the function which does not refer to this object
function capitalize(string){
    return string.charAt(0).toUpperCase() + string.slice(1);
}

var china = new String("china"); //value type object can't have members

// string "china" will be binding as first parameter for capitalize
china.capitalize = $methodize(capitalize, china); 

china.capitalize(); // => China


// $overwrite()
var a={hi: function(){
    return "hello";
}}

$overwrite(a, "hi", function(hi){
    return hi() + " world!"; 
})

a.hi(); // => hello world!

// $merge()
var man = function(option){
    option = $merge({name: "noname"}, {age: 0}, option);
    return option;
}

var hal = man({name: "hal", age: 20}); 
hal.name; // => hal
hal.age; // => 20


// $mix()
var a = {};
$mix(a, {name: "jim"});
a.name; // => jim


// $enum()
var Gender = $enum("Male","Female")

// both are unique object
typeof Gender.Male // => object
typeof Gender.Female // => object

var Gender2 = {
    male: 1,
    female: 2
}
Gender2 == $enum(Gender2); // => true

License

Copyright (c) 2015 Zhong Xingdou Licensed under the MIT license.