JavaScript Proprietary Library Example: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
Line 141: | Line 141: | ||
$meta : function(){return $.meta(me)}, | $meta : function(){return $.meta(me)}, | ||
add : function(name, lang, liked){ | add : function(name, lang, liked){ | ||
channels.push({name: name, lang: lang, like: liked}); | channels.push({name: name, lang: lang||'English', like: liked}); | ||
return me; | return me; | ||
}, | }, |
Revision as of 02:28, 4 January 2019
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||'English', like: liked});
return me;
},
remove : function(name, index){
for(index=0;index < channels.length;index++){
if(channels[index].name == name){
channels.splice(index, 1);
return me;
}
}
return me;
},
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();
return me;
}
}
$.Edu.YouTube.Channel = me;
})(CKi);
Checking Library
(function(Channel){
Channel.add('Ascriber', 'English', 0).add('Academia', 'English', 1).add('Agronomy', 'English', 1);
Channel.add('Ascriber', 'English', 1).add('Academia', 'English', 0).add('Agronomy', 'English', 1);
Channel.add('Ascriber', 'English', 1).add('Academia', 'English', 1).add('Agronomy', 'English', 0);
Channel.print();
})(CKi.Edu.YouTube.Channel);