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

From Wikibase Personal data
Jump to navigation Jump to search
m
m
Line 3: Line 3:
  
 
function wbContainer( universe, location ) {
 
function wbContainer( universe, location ) {
 +
 +
Debug_switch = false;
  
 
this.main = interjson( checkdive( universe, location ));
 
this.main = interjson( checkdive( universe, location ));
Line 38: Line 40:
 
})
 
})
 
}).then( function(result) {
 
}).then( function(result) {
console.log(result);
+
debuglog(result);
 
return result;
 
return result;
 
});
 
});
Line 75: Line 77:
 
 
 
Object.keys(read).forEach( function(path) {
 
Object.keys(read).forEach( function(path) {
console.log("instructions are an object, in path " + path);
+
debuglog("instructions are an object, in path " + path);
  
 
//||||||||||||||||||||||||||[ Check if elements of instruction object are arrays themselves ]||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
 
//||||||||||||||||||||||||||[ Check if elements of instruction object are arrays themselves ]||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Line 83: Line 85:
 
Object.keys(read[path]).forEach( function( path2 ) {
 
Object.keys(read[path]).forEach( function( path2 ) {
  
console.log("instructions are an array of arrays, going through " + read[ path ][ path2 ] );
+
debuglog("instructions are an array of arrays, going through " + read[ path ][ path2 ] );
 
 
 
if ( data_function( future_return_partition, read[ path ][ path2 ] ) != future_return_partition ){
 
if ( data_function( future_return_partition, read[ path ][ path2 ] ) != future_return_partition ){
Line 96: Line 98:
 
if ( success_o_meter === true ) {
 
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
 
// Put the result of the travel along the paths in array element of array into the future_instructional_return bunch
console.log("arrived at")
+
debuglog("arrived at")
console.log(future_return_partition);
+
debuglog(future_return_partition);
 
future_instructional_return[path] = future_return_partition;
 
future_instructional_return[path] = future_return_partition;
 
} else {
 
} else {
console.log("seeking derailed");
+
debuglog("seeking derailed");
 
success_o_meter = true;
 
success_o_meter = true;
 
future_instructional_return[path] = undefined;
 
future_instructional_return[path] = undefined;
Line 169: Line 171:
 
var future_temp_obj = [];
 
var future_temp_obj = [];
 
if ( Array.isArray( temp_obj ) ) {
 
if ( Array.isArray( temp_obj ) ) {
console.log("data is on an array level");
+
debuglog("data is on an array level");
 
Object.keys( temp_obj ).forEach( function ( ect ) {
 
Object.keys( temp_obj ).forEach( function ( ect ) {
 
 
 
// if we are on an array level in the data, lets iterate through the array
 
// if we are on an array level in the data, lets iterate through the array
 
 
console.log("Within partition: " + ect );
+
debuglog("Within partition: " + ect );
 
if ( checkdive( temp_obj[ ect ], moving ) !== undefined ) {
 
if ( checkdive( temp_obj[ ect ], moving ) !== undefined ) {
 
future_temp_obj.push( checkdive( temp_obj[ ect ], moving ) );
 
future_temp_obj.push( checkdive( temp_obj[ ect ], moving ) );
Line 186: Line 188:
 
// if we are not on an array level, proceed with the path
 
// if we are not on an array level, proceed with the path
 
 
console.log("data is on an objective level");
+
debuglog("data is on an objective level");
 
 
 
if ( checkdive( temp_obj, moving ) !== temp_obj ) {
 
if ( checkdive( temp_obj, moving ) !== temp_obj ) {
Line 194: Line 196:
 
temp_obj = future_temp_obj;
 
temp_obj = future_temp_obj;
 
} else {
 
} else {
console.log('nothing to see here')
+
debuglog('nothing to see here')
 
}
 
}
  
Line 240: Line 242:
 
} catch (e) {
 
} catch (e) {
 
// if not JSON, do silly things
 
// if not JSON, do silly things
console.log('not a json ¯\_(ツ)_/¯');
+
debuglog('not a json ¯\_(ツ)_/¯');
 
};
 
};
 
}  
 
}  
+
 
 +
function debuglog( l0g ){
 +
if ( Debug_switch ) {
 +
debuglog( l0g );
 +
}
 +
}
 
}
 
}

Revision as of 09:49, 20 May 2019

// License: GPL
// Wikibase Processor

	function wbContainer( universe, location ) {

		Debug_switch = false;

		this.main = interjson( checkdive( universe, location ));
		var the = this.main;
		
		/* The following is the main exposed function of the current Wb Processing class
			usage cases

			to check the last value in a location given by a preceding array of internal paths to find, with the possibility of array layers between elements of array of paths: 
			wbproc.checkentity( [ wbEStruct['instanceOfRelations'], wbEStruct['IDPropLoc'], wbEStruct['IDName'], 96 ] );

			to return for example content of default value given by array of location paths, or check for existence of 
			wbproc.checkentity( { default_value: [ wbEStruct['defaultValRelation'], wbEStruct['IDPropLoc'] ] } )


		*/

		this.checkentity = function ( paths ) {
			if ( ( the !== undefined ) && ( paths !== undefined ) ){
				return proceed( the, paths );
			}
		}
                
                this.EnqueueWB = async function( idk ) {
			var k = await wbqueue(idk);
			return k;
		}

		async function wbqueue(id) {
			return new Promise(
				function(resolve, reject) {
					$.getJSON('/w/api.php?action=wbgetentities&format=json&ids=' + id, function(json) { 
						var h = json.entities;
						resolve(h);
					})
				}).then( function(result) {
					debuglog(result);
					return result;
				});
		}


		// 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) {  if ( obj !== undefined) { 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){
			if ( array !== undefined ) {
				return (read + '').split('.').reduce(indexinterpolate, array);
			}
		}
		function check(array, read){
			if ( array !== undefined ) {
				return (read + '').split('.').reduce(indexcheck, array);
			}
		}

		function proceed(array, read) {
			var ret_obj = {},
			temp_obj = array,
			// Have a holder for instructional control return array
			success_o_meter = true,
			future_instructional_return = [];
			
//|||||||||||||||||||||||||||||||||||||||||||||||||||||[ I N S T R U C T I O N S  C O N T R O L   ]||||||||||||||||||||||||||||||||||||||||||||||||||||||

			// Init program, check if instructions are an object :
			if ( ( !Array.isArray(read) ) && ( Object.keys(read).length > 0 ) ) {

				
				Object.keys(read).forEach( function(path) {
				debuglog("instructions are an object, in path " + path);

//||||||||||||||||||||||||||[ Check if elements of instruction object are arrays themselves ]||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
					
					if ( Array.isArray (read[path]) ) {
						var future_return_partition = temp_obj;
						Object.keys(read[path]).forEach( function( path2 ) {

							debuglog("instructions are an array of arrays, going through " + read[ path ][ path2 ] );
							
							if ( data_function( future_return_partition, read[ path ][ path2 ] ) != future_return_partition ){
								future_return_partition = data_function( future_return_partition, read[ path ][ path2 ] );
							} else {
								success_o_meter = false;
							}

//|||||||||||||||||||||||||||||||||||||||||||||||||||[   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
							debuglog("arrived at")
							debuglog(future_return_partition);
							future_instructional_return[path] = future_return_partition;
						} else {
							debuglog("seeking derailed");
							success_o_meter = true;
							future_instructional_return[path] = 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 we haven't got a not found signal along the way, let's try returning the object

			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 ) ) {
				debuglog("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
					
					debuglog("Within partition: " + ect );
					if ( checkdive( temp_obj[ ect ], moving ) !== undefined ) {
						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
				
				debuglog("data is on an objective level");
				
				if ( checkdive( temp_obj, moving ) !== temp_obj ) {
					future_temp_obj = checkdive( temp_obj, moving );
				}
				if ( future_temp_obj !== undefined ){
					temp_obj = future_temp_obj;
				} else {
					debuglog('nothing to see here')
				}

			}

			return temp_obj;
		}
							
//|||||||||||||||||||||||||||||||||||||||||||||||||||||[  E N D   O F   S T E P   B L O C K    ]|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

		this.xcontains = function ( unv, angle ) {
			if ( ( unv !== undefined ) && ( angle !== undefined ) ){
				return proceed( unv, angle );
			};
		}

		function checkdive( array, read ){
			if ( check(array, read) !== undefined) {
				return dive(array, read);
			} 
			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
				debuglog('not a json ¯\_(ツ)_/¯');
			};
		} 

		function debuglog( l0g ){
			if ( Debug_switch ) {
				debuglog( l0g );
			}
		}
	}