Difference between revisions of "User:Abel/WbProcessor.js"
Jump to navigation
Jump to search
m |
m |
||
| Line 1: | Line 1: | ||
// License: GPL | // License: GPL | ||
// | // | ||
| − | |||
| − | |||
function wbContainer( universe, location ) { | function wbContainer( universe, location ) { | ||
this.main = interjson( get_Y_from_X( universe, location )); | this.main = interjson( get_Y_from_X( universe, location )); | ||
| + | var container = this; | ||
| − | this.checkentity = function ( rel_id_list, | + | this.checkentity = function ( rel_id_list, paths ) { |
| − | var return_obj = | + | var return_obj = [], |
| − | Object.keys(rel_id_list). | + | temporary_obj = [], |
| − | + | baseRelation; | |
| − | + | if ( Object.keys(rel_id_list).length > 0 ) { | |
| − | if ( | + | Object.keys(rel_id_list).forEach(item){ |
| − | + | temporary_obj = []; | |
| + | baseRelation = checkdive( container.main, rel_id_list[item][0] ); | ||
| + | rise( temporary_obj, depthdive( baseRelation, paths )); | ||
| + | if ( Object.keys(temporary_obj).length > 0 ) { | ||
| + | Object.keys(temporary_obj).forEach(hit){ | ||
| + | if ( temporary_obj[hit] == rel_id_list[item][1] ) { | ||
| + | return_obj.push(rel_id_list[item]); | ||
| + | } | ||
| + | } | ||
| + | } else if ( temporary_obj == rel_id_list[item][1] ) { | ||
| + | return_obj.push(rel_id_list[item]); | ||
} | } | ||
} | } | ||
| − | } | + | } |
return return_obj; | return return_obj; | ||
} | } | ||
| Line 26: | Line 35: | ||
// dive selects matrix.a.b.c.d from the array called matrix and 'a.b.c.d' as string | // 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. | // 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){ | function dive(array, read){ | ||
return read.split('.').reduce(indexinterpolate, array); | return read.split('.').reduce(indexinterpolate, array); | ||
} | } | ||
| + | |||
function check(array, read){ | function check(array, read){ | ||
| − | return read.split('.').reduce(indexcheck, array); | + | if ( array != undefined ) { |
| + | return read.split('.').reduce(indexcheck, array); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | function checkdive(array, read){ | ||
| + | if ( check(array, read) != undefined) { | ||
| + | return dive(array, read); | ||
| + | } else if ( Object.keys(array).length > 0 ) { | ||
| + | return complexcheck( array, read ); | ||
| + | } | ||
} | } | ||
| − | function | + | |
| − | + | function complexcheck(array, read){ | |
| − | + | var ret_obj; | |
| − | + | Object.keys(array).forEach(item){ | |
| − | + | rise( ret_obj, checkdive(array[item], read)) ; | |
| − | + | } | |
| − | + | return ret_obj; | |
| − | } | + | } |
| − | + | ||
| − | function | + | function depthdive(array, read){ |
| − | + | var ret_obj, | |
| − | + | temp_obj = array; | |
| − | + | if ( Object.keys(read).length > 0 ) { | |
| − | + | Object.keys(read).forEach(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){ | |
| − | function | + | if ( to_push != undefined) { |
| − | if ( | + | if ( Object.keys(to_push).length > 0 ){ |
| − | + | Object.keys(to_push).forEach(item){ | |
| − | + | if ( array != undefined ) { | |
| − | + | array.push(to_push[item]); | |
| − | + | } | |
| − | + | } | |
| − | + | } else { | |
| − | + | array.push(to_push); | |
| − | } | + | } |
} | } | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
} | } | ||
| + | |||
| + | function interjson( obj ) { | ||
| + | try { | ||
| + | return JSON.parse( obj ) | ||
| + | } catch (e) { | ||
| + | // if not JSON, do silly things | ||
| + | console.log('not a json ¯\_(ツ)_/¯'); | ||
| + | }; | ||
| + | } | ||
| + | |||
} | } | ||
Revision as of 10:37, 14 May 2019
// License: GPL
//
function wbContainer( universe, location ) {
this.main = interjson( get_Y_from_X( universe, location ));
var container = this;
this.checkentity = function ( rel_id_list, paths ) {
var return_obj = [],
temporary_obj = [],
baseRelation;
if ( Object.keys(rel_id_list).length > 0 ) {
Object.keys(rel_id_list).forEach(item){
temporary_obj = [];
baseRelation = checkdive( container.main, rel_id_list[item][0] );
rise( temporary_obj, depthdive( baseRelation, paths ));
if ( Object.keys(temporary_obj).length > 0 ) {
Object.keys(temporary_obj).forEach(hit){
if ( temporary_obj[hit] == rel_id_list[item][1] ) {
return_obj.push(rel_id_list[item]);
}
}
} 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 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){
if ( array != undefined ) {
return read.split('.').reduce(indexcheck, array);
}
}
function checkdive(array, read){
if ( check(array, read) != undefined) {
return dive(array, read);
} else if ( Object.keys(array).length > 0 ) {
return complexcheck( array, read );
}
}
function complexcheck(array, read){
var ret_obj;
Object.keys(array).forEach(item){
rise( ret_obj, checkdive(array[item], read)) ;
}
return ret_obj;
}
function depthdive(array, read){
var ret_obj,
temp_obj = array;
if ( Object.keys(read).length > 0 ) {
Object.keys(read).forEach(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){
if ( to_push != undefined) {
if ( Object.keys(to_push).length > 0 ){
Object.keys(to_push).forEach(item){
if ( array != undefined ) {
array.push(to_push[item]);
}
}
} else {
array.push(to_push);
}
}
}
function interjson( obj ) {
try {
return JSON.parse( obj )
} catch (e) {
// if not JSON, do silly things
console.log('not a json ¯\_(ツ)_/¯');
};
}
}