Line 1: |
Line 1: |
| // License: GPL | | // License: GPL |
| // | | // |
− | function wbContainer( universe, location ) {
| + | function wbContainer( universe, location ) { |
| | | |
− | this.main = interjson( checkdive( universe, location ));
| + | this.main = interjson( checkdive( universe, location )); |
− | var container = this;
| + | var the = this.main; |
| + | |
| + | this.checkentity = function ( paths ) { |
| + | if ( ( the != undefined ) && ( paths != undefined ) ){ |
| + | return proceed( the, paths ); |
| + | } |
| + | } |
| | | |
− | this.checkentity = function ( rel_id_list, paths ) {
| + | // the list of private helper functions, could not be accessed from outside |
− | var return_obj = [],
| + | function indexinterpolate(obj,i) { return (obj[i] !== undefined) ? obj[i] : obj}; |
− | temporary_obj = [],
| + | // helper function for checking a part of an array exists |
− | baseRelation;
| + | function indexcheck(obj,i) { if ( obj !== undefined) { return ( obj[i] !== undefined ) ? obj[i] : undefined } }; |
− | if ( Object.keys(rel_id_list).length > 0 ) {
| + | // dive selects matrix.a.b.c.d from the array called matrix and 'a.b.c.d' as string |
− | Object.keys(rel_id_list).forEach( function( item ){
| + | // 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. |
− | temporary_obj = [];
| + | function dive(array, read){ |
− | baseRelation = checkdive( container.main, rel_id_list[item][0] );
| + | if ( array !== undefined ) { |
− | rise( temporary_obj, depthdive( baseRelation, paths ));
| + | return (read + '').split('.').reduce(indexinterpolate, array); |
− | if ( Object.keys(temporary_obj).length > 0 ) {
| + | } |
− | Object.keys(temporary_obj).forEach( function (hit) {
| + | } |
− | if ( temporary_obj[hit] == rel_id_list[item][1] ) {
| + | function check(array, read){ |
− | return_obj.push(rel_id_list[item]);
| + | if ( array !== undefined ) { |
− | }
| + | return (read + '').split('.').reduce(indexcheck, array); |
− | });
| + | } |
− | } else if ( temporary_obj == rel_id_list[item][1] ) {
| + | } |
− | return_obj.push(rel_id_list[item]);
| |
− | }
| |
− | });
| |
− | }
| |
− | return return_obj;
| |
− | }
| |
| | | |
− | // the list of private helper functions, could not be accessed from outside
| + | function proceed(array, read) { |
− | function indexinterpolate(obj,i) { return (obj[i] !== undefined) ? obj[i] : obj};
| + | var ret_obj = {}, |
− | // helper function for checking a part of an array exists
| + | temp_obj = array, |
− | function indexcheck(obj,i) { if ( obj !== undefined) { return (obj[i] !== undefined) ? obj[i] : undefined } };
| + | // Have a holder for instructional control return array |
− | // dive selects matrix.a.b.c.d from the array called matrix and 'a.b.c.d' as string
| + | success_o_meter = true, |
− | // 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.
| + | future_instructional_return = []; |
− |
| + | |
− | function dive(array, read){
| + | //|||||||||||||||||||||||||||||||||||||||||||||||||||||[ I N S T R U C T I O N S C O N T R O L ]|||||||||||||||||||||||||||||||||||||||||||||||||||||| |
− | return (read + '').split('.').reduce(indexinterpolate, array);
| |
− | }
| |
− |
| |
− | function check(array, read){
| |
− | if ( array !== undefined ) {
| |
− | return (read + '').split('.').reduce(indexcheck, array);
| |
− | }
| |
− | }
| |
− |
| |
− | function checkdive( array, read){
| |
− | if ( check(array, read) !== undefined) {
| |
− | return dive(array, read);
| |
− | } else if ( Array.isArray(array) ) {
| |
− | return complexcheck( array, read );
| |
− | }
| |
− | }
| |
| | | |
− | function complexcheck(array, read){
| + | // Init program, check if instructions are an object : |
− | var ret_obj;
| + | if ( ( !Array.isArray(read) ) && ( Object.keys(read).length > 0 ) ) { |
− | Object.keys(array).forEach( function(item){
| |
− | rise( ret_obj, checkdive(array[item], read)) ;
| |
− | });
| |
− | return ret_obj;
| |
− | }
| |
| | | |
− | function depthdive(array, read){
| + | |
− | var ret_obj,
| + | Object.keys(read).forEach( function(path) { |
− | temp_obj = array;
| + | console.log("instructions are an object, in path " + path); |
− | if ( Object.keys(read).length > 0 ) {
| |
− | Object.keys(read).forEach( function(path) {
| |
− | temp_obj = checkdive(temp_obj, path);
| |
− | });
| |
− | rise(ret_obj, temp_obj);
| |
− | return ret_obj;
| |
− | } else {
| |
− | return checkdive(array, read);
| |
− | }
| |
− | }
| |
| | | |
− | function rise(array, to_push) {
| + | //||||||||||||||||||||||||||[ Check if elements of instruction object are arrays themselves ]|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |
− | if ( to_push !== undefined) {
| + | |
− | if ( Object.keys(to_push).length > 0 ){
| + | if ( Array.isArray (read[path]) ) { |
− | Object.keys(to_push).forEach( function(item){
| + | var future_return_partition = temp_obj; |
− | if ( array !== undefined ) {
| + | Object.keys(read[path]).forEach( function( path2 ) { |
− | array.push(to_push[item]);
| |
− | }
| |
− | });
| |
− | } else {
| |
− | array.push(to_push);
| |
− | }
| |
− | }
| |
− | }
| |
| | | |
− | function interjson( obj ) {
| + | console.log("instructions are an array of arrays, going through " + read[ path ][ path2 ] ); |
− | try {
| + | |
− | return JSON.parse( obj )
| + | if ( data_function( future_return_partition, read[ path ][ path2 ] ) !== future_return_partition ){ |
− | } catch (e) {
| + | future_return_partition = data_function( future_return_partition, read[ path ][ path2 ] ); |
− | // if not JSON, do silly things
| + | } else { |
− | console.log('not a json ¯\_(ツ)_/¯');
| + | success_o_meter = false; |
− | };
| + | } |
− | }
| |
− |
| |
− | }
| |
| | | |
− | mw.loader.load( '//wiki.personaldata.io/w/index.php?title=User:Abel/Experimaintal.js&action=raw&ctype=text/javascript' );
| + | //|||||||||||||||||||||||||||||||||||||||||||||||||||[ I N S T R U C T I O N S C O N T R O L ||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |
| + | |
| + | }); |
| + | if ( success_o_meter === true ) { |
| + | // Put the result of the travel along the paths in array element of array into the future_instructional_return bunch |
| + | console.log("arrived at") |
| + | console.log(future_return_partition); |
| + | future_instructional_return.push(future_return_partition); |
| + | } else { |
| + | console.log("seeking derailed"); |
| + | success_o_meter = true; |
| + | future_instructional_return.push( undefined ); |
| + | } |
| + | |
| + | } |
| + | |
| + | //|||||||||||||||||| If instruction object element is not an array anymore, lets just do one step |||||||||||||||||||||||||||||||||||||||||||||||||||||||| |
| + | |
| + | else { |
| + | |
| + | // INCLUDE step block with read[path] |
| + | if ( data_function( temp_obj, read[ path ] ) !== temp_obj ){ |
| + | future_instructional_return.push( data_function( temp_obj, read[ path ] ) ); |
| + | } else { |
| + | future_instructional_return.push( undefined ); |
| + | } |
| + | |
| + | } |
| + | }); |
| + | |
| + | temp_obj = future_instructional_return; |
| + | } |
| + | |
| + | //||||||||||||[ If we are not in instruction object, let's see if we have an instruction array ]|||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |
| + | |
| + | else if ( Array.isArray(read) ) { |
| + | // If so let's follow the array of instructions |
| + | Object.keys( read ).forEach( function ( ect ) { |
| + | if ( data_function( temp_obj, read[ ect ] ) !== temp_obj ){ |
| + | |
| + | temp_obj = data_function( temp_obj, read[ ect ] ); |
| + | |
| + | } else { |
| + | |
| + | success_o_meter = false; |
| + | |
| + | } |
| + | }); |
| + | } |
| + | |
| + | //||||||||||||[ If we have a single instruction, let's try following that ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |
| + | |
| + | else { |
| + | |
| + | if ( data_function( temp_obj, read ) !== temp_obj ){ |
| + | |
| + | temp_obj = data_function( temp_obj, read ); |
| + | |
| + | } else { |
| + | |
| + | success_o_meter = false; |
| + | |
| + | } |
| + | } |
| + | if ( success_o_meter === true ) { |
| + | return temp_obj; |
| + | } else { |
| + | return undefined; |
| + | } |
| + | } |
| + | |
| + | //|||||||||||||||||||||||||||||||||||||||||||||||||||||[ S T E P B L O C K ]|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |
| + | |
| + | function data_function ( temp_obj, moving ) { |
| + | var future_temp_obj = []; |
| + | if ( Array.isArray( temp_obj ) ) { |
| + | console.log("data is on an array level"); |
| + | Object.keys( temp_obj ).forEach( function ( ect ) { |
| + | |
| + | // if we are on an array level in the data, lets iterate through the array |
| + | |
| + | console.log("Within partition: " + ect ); |
| + | future_temp_obj.push( checkdive( temp_obj[ ect ], moving ) ); |
| + | |
| + | }); |
| + | temp_obj = future_temp_obj; |
| + | |
| + | } else { |
| + | |
| + | // if we are not on an array level, proceed with the path |
| + | |
| + | console.log("data is on an objective level"); |
| + | future_temp_obj = checkdive( temp_obj, moving ); |
| + | if ( future_temp_obj !== undefined ){ |
| + | temp_obj = future_temp_obj; |
| + | } else { |
| + | console.log('nothing to see here') |
| + | } |
| + | |
| + | } |
| + | |
| + | return temp_obj; |
| + | } |
| + | |
| + | //|||||||||||||||||||||||||||||||||||||||||||||||||||||[ E N D O F S T E P B L O C K ]||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |
| + | |
| + | function checkdive( array, read){ |
| + | if ( check(array, read) !== undefined) { |
| + | return dive(array, read); |
| + | } else if ( Array.isArray(array) ) { |
| + | var potential_return = []; |
| + | Object.keys(array).forEach( function(item){ |
| + | if ( check(array[item], read) !== undefined ) { |
| + | potential_return.push( dive( array[item], read) ); |
| + | } |
| + | }); |
| + | return potential_return; |
| + | } |
| + | if ( array === read ) { |
| + | return read; |
| + | } |
| + | if (Object.keys( array ).length > 0 ){ |
| + | Object.keys( array ).forEach( function ( piece ) { |
| + | if ( array[ piece ] === read ) { return read }; |
| + | }); |
| + | } |
| + | } |
| + | |
| + | function interjson( obj ) { |
| + | try { |
| + | return JSON.parse( obj ) |
| + | } catch (e) { |
| + | // if not JSON, do silly things |
| + | console.log('not a json ¯\_(ツ)_/¯'); |
| + | }; |
| + | } |
| + | |
| + | } |