@appsemble/utils
Version:
Utility functions used in Appsemble internally
56 lines • 1.41 kB
JavaScript
/**
* Return the input data.
*
* @param data The data to return.
* @returns The input data.
*/
export function identity(data) {
return data;
}
/**
* Throw the input data.
*
* @param data The data to throw.
* @throws The input data.
*/
export function rethrow(data) {
throw data;
}
/**
* Strip all null, undefined, and empty array values from an object.
*
* @param value The value to strip.
* @param options Additional options for stripping null values.
* @returns A copy of the input, but with all nullish values removed recursively.
*/
export function stripNullValues(value, { depth = Number.POSITIVE_INFINITY, ...options } = {}) {
if (value == null) {
return;
}
if (typeof value !== 'object') {
return value;
}
if (value instanceof Blob) {
return value;
}
if (depth <= 0) {
return value;
}
if (Array.isArray(value)) {
const result = [];
for (const val of value) {
if (val != null) {
result.push(stripNullValues(val, { depth: depth - 1, ...options }));
}
}
return result;
}
const result = {};
for (const [key, val] of Object.entries(value)) {
if (val != null) {
result[key] = stripNullValues(val, { depth: depth - 1, ...options });
}
}
return result;
}
//# sourceMappingURL=miscellaneous.js.map