ObjectTools

by juvian

JavaScript

function ObjectTools(){
    
}
ObjectTools.prototype.simpleGet=function(obj, str, value){
        if (typeof str == 'string')
            return this.simpleGet(obj,str.split('.'), value);
        else if (str.length==1 && value!==undefined)
            return obj[str[0]] = value;
        else if (str.length==0)
            return obj;
        else
            return this.simpleGet(obj[str[0]],str.slice(1), value);
}
        ObjectTools.prototype.edit = function (obj, fields, overwrite) {
            
            function travel(container, obj) { //function recursiva para no sobreescribir objetos (modifiers)
                for (var i in obj) { // por cada campo a modificar
                    if (typeof (obj[i]) == 'object' && !(obj[i] instanceof Array)) { //si es objeto pero no array
                        if(!container.hasOwnProperty(i)){
                            container[i]={};
                        }
                        travel(container[i], obj[i]);
                    } else {
                        if(!container.hasOwnProperty(i) || overwrite){
                            container[i] = obj[i];
                        }
                    }
                }
            }

            travel(obj, fields);

        } 


ObjectTools.prototype.get = function (obj, str, params) {

    params=params || {};
    this.edit(params,{ignoreCase: true, index:0}, false);
    var matches = [];
    var strArr = str.split('.');
  
    
    function isMatch(str, match, ignoreCase){
        if(ignoreCase){
            match=match.toLowerCase();
            str=str.toLowerCase();
        }
        if(match.split('*').join('')=='' ) return true;
        if( match.length > str.length) return false;
        var index=0;
        var lastMatch=-1;
        for(var i=0;i<str.length;i++){
            var a = str[i];
            var b = match[index];
            var hasNext=index+1 < match.length;
            if(a==b){
                if(hasNext){
                   ...