HTML/Web Storage: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
Line 29: Line 29:
         $class  : 'Chorke',
         $class  : 'Chorke',
         $package : 'Default',
         $package : 'Default',
         meta    : function(ns, tpl, out){
         meta    : function(ns, tpl){
             tpl = '{\n  "package" : "{0}",\n  "class"  : "{1}"\n}';
             tpl = '{\n  "package" : "{0}",\n  "class"  : "{1}"\n}';
             out = tpl.format(ns.$package, ns.$class);
             return tpl.format(ns.$package, ns.$class);
            return out;
         }
         }
     };
     };
Line 38: Line 37:
})(),
})(),
//chorke alias name
//chorke alias name
CKi=CKi||(function(me){
CKi=CKi||(function($, me){
     me.$class = 'CKi';
    me = $;
     me.$meta = function(){return me.meta(me)};
    me.$origin    = $.meta($);
     me.hasDB = function(){return typeof(Storage)!=='undefined'};
     me.$class     = 'CKi';
     me.$meta     = function(){return me.meta(me)};
     me.hasDB     = function(){return typeof(Storage)!=='undefined'};
    me.isNone    = function(obj){return me.isNull(obj)||me.isUndefine(obj)};
    me.isNull    = function(obj){return me.getType(obj)==='[object Null]'};
    me.isDate    = function(obj){return me.getType(obj)==='[object Date]'};
    me.isArray    = function(obj){return me.getType(obj)==='[object Array]'};
    me.getType    = function(obj){return Object.prototype.toString.call(obj)};
    me.isNumber  = function(obj){return me.getType(obj)==='[object Number]'};
    me.isObject  = function(obj){return me.getType(obj)==='[object Object]'};
    me.isRegExp  = function(obj){return me.getType(obj)==='[object RegExp]'};
    me.isString  = function(obj){return me.getType(obj)==='[object String]'};
    me.isBoolean  = function(obj){return me.getType(obj)==='[object Boolean]'};
    me.isFunction = function(obj){return me.getType(obj)==='[object Function]'};
    me.isUndefine = function(obj){return me.getType(obj)==='[object Undefined]'};
    me.isEmpty    = function(obj){
        return me.isNone(obj)||(me.isString(obj)&&obj.trim()==='')||(me.isArray(obj)&&obj.length===0)
              ||(me.isObject(obj)&&(function(key,flg){flg=!0;for(key in obj){if(obj.hasOwnProperty(key)){flg=!1}}return flg}()))
    };
     return me;
     return me;
})(Chorke);
})(Chorke);

Revision as of 00:40, 29 September 2018

Prototyping Library

//prototyping for 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;
        });
    };
})();

//prototyping for console info
;String.prototype.info||(function(){
    String.prototype.info = function() {
        if(console && console.info){
            console.info(this.toString());
        }
        return this;
    };
})();

Proprietary Library

//chorke main namespace/package
var Chorke=Chorke||(function(me){
    me = {
        $class   : 'Chorke',
        $package : 'Default',
        meta     : function(ns, tpl){
            tpl = '{\n  "package" : "{0}",\n  "class"   : "{1}"\n}';
            return tpl.format(ns.$package, ns.$class);
        }
    };
    return me;
})(),
//chorke alias name
CKi=CKi||(function($, me){
    me = $;
    me.$origin    = $.meta($);
    me.$class     = 'CKi';
    me.$meta      = function(){return me.meta(me)};
    me.hasDB      = function(){return typeof(Storage)!=='undefined'};
    me.isNone     = function(obj){return me.isNull(obj)||me.isUndefine(obj)};
    me.isNull     = function(obj){return me.getType(obj)==='[object Null]'};
    me.isDate     = function(obj){return me.getType(obj)==='[object Date]'};
    me.isArray    = function(obj){return me.getType(obj)==='[object Array]'};
    me.getType    = function(obj){return Object.prototype.toString.call(obj)};
    me.isNumber   = function(obj){return me.getType(obj)==='[object Number]'};
    me.isObject   = function(obj){return me.getType(obj)==='[object Object]'};
    me.isRegExp   = function(obj){return me.getType(obj)==='[object RegExp]'};
    me.isString   = function(obj){return me.getType(obj)==='[object String]'};
    me.isBoolean  = function(obj){return me.getType(obj)==='[object Boolean]'};
    me.isFunction = function(obj){return me.getType(obj)==='[object Function]'};
    me.isUndefine = function(obj){return me.getType(obj)==='[object Undefined]'};
    me.isEmpty    = function(obj){
        return me.isNone(obj)||(me.isString(obj)&&obj.trim()==='')||(me.isArray(obj)&&obj.length===0)
               ||(me.isObject(obj)&&(function(key,flg){flg=!0;for(key in obj){if(obj.hasOwnProperty(key)){flg=!1}}return flg}()))
    };
    return me;
})(Chorke);

Database Engine

//chorke database engine
;CKi.db||(function($, win, me, session, local){
    session = win && win.sessionStorage;
    local   = win && win.localStorage;
    me      = {
        $package : 'CKi',
        $class   : 'CKi.db',
        $meta    : function(){return $.meta(me)},
        getData  : function(name, secured, store){
            if($.hasDB() && name && (store = secured ? session : local)){
                return JSON.parse(store.getItem(name));
            }
        },
        setData  : function(name, data, secured, store){
            if($.hasDB() && name && data && (store = secured ? session : local)){
                store.setItem(name, JSON.stringify(data));
                return true;
            }
        }
    };
    $.db = me;
})(CKi, window);

Local Database

//permanent/local storage database
;CKi.db.pdb||(function($, win, me){
    me  = {
        $package : 'CKi.db',
        $class   : 'CKi.db.pdb',
        $meta    : function(){return $.meta(me)},
        getData  : function(name){
            if($.hasDB() && name){
                return (me[name] = $.db.getData(name));
            }
        },
        setData  : function(name, data){
            if($.hasDB() && name && data){
                return $.db.setData(name, (me[name] = data));
            }
        }
    };
    $.db.pdb = me;
})(CKi, window);

Session Database

//temporary/session storage database
;CKi.db.sdb||(function($, win, secured, me){
    me  = {
        $package : 'CKi.db',
        $class   : 'CKi.db.sdb',
        $meta    : function(){return $.meta(me)},
        getData  : function(name){
            if($.hasDB() && name && secured){
                return (me[name] = $.db.getData(name, secured));
            }
        },
        setData  : function(name, data){
            if($.hasDB() && name && data && secured){
                return $.db.setData(name, (me[name] = data), secured);
            }
        }
    };
    $.db.sdb = me;
})(CKi, window, true);

Checking Output

//insert/store data into client db
CKi.db.sdb.setData('T00I00', [{id: 1, name: 'Selina'}, {id: 2, name: 'Daliya'}]);
CKi.db.pdb.setData('T00I00', [{id: 1, name: 'Raiyan'}, {id: 2, name: 'Rashid'}]);

//retrieve data from client db
CKi.db.sdb.getData('T00I00');
CKi.db.pdb.getData('T00I00');

References