How to build Proprietary JavaScript Library

From Chorke Wiki
Revision as of 04:41, 10 October 2018 by Shahed (talk | contribs) (→‎References)
Jump to navigation Jump to search

Prototyping Library

/**
 * This is the String.format() method developed aim to support
 * format String like java in perspectives of JS Engine.
 *
 * @public
 * @package     String
 * @class       String.format()
 */
;String.prototype.format||(function(){
    String.prototype.format = function() {
        var args = arguments;
        return this.replace(/{(\d+)}/g, function(match, number) {
            return typeof args[number] != 'undefined' ? args[number] : match;
        });
    };
})();
/**
 * This is the String.info() method developed aim to support
 * print String like java in perspectives of JS Engine.
 *
 * @public
 * @package     String
 * @class       String.info()
 */
;String.prototype.info||(function(){
    String.prototype.info = function() {
        if(console && console.info){
            console.info(this.toString());
        }
        return this;
    };
})();

Proprietary Library

/**
 * @public
 * @class     Chorke
 * @package   Default
 */
var Chorke=Chorke||(function(){
    var me = {
        $class   : 'Chorke',
        $package : 'Default',
        meta     : function(ns){
            var tpl = '{\n  "package" : "{0}",\n  "class"   : "{1}"\n}',
                out = tpl.format(ns.$package, ns.$class);
            return out;
        }
    };
    return me;
})(),

/**
 * @public
 * @class     CKi
 * @package   Default
 */
CKi=CKi||(function(me){
    me.$class = 'CKi';
    me.$meta   = function(){return me.meta(me)};
    return me;
})(Chorke);

/**
 * @public
 * @static
 * @package   CKi
 * @class     CKi.biz
 */
;CKi.biz||(function($){
    var me = {
        $package : 'CKi',
        $class   : 'CKi.biz',
        $meta    : function(){return $.meta(me)}
    }
    $.biz = me;
})(CKi);

/**
 * @public
 * @static
 * @package   CKi
 * @class     CKi.edu
 */
;CKi.edu||(function($){
    var me = {
        $package : 'CKi',
        $class   : 'CKi.edu',
        $meta    : function(){return $.meta(me)}
    }
    $.edu = me;
})(CKi);

/**
 * @public
 * @static
 * @package   CKi.edu
 * @class     CKi.edu.m00
 */
;CKi.edu.m00||(function($){
    var me = {
        $package : 'CKi.edu',
        $class   : 'CKi.edu.m00',
        $meta    : function(){return $.meta(me)}
    }
    $.edu.m00 = me;
})(CKi);

/**
 * @public
 * @static
 * @package   CKi.edu.m00
 * @class     CKi.edu.m00.F00T001
 */
;CKi.edu.m00.F00T001||(function($){
    var me = {
        $package : 'CKi.edu.m00',
        $class   : 'CKi.edu.m00.F00T001',
        $meta    : function(){return $.meta(me)},
        add      : function(a,b){
            return a+b;
        },
        sub      : function(a,b){
            return a-b;
        }
    }
    $.edu.m00.F00T001 = me;
})(CKi);

/**
 * @public
 * @static
 * @package   CKi.edu.m00
 * @class     CKi.edu.m00.C00T001
 */
;CKi.edu.m00.C00T001||(function($){
    var me = {
        $package : 'CKi.edu.m00',
        $class   : 'CKi.edu.m00.C00T001',
        $meta    : function(){return $.meta(me)},
        mul      : function(a,b){
            return a*b;
        },
        div      : function(a,b){
            return a/b;
        }
    }
    $.edu.m00.C00T001 = me;
})(CKi);
/**
 * This is the CKi.info() method developed aim to verify
 * it's metadata information.
 *
 * @public
 * @package     CKi
 * @class       CKi.info()
 */
;CKi.info||(function(){
    CKi.info = function() {
        return CKi.$meta().info();
    };
})();

Checking Library

//console.log('Hello {0}'.format('Dhaka'));
//'Hello {0}'.format('Dhaka').info();
//'Hello Dhaka'.info();

CKi.info();
CKi.biz.$meta().info();
CKi.edu.$meta().info();
CKi.edu.m00.$meta().info();
CKi.edu.m00.F00T001.$meta().info();
CKi.edu.m00.C00T001.$meta().info();

(function(a, b, F00T001, C00T001){
    '{0} + {1} = {2}'.format(a, b, F00T001.add(a, b)).info();
    '{0} - {1} = {2}'.format(a, b, F00T001.sub(a, b)).info();
    '{0} * {1} = {2}'.format(a, b, C00T001.mul(a, b)).info();
    '{0} / {1} = {2}'.format(a, b, C00T001.div(a, b)).info();
}(20, 10, CKi.edu.m00.F00T001, CKi.edu.m00.C00T001));

References