JavaScript Proprietary Library Example

From Chorke Wiki
Revision as of 02:08, 4 January 2019 by Shahed (talk | contribs) (Created page with "==Prototyping Library== <syntaxhighlight lang="js"> /** * This is the Number.lpad() method developed aim to support * oracle like lpad single rows function for JS Engine. *...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Prototyping Library

/**
 * This is the Number.lpad() method developed aim to support
 * oracle like lpad single rows function for JS Engine.
 *
 * @public
 * @package     Number
 * @class       Number.lpad()
 */
;Number.prototype.lpad||(function(){
    Number.prototype.lpad = function(width, fill, me, padded) {
        me = this.toString();
        if(me.length >= width){
            return me;
        }
        padded = new Array(width - me.length + 1);
        return padded.join(fill||'0') + me;
    };
})();
/**
 * This is the String.format() method developed aim to support
 * Java like format String 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.rpad() method developed aim to support
 * Oracle like rpad single rows function for JS Engine.
 *
 * @public
 * @package     String
 * @class       String.rpad()
 */
;String.prototype.rpad||(function(){
    String.prototype.rpad = function(width, fill, me, padded) {
        me = this;
        if(me.length >= width){
            return me;
        }
        padded = new Array(width - me.length + 1);
        return me + padded.join(fill||'0');
    };
})();
/**
 * This is the String.info() method developed aim to support
 * Java like print String 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(me){
    me = {
        $class   : 'Chorke',
        $package : 'Default',
        meta     : function(ns, tpl, out){
            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.Edu
 */
;CKi.Edu||(function($, me){
    me = {
        $package : 'CKi',
        $class   : 'CKi.Edu',
        $meta    : function(){return $.meta(me)}
    }
    $.Edu = me;
})(CKi);
/**
 * @public
 * @static
 * @package   CKi.Edu
 * @class     CKi.Edu.YouTube
 */
;CKi.Edu.YouTube||(function($, me){
    me = {
        $package : 'CKi.Edu',
        $class   : 'CKi.Edu.YouTube',
        $meta    : function(){return $.meta(me)}
    }
    $.Edu.YouTube = me;
})(CKi);
/**
 * @public
 * @static
 * @package   CKi.Edu.YouTube
 * @class     CKi.Edu.YouTube.Channel
 */
;CKi.Edu.YouTube.Channel||(function($, me, channels){
    channels = [];
    me = {
        $package : 'CKi.Edu.YouTube',
        $class   : 'CKi.Edu.YouTube.Channel',
        $meta    : function(){return $.meta(me)},
        add      : function(name, lang, liked){
            channels.push({name: name, lang: lang, like: liked});
            return channels
        },
        remove   : function(name, index){
            for(index=0;index < channels.length;index++){
                if(channels[index].name == name){
                    channels.splice(index, 1);
                    return channels;
                }
            }
            return channels;
        },
        compare  : function(prev, next) {
            if (prev.name < next.name){
                return -1;
            } else if (prev.name > next.name){
                return 1;
            }
            return 0;
        },
        sort     : function(){
            return channels.sort(me.compare);
        },
        format   : function(index, channel, name, lang, like, tpl, out){
            tpl = ' {0}. Chorke {1}, Inc.   => {2}[{3}]\n';
            out = '\n';me.sort();
            for(index=0;index < channels.length;index++){
                channel = channels[index];
                name = channel.name;
                lang = channel.lang;
                like = channel.like;
                out += tpl.format((index + 1).lpad(2), name, lang.rpad(12, ' '), like&&'x');
                out += ((index+1)%20) ? '' : '\n';
            }
            return out;
        },
        print    : function(){
            me.format().info();
        }
    }
    $.Edu.YouTube.Channel = me;
})(CKi);


Checking Library

CKi.Edu.YouTube.Channel.add('Ascriber', 'English', 1);
CKi.Edu.YouTube.Channel.add('Academia', 'English', 1);
CKi.Edu.YouTube.Channel.add('Agronomy', 'English', 1);
CKi.Edu.YouTube.Channel.print();