From b8076460ddeb9d9b3bd6652de96ad2201c1d5765 Mon Sep 17 00:00:00 2001 From: James Lucas Date: Mon, 25 Sep 2023 15:47:47 +1000 Subject: [PATCH] fix: Improve the definition of utils.trimObj() to not modify the source object --- src/js/utils.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/js/utils.js b/src/js/utils.js index 2869dfd80..004882e9f 100644 --- a/src/js/utils.js +++ b/src/js/utils.js @@ -13,11 +13,15 @@ window.fbEditors = { } /** - * Remove null or undefined values - * @param {Object} attrs {attrName: attrValue} - * @return {Object} Object trimmed of null or undefined values - */ -export const trimObj = function (attrs, removeFalse = false) { + * Remove null or undefined values from an object, original object is not modified + * @param {Object} obj {attrName: attrValue} + * @param {boolean} [removeFalse=false] Remove values === false + * @return {Object} Object trimmed of null or undefined values + */ +export const trimObj = function (obj, removeFalse = false) { + if (null == obj || typeof obj !== 'object') return obj + const attrs = (typeof window.structuredClone === 'function') ? window.structuredClone(obj) : Object.assign({}, obj) + /** @type {(null|undefined|''|false)[]} xmlRemove */ const xmlRemove = [null, undefined, ''] if (removeFalse) { xmlRemove.push(false)