From 4e76d261f5cf006b0971e1815a8b688368c77394 Mon Sep 17 00:00:00 2001 From: "S.Listl" <s.listl@adito.de> Date: Mon, 24 Aug 2020 15:33:45 +0200 Subject: [PATCH] Added function Utils.parseBoolean --- process/Util_lib/process.js | 46 +++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/process/Util_lib/process.js b/process/Util_lib/process.js index b005263bfa..f7f70a01f2 100644 --- a/process/Util_lib/process.js +++ b/process/Util_lib/process.js @@ -134,56 +134,72 @@ Utils.isEqual = function (pFirstObject, pSecondObject) /** * Checks if the given value is a function. * - * @param {Object} pObject the value to check + * @param {Object} pValue the value to check * @return {Boolean} true if the value is a function */ -Utils.isFunction = function (pObject) +Utils.isFunction = function (pValue) { - return typeof pObject === "function"; + return typeof pValue === "function"; } /** * Checks if the given value is a string. * - * @param {Object} pObject the value to check + * @param {Object} pValue the value to check * @return {Boolean} true if the value is a string */ -Utils.isString = function (pObject) +Utils.isString = function (pValue) { - return typeof pObject === "string"; + return typeof pValue === "string"; } /** * Checks if the given value is a number. * - * @param {Object} pObject the value to check + * @param {Object} pValue the value to check * @return {Boolean} true if the value is a number */ -Utils.isNumber = function (pObject) +Utils.isNumber = function (pValue) { - return typeof pObject === "number"; + return typeof pValue === "number"; } /** * Checks if the given value is an object. Be careful, null is also considered "object". * - * @param {Object} pObject the value to check + * @param {Object} pValue the value to check * @return {Boolean} true if the value is an object */ -Utils.isObject = function (pObject) +Utils.isObject = function (pValue) { - return typeof pObject === "object"; + return typeof pValue === "object"; } /** * Checks if the given value is a boolean. * - * @param {Object} pObject the value to check + * @param {Object} pValue the value to check * @return {Boolean} true if the value is a boolean */ -Utils.isBoolean = function (pObject) +Utils.isBoolean = function (pValue) { - return typeof pObject === "boolean"; + return typeof pValue === "boolean"; +} + +/** + * Parses the given value to Boolean, this can be used to check for stringified Booleans. These rules apply: + * <ul> + * <li>If the value is either falsy, the string "false" or the string "0", false is returned</li> + * <li>If the valueOf method of the value returns a falsy value, false is returned + * (this is to make sure the function returns false for weird stuff like 'new Boolean(false)')</li> + * <li>Every other value results in true</li> + * + * @param {String|Object} pValue the value to parse + * @return {Boolean} a real boolean + */ +Utils.parseBoolean = function (pValue) +{ + return !(!pValue || pValue === "0" || pValue === "false" || !(pValue.valueOf())); } /** -- GitLab