Difference between revisions of "User:Abel/WbProcessor.js"

From Wikibase Personal data
Jump to navigation Jump to search
m
m
Line 1: Line 1:
 
// License: GPL
 
// License: GPL
 
//  
 
//  
 +
 
console.log('Wbprocessor loading');
 
console.log('Wbprocessor loading');
 
function wbContainer( universe, location ) {
 
function wbContainer( universe, location ) {
Line 36: Line 37:
 
         } catch (e) {
 
         } catch (e) {
 
     // if not JSON, do silly things
 
     // if not JSON, do silly things
     consol.log('not a json ¯\_(ツ)_/¯');
+
     console.log('not a json ¯\_(ツ)_/¯');
 
     };
 
     };
 
     }  
 
     }  
Line 42: Line 43:
 
     function get_Y_from_X( obj, B ){
 
     function get_Y_from_X( obj, B ){
 
         if ( check(obj, B) !==  undefined ) {
 
         if ( check(obj, B) !==  undefined ) {
             consol.log( "entity is defined" )
+
             console.log( "entity is defined" )
 
             return dive(obj, B);
 
             return dive(obj, B);
 
         } else {  
 
         } else {  
 
             for (var i = 0; i < obj.length; i++ ){
 
             for (var i = 0; i < obj.length; i++ ){
 
                 if ( check(obj[i], B) !==  undefined ) {
 
                 if ( check(obj[i], B) !==  undefined ) {
                     consol.log( "entity is defined within index " + i );
+
                     console.log( "entity is defined within index " + i );
 
                     return dive(obj[i], B);           
 
                     return dive(obj[i], B);           
 
                 };
 
                 };
 
                 Object.keys(obj).forEach( function(item) {
 
                 Object.keys(obj).forEach( function(item) {
 
                     if ( check(obj[item], B) !==  undefined ) {
 
                     if ( check(obj[item], B) !==  undefined ) {
                         consol.log( "entity is defined within associative element" + item )
+
                         console.log( "entity is defined within associative element" + item )
 
                         return dive(obj[item], B);           
 
                         return dive(obj[item], B);           
 
                     };
 
                     };
Line 63: Line 64:
 
     function check_Y_in_X(obj, to_check){
 
     function check_Y_in_X(obj, to_check){
 
         if (obj === to_check) {  
 
         if (obj === to_check) {  
             consol.log( "entity is confirmed");
+
             console.log( "entity is confirmed");
             return true  
+
             return true;
 
         };  
 
         };  
 
         for (var i = 0; i < obj.length; i++ ){
 
         for (var i = 0; i < obj.length; i++ ){
 
             if (obj[i] === to_check) {  
 
             if (obj[i] === to_check) {  
                 consol.log( "entity is confirmed within index " + i );
+
                 console.log( "entity is confirmed within index " + i );
                 return true  
+
                 return true;
 
             };
 
             };
 
         }
 
         }
 
         Object.keys(obj).forEach( function(item) {
 
         Object.keys(obj).forEach( function(item) {
 
             if (obj[item] === to_check) {  
 
             if (obj[item] === to_check) {  
                 consol.log( "entity is confirmed within associative element" + item )
+
                 console.log( "entity is confirmed within associative element" + item )
                 return true  
+
                 return true;
 
             };  
 
             };  
 
         });
 
         });

Revision as of 06:58, 14 May 2019

// License: GPL
// 

console.log('Wbprocessor loading');
function wbContainer( universe, location ) {

    this.main = interjson( get_Y_from_X( universe, location ));

    this.checkentity = function ( rel_id_list, inner_location, property ) {
        var return_obj = {};
        Object.keys(rel_id_list).forEach( function(item){
            if ( check( this.main, rel_id_list[item][0] ) != undefined ) {
                var primary_part = get_Y_from_X( this.main, rel_id_list[item][0] );
                if ( check_Y_in_X ( primary_part, (property) ? inner_location + '.' + property : inner_location ) ) {
                    return_obj[item] = rel_id_list[item];
                }
            }
        });
        return return_obj;
    }

    // the list of private helper functions, could not be accessed from outside
    function indexinterpolate(obj,i) { return  (obj[i] != undefined) ? obj[i] : obj}; 
    // helper function for checking a part of an array exists
    function indexcheck(obj,i) {  return  (obj[i] != undefined) ? obj[i] : undefined };
    // dive selects matrix.a.b.c.d from the array called matrix and 'a.b.c.d' as string
    // if a.b.c.d does not exists, it returns the substructure until the substructure exists, if d does not exist, it returns matrix.a.b.c e.g.
    function dive(array, read){
        return read.split('.').reduce(indexinterpolate, array);
    }
    function check(array, read){
        return read.split('.').reduce(indexcheck, array);
    }
    function interjson( obj ) {
        try {
            return JSON.parse( obj )
        } catch (e) {
    // if not JSON, do silly things
    console.log('not a json ¯\_(ツ)_/¯');
    };
    } 
    // Get Y from X whether its a single object or an arrayish object
    function get_Y_from_X( obj, B ){
        if ( check(obj, B) !==  undefined ) {
            console.log( "entity is defined" )
            return dive(obj, B);
        } else { 
            for (var i = 0; i < obj.length; i++ ){
                if ( check(obj[i], B) !==  undefined ) {
                    console.log( "entity is defined within index " + i );
                    return dive(obj[i], B);           
                };
                Object.keys(obj).forEach( function(item) {
                    if ( check(obj[item], B) !==  undefined ) {
                        console.log( "entity is defined within associative element" + item )
                        return dive(obj[item], B);           
                    };
                });
            }
        }
    }

    // Check if Y can be found in X whether X is a single value, an indexed array, or an associative array
    function check_Y_in_X(obj, to_check){
        if (obj === to_check) { 
            console.log( "entity is confirmed");
            return true;
        }; 
        for (var i = 0; i < obj.length; i++ ){
            if (obj[i] === to_check) { 
                console.log( "entity is confirmed within index " + i );
                return true;
            };
        }
        Object.keys(obj).forEach( function(item) {
            if (obj[item] === to_check) { 
                console.log( "entity is confirmed within associative element" + item )
                return true;
            }; 
        });
        return false;
    }
}