From ea6889f84e8f44b4060025204ca019f13522dcae Mon Sep 17 00:00:00 2001 From: Oliver Tacke Date: Fri, 22 Dec 2023 14:51:33 +0100 Subject: [PATCH] Increase intelligibility of ifEmpty function --- js/h5p.js | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/js/h5p.js b/js/h5p.js index f03a9598..783a63a3 100644 --- a/js/h5p.js +++ b/js/h5p.js @@ -2124,31 +2124,22 @@ H5P.trim = function (value) { }; /** - * Recursive function that detects deep empty structures. + * Recursively detect deep empty structures. * - * @param {*} value - * @returns {bool} + * @param {*} item Item to check for being empty. + * @returns {boolean} True if empty. */ -H5P.isEmpty = value => { - if (!value && value !== 0 && value !== false) { +H5P.isEmpty = (item) => { + if (!item && item !== 0 && item !== false) { return true; // undefined, null, NaN and empty strings. } - else if (Array.isArray(value)) { - for (let i = 0; i < value.length; i++) { - if (!H5P.isEmpty(value[i])) { - return false; // Array contains a non-empty value - } - } - return true; // Empty array + else if (Array.isArray(item)) { + return item.every((item) => H5P.isEmpty(item)); } - else if (typeof value === 'object') { - for (let prop in value) { - if (value.hasOwnProperty(prop) && !H5P.isEmpty(value[prop])) { - return false; // Object contains a non-empty value - } - } - return true; // Empty object + else if (typeof item === 'object') { + return Object.values(item).every((item) => H5P.isEmpty(item)); } + return false; };