removing items from nested objects

master
Theunis J. Botha 2021-10-02 10:40:24 +02:00
parent c9fa144d02
commit ba008e8e10
1 changed files with 62 additions and 34 deletions

View File

@ -229,53 +229,58 @@ class SystemLinking extends System {
//GENERATED_STATIC_SANITY_CHECKS_METHOD_END //GENERATED_STATIC_SANITY_CHECKS_METHOD_END
//CUSTOM_STATIC_SANITY_CHECKS_METHOD_START //CUSTOM_STATIC_SANITY_CHECKS_METHOD_START
/**
* First we check if this is a required property
*/
if (!r3Object.required.hasOwnProperty(property)) {
return;
}
/** for (let r = 0; r < r3Object.required.length; r++) {
* We know this property is required - so continue..
*/
if (r3Object.required[property] instanceof Array) {
/** /**
* First we need to check that this value conforms to the requirement of being an Array * First we check if this is a required property
*/ */
if (!(value instanceof Array)) { if (!r3Object.required[r].hasOwnProperty(property)) {
throw new TypeError('The property ' + property + ' of ' + r3Object.name + ' was not properly defined - it should be an array');
}
if (value.length === 0) {
return; return;
} }
/** /**
* Check all items in the array are valid objects of type r3Object.required[property] * We know this property is required - so continue..
*/ */
for (let i = 0; i < value.length; i++) { if (r3Object.required[r][property] instanceof Array) {
r3Object.required[property].map(
function(constructor) { /**
if (!(value[i] instanceof constructor)) { * First we need to check that this value conforms to the requirement of being an Array
throw new TypeError('The property ' + property + ' of this object is not of the correct type'); */
if (!(value instanceof Array)) {
throw new TypeError('The property ' + property + ' of ' + r3Object.name + ' was not properly defined - it should be an array');
}
if (value.length === 0) {
return;
}
/**
* Check all items in the array are valid objects of type r3Object.required[property]
*/
for (let i = 0; i < value.length; i++) {
r3Object.required[r][property].map(
function (constructor) {
if (!(value[i] instanceof constructor)) {
throw new TypeError('The property ' + property + ' of this object is not of the correct type');
}
} }
} );
); }
} } else {
} else {
if (value === null) { if (value === null) {
return; return;
} }
if (typeof value === 'undefined') { if (typeof value === 'undefined') {
return; return;
} }
if (!(value instanceof r3Object.required[r][property])) {
throw new TypeError('The property ' + property + ' of ' + r3Object.name + ' is not of the correct type')
}
if (!(value instanceof r3Object.required[property])) {
throw new TypeError('The property ' + property + ' of ' + r3Object.name + ' is not of the correct type')
} }
} }
@ -325,6 +330,29 @@ class SystemLinking extends System {
* We need to check if we removed an item from the existing array * We need to check if we removed an item from the existing array
*/ */
if (r3Object[property].length > 0) { if (r3Object[property].length > 0) {
/**
* Find the missing value (if any) from the old array
*/
let unlinked = r3Object[property].reduce(
function(result, object) {
if (value.indexOf(object) === -1) {
result.push(object);
}
return result;
},
[]
);
unlinked.map(
function(unlinkObject) {
Utils.RemoveFromArray(unlinkObject.parents, r3Object);
Utils.RemoveFromArray(r3Object.children, unlinkObject);
if (r3Object instanceof Entity) {
Utils.RemoveFromArray(r3Object.components, unlinkObject);
}
}
);
} }