SetIfEmpty

Checks an object for an empty value and returns a pre-set default value.

javascript · Filters And Transformers

Source

function toBoolean(value) {
    if (typeof (value) === 'string') {
        if (['yes', 'true'].indexOf(value.toLowerCase()) != -1) {
            return true;
        } else if (['no', 'false'].indexOf(value.toLowerCase()) != -1) {
            return false;
        }
        throw 'Argument does not contain a valid boolean-like value';
    }
    return value ? true : false;
}

function isValueEmpty(value) {
    if (Array.isArray(value) && value.length === 1) {
        value = value[0];
    }

    const valueIsStringNull = typeof value === 'string' && (value.toLowerCase() === 'none' || value.toLowerCase() === 'null' || value === '');

    return value === null || (Array.isArray(value) && value.length === 0) || (value.constructor == Object && Object.keys(value).length === 0) || valueIsStringNull;
}


function getValueToSet(value) {
    const applyIfEmpty = toBoolean(args.applyIfEmpty);

    if (value == null || (applyIfEmpty && isValueEmpty(value))) {
        value = args.defaultValue;
    }

    if (value == null) {
        return [];
    } else if (Array.isArray(value)) {
        return JSON.parse(JSON.stringify(value));  // solve reference issue from chaining transformers XSUP-32809
    } else {
        return value;
    }
}


function main() {
    let value = args.value;
    return getValueToSet(value);
}


try {
    return main();
} catch (error) {
    throw 'Error occurred while running the script:\n' + error;
}

README

Checks an object for an empty value and returns a pre-set default value.

Script Data


Name Description
Script Type javascript
Tags transformer, general, entirelist
Cortex XSOAR Version 5.0.0

Inputs


Argument Name Description
value The object value to check, if empty.
defaultValue The new value to return if the original value was empty.
applyIfEmpty If set to true, empty strings, arrays and dictionaries and the strings “None” and “Null” are considered empty entities and the default value is returned.

Outputs


There are no outputs for this script.