Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase intelligibility of ifEmpty function #164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 10 additions & 19 deletions js/h5p.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down