-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2123,6 +2123,35 @@ H5P.trim = function (value) { | |
// So should we make this function deprecated? | ||
}; | ||
|
||
/** | ||
* Recursive function that detects deep empty structures. | ||
* | ||
* @param {*} value | ||
* @returns {bool} | ||
*/ | ||
H5P.isEmpty = value => { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
icc
Member
|
||
if (!value && value !== 0 && value !== 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 (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 | ||
} | ||
return false; | ||
}; | ||
|
||
/** | ||
* Check if JavaScript path/key is loaded. | ||
* | ||
|
@Languafe I checked that new function in H5P core. Just suggestions:
Using
Array.prototype.every
feels more concise, even though there's nothing wrong with thefor
-loop.I also wonder about using
bool
as the type in the JSDoc comment. Why notboolean
(which is whattypeof true
ortypeof false
would return (as the primitive data type) and what's commonly used throughout H5P code)?Yes, I have too much time, obviously :-D